How to upload a folder of images, by giving only path using npm's pinata sdk ? I only want to give folder's path. using javascript.
3 Answers
Ok, After a lot of R&D,found out that you cannot, upload folder of images using the pinata sdk(npm pack.). But you can use Moralis and IPFS public gateway.
You can use:
const IMAGES_LOCATION = "path/to/folder";
const imageFolderUploadResponse = await pinata.pinFromFS(IMAGES_LOCATION);
It's pretty simple using node.js (Javascript)
- First resolve the dir path
- Read by fs module in node.js
- Put the stuff in loop as per below code and then it will automatically pin your files to the Pinat
async function storeImages(imagesDirPath) {
const fullImagesPath = path.resolve(imagesDirPath);
const files = fs.readdirSync(fullImagesPath);
let responses = [];
console.log("Uploading to Pinata !!!");
console.log("paths ", fs.createReadStream(fullImagesPath + "/" + files[0]));
for (fileIndex in files) {
const readableStreamForFile = fs.createReadStream(
`${fullImagesPath}/${files[fileIndex]}`
);
try {
const response = await pinata.pinFileToIPFS(readableStreamForFile);
responses.push(response);
} catch (error) {
console.log(error);
}
}
return { responses, files };
}