Lesson 14: Error when uploading to nft.storage #1120
-
This is my js code where I try to upload my images to nft.storage.
After
I can't figure out how to fix this issue but I am guessing for some reason it can't upload my files to nft. storage. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 2 replies
-
Beta Was this translation helpful? Give feedback.
-
Try using this const { NFTStorage, File } = require("nft.storage")
const mime = require("mime")
const fs = require("fs")
const path = require("path")
require("dotenv").config()
const NFT_STORAGE_KEY = process.env.NFT_STORAGE_KEY
/**
* Reads an image file from `imagePath` and stores an NFT with the given name and description.
* @param {string} imagePath the path to an image file
* @param {string} name a name for the NFT
* @param {string} description a text description for the NFT
*/
async function storeNFTs(imagesPath) {
const fullImagesPath = path.resolve(imagesPath)
const files = fs.readdirSync(fullImagesPath)
let responses = []
for (fileIndex in files) {
const image = await fileFromPath(
`${fullImagesPath}/${files[fileIndex]}`
)
console.log(image)
const nftstorage = new NFTStorage({ token: NFT_STORAGE_KEY });
const dogName = files[fileIndex].replace(".png", "");
const response = await nftstorage.store({
image,
name: dogName,
description: `An adorable ${dogName}`,
// Currently doesn't support attributes 😔
// attributes: [{ trait_type: "cuteness", value: 100 }],
});
responses.push(response);
}
//return responses;
}
/**
* A helper to read a file from a location on disk and return a File object.
* Note that this reads the entire file into memory and should not be used for
* very large files.
* @param {string} filePath the path to a file to store
* @returns {File} a File object containing the file content
*/
async function fileFromPath(filePath) {
const content = await fs.promises.readFile(filePath)
const type = mime.getType(filePath)
return new File([content], path.basename(filePath), { type })
}
Alo try running this script separately and getting the reponses before using it in the |
Beta Was this translation helpful? Give feedback.
-
There was some issue with my private key which was getting me the error. |
Beta Was this translation helpful? Give feedback.
-
I was having same issue because there was an extra space in my token, when i passed the correct token it works correctly |
Beta Was this translation helpful? Give feedback.
Try using this