I was trying to upload image to pinata ipfs cloud from my frontend app of react js. But it shows me error:Unhandled Rejection (TypeError): fs.createReadStream is not a function. .
Here is my code snippet:
require('dotenv').config();
const fs = require('fs');
const axios = require('axios');
const FormData = require('form-data');
const key = process.env.REACT_APP_PINATA_KEY;
const secret = process.env.REACT_APP_PINATA_SECRET;
export const pinFileToIPFS = async (filePath) => {
const url = "https://api.pinata.cloud/pinning/pinFileToIPFS";
//we gather a local file for this example, but any valid readStream source will work here.
let data = new FormData();
data.append('file',fs.createReadStream(filePath));
return axios.post(url,
data,
{
maxContentLength: 'Infinity', //this is needed to prevent axios from erroring out with large files
headers: {
'Content-Type': `multipart/form-data; boundary=${data._boundary}`,
'pinata_api_key': key,
'pinata_secret_api_key': secret
}
})
.then(function (response) {
//handle response here
return {
success: true,
pinataUrl: "https://gateway.pinata.cloud/ipfs/" + response.data.IpfsHash
};
})
.catch(function (error) {
//handle error here
console.log(error)
return {
success: false,
message: error.message,
}
});
};
How to fix it Or Is there any way of doing this rather than using 'fs'?