How to upload multiple files on IPFS under the same CID with cURL

Hello everybody,

If you’d like to add multiple files to IPFS under a CID that wraps them all together, please see the below example request.
In order to do so, use the “wrap-with-directory” parameter and set it equal to “true”. Add multiple files, with their full path, to your request by specifying as many -F options.
Don’t forget to set the “pin” parameter equal to true if you’d like to pin the CID of the directory object to your Infura IPFS project, and to use your Project ID and Project Secret when authenticating the request.

curl "https://ipfs.infura.io:5001/api/v0/add?pin=true&wrap-with-directory=true" \
        -X POST \
        -u "Project Id:API KEY SECRET" \
        -H "Content-Type: multipart/form-data" \
        -F file=@"/home/user/ipfs_files/picture1.JPG" -F file=@"/home/user/ipfs_files/picture2.JPG" -F file=@"/home/user/ipfs_files/picture3.JPG"

The request outputs the CID of each of the files and the CID of the directory object that wraps them together, as per the below.

    {"Name":"picture1.JPG","Hash":"QmcUio5Tg3UEPz8Df6WtebAV1qnuPzPFMQcn5rCJufQgUP","Size":"3367989"}
    {"Name":"picture2.JPG","Hash":"QmP1cmcwSes1MJMLyyiDm4RZksPCsoMnNkJzwUsdhf3Arc","Size":"3521320"}
    {"Name":"picture3.JPG","Hash":"QmSUWunr458oHugFszuzxJosTupqSqqavojbRTWozHEeX4","Size":"4807337"}
    {"Name":"","Hash":"QmdFRorEekCMHd2zxDiZ59wfb3N5Kb7a6eVbNUZp3iniYB","Size":"11696821"}

As mentioned earlier, only the CID of the directory object will be pinned to your Infura IPFS project. To list the files under the directory you can use the Infura public gateway or your own dedicated gateway.

Also through our community we have learned of a useful solution when you have a bunch of files and don’t want add them manually to your request:

cd /your/chosen/path
FILES=$(find * -type f | grep -v ' ' | sed -e 's/ /\\ /g' | awk -v q="'" '{print " -F " q "file=@\"" $0 "\"" q}')
curl "https://ipfs.infura.io:5001/api/v0/add?pin=true&wrap-with-directory=true" -X POST -H "Content-Type: multipart/form-data" $FILES
6 Likes

Do you by any change, have a working example to add a full directory in node.js using ipfs-http-client

1 Like

Hey Arif, welcome to the community.

Please see the below article:

and the code example below:

const { globSource, create} = require('ipfs-http-client')
const projectId = '27xxxsj';
const projectSecret = '20xxx5b';

async function addFolder(){
  const auth =
    'Basic ' + Buffer.from(projectId + ':' + projectSecret).toString('base64')
    const client =  create({

        host: 'ipfs.infura.io',
        port: 5001,
        protocol: 'https',
        headers: {
          authorization: auth
        }
      })

    for await (const file of client.addAll(globSource('/home/user/path/to/directory', '**/*'))) {
        console.log(file)
        }
    }

addFolder()