Using a Node.js Script to Upload a Folder to a Remote IPFS Node

Itay Podhajcer
3 min readJan 29, 2020

--

Not always running ipfs add or browsing to the webui to upload a file or a folder is possible. There are scenarios where the only option is to use a command line tool to upload to a remote IPFS node (such as automated processes).

If you are thinking curl, then you are right. You can use it with the -F (or — form see here) option to upload a file (The IPFS API expects a POST request with a multipart/form-data content type). But, that option cannot accept a folder, meaning you either specify -F for each file you need to upload, or you can create a script that does that for you using existing npm packages.

Prerequisites

As we will be writing a Node.js script, you can download it from here and install it.
Additionally, we will need an IPFS node we can use for our uploads, so you can take a look one of my previous articles on running your own IPFS node on Azure:

The CLI Tool

As I was working on the example script, I realized that it would make sense to publish an npm package of a CLI tool (with a few options), that can take a folder path and upload it a remote IPFS node. Feel free to try it out (note the installation instructions):

You can also find the source code for the npm package here:

The Script

We will start by creating a folder for our Node.js script and in it execute npm init (the values request by the command are not important for our example).
Once complete, create a new JavaScript file which will hold our script.

We will be creating a script that reads two command line arguments that represent the path and the node URL, recursively gathers all the file names inside the the folder (with relative paths) and finally uploads the contents of those files to IPFS.

  • We will start by installing and then importing fs, path and ipfs-http-client packages:
  • Now to creating a function that reads the files:

Notice that we are replacing \ with /, this is required when running the script in Windows, as paths returned by functions from thepath package use this format folder\subfolder, but IPFS requires folder/subfolder.

  • Next we will create our main execution logic (the path and the URL will be located inside argv[2] and argv[3] respectively):

The call to ipfs.cp will create a named folder (using the name of the folder we uploaded), just like when uploading a folder through the webui.

The full script should look similar to this:

Testing The Script

Now that the script is complete, you can try it by running it with node:

Conclusion

This script is a basic example of how a remote upload of a folder can be accomplished. It can be improved much further with, for example, a command line interface package such as commander, provide additional options that tweak the upload process or the output and even adding more extensive logging.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Written by Itay Podhajcer

Tech expert with 20+ years’ experience as CTO, Chief Architect, and Consultant. 3x Microsoft MVP award winner. Passionate blogger and open-source contributor

Responses (1)

Write a response