nftstorage / nft.storage

**Notice: Uploads Decommissioned - Existing Data is Safe!** NFT.Storage Classic (classic.nft.storage) offers free decentralized storage and bandwidth for NFTs on IPFS and Filecoin. June 2024 Update: No new uploads, but all existing data is safe. Try the new NFT.Storage, which preserves data in long term Filecoin storage. Go to app.nft.storage
https://classic.nft.storage
Other
621 stars 168 forks source link

Getting TypeError when using Next-api route to storeDirectory on NFT.Storage #2198

Closed joydmukherjee closed 1 year ago

joydmukherjee commented 1 year ago

Hi I am trying to use Next.js api routes to store an image file on NFT.storage. I am taking an image as an input from the user in the browser and passing that to the api file using Axios and FormData const formData = new FormData(); formData.append('image', imageUrl); const cid = await axios.post('api/uploadNFTData', formData); here the imageUrl is event.target.files[0] and in the api folder I have uploadNFTData.js containing this code I have done this to keep my API key more secure by taking the value from the .env ` import { NFTStorage, File, Blob } from 'nft.storage'; import formidable from 'formidable'; const client = new NFTStorage({ token: process.env.NFT_STORE_TOKEN}); // first we need to disable the default body parser export const config = { api: { bodyParser: false, }, } export default async function uploadNFTData(req,res) { const form = new formidable.IncomingForm(); form.uploadDir = "./"; form.keepExtensions = true; var formfields = await new Promise(function (resolve, reject) { form.parse(req, (err, fields, files) => { if (err) { reject(err); return; } resolve(files); console.log("within form.parse method, subject field of fields object is: " + files);

    }); // form.parse
});

try{ const cid = await client.storeDirectory([formfields]); res.status(200).json({ message: cid });
}catch(err){ res.status(500).json({ message: "could not upload data" }); } } `

Now I am getting the following error on server side error - TypeError: blob.stream is not a function Can someone please help regarding what I am doing wrong?? Or This kind of call cannot be done??

yusefnapora commented 1 year ago

It looks like you may want

const cid = await client.storeDirectory(files)

instead of

const cid = await client.storeDirectory([formfields]);

If I'm reading that right, formfields contains the unparsed form data?

alanshaw commented 1 year ago

@joydmukherjee did this fix things for you?

vasco-santos commented 1 year ago

@joydmukherjee Closing this for lack of activity. In case you are still hitting issues please re-open and let us know :)