Uploading 10K Images to IPFS
In this blog post, we are going to see how to upload 10K images to IPFS
and later on how to upload all the JSONs
into one folder, so we can use that route to reference it from our smart contract tokenURI
method.
Prerequisites
- Create an account at nft.storage and get an API key.
- Have your
10K images
and theirJSONs
. EachJSON
should have attributes defining the parts of the image.
For example:
"attributes": [{
"trait_type": "background",
"value": "Pink"
},
{
"trait_type": "beard",
"value": "None"
}]
Uploading the Images
First, we need to upload each image individually and save their IPFS
address.
We will call the following method (or something similar) that will receive a file path and return an object with the IPFS
path.
async function uploadToIPFS(filePath) {
const response = await nftStorage.store({
image: fs.readFileSync(filePath),
name: path.basename(filePath),
description: "NFT Image",
});
return response.ipfsUrl;
}
I advise you to do this in batches, upload 500
to IPFS
and save their IPFS
paths in your local directory folder. What I will do is store the final JSON
I want to be returned from my smart contract when calling tokenURI(tokenID)
.
This JSON
has to include an image
property, a description
, and the array of attributes
. The JSON
should be saved in the form of 1.json
, 2.json
, and so on.
{
"name": "NFT #1",
"description": "This is an NFT",
"image": "ipfs://bafybeid...",
"attributes": [
{
"trait_type": "background",
"value": "Pink"
},
{
"trait_type": "beard",
"value": "None"
}
]
}
Once you have your 10K images
uploaded and the 10K JSON
generated, it's time to upload the directory with the 10K JSON
and get the final IPFS
hash that will be used as a tokenURI
.
The method storeDirectory
will return an IPFS
hash that will admit parameters as the last part of the URL. In this case, the file.name
property will be 1
, 2
, 3
, so you will be able to access it through the URL.
Example:
https://bafybeifnzchyeg7ihapnrumlmquybi6mj7y6i4gwifjbzgjj7ym6u7eguy.ipfs.dweb.link/1
Good luck, and may the force be with you!