Skip to main content

IPFS Upload

Let's upload our files to IPFS.

We will follow the following steps:

  1. Create an NFT.Storage account.
  2. Upload files and directories
    1. You can use the desktop app NFTUp
    2. Or, you can use the javascript library as we will use below.
  3. Get the NFT Storage Token from the dashboard. We are going to be needing this.

Let's start writing some code.

Install

npm install nft.storage --save

Setup

Import

import { NFTStorage } from "nft.storage";

Initialise

You can get your IPFS_TOKEN from the NFT.Storage dashboard.

var storage = await new NFTStorage({ token: IPFS_TOKEN });

Upload File

tip

If you face issues with using the NFTStorage SDK because of data format, try using the NFTStorage HTTP endpoint

import fs from "fs";

// Read File
const file = fs.readFileSync(filepath);

// Create a blob of the file.
let blob = new Blob([file]);

// Upload to IPFS and get the cid
const cid = await storage.storeBlob(blob);
console.log("cid:", cid);

Now, we got our cid. We can form it into an object as such:

{
"assetType": "image",
"cid": cid
}

Upload Directory

We can also upload entire directory to IPFS. To do so, get the absolute directory path as dirpath and follow the code snippet below:

import { filesFromPath } from "files-from-path";

// Read all files in the directory path
const files = filesFromPath(dirpath, {
pathPrefix: path.resolve(dirpath),
hidden: true,
});

// Upload Directory to IPFS and get the cid of the directory.
let cidDir = await storage.storeDirectory(files);

You can always check the status and files uploaded using your token from the nft.storage dashboard.