Directly create an IPFS directory, to contain existing IPFS objects

Let’s say I have found a collection of various PDF docs hosted on IPFS. And I know the CIDs for all these objects. I want to put them together in a single directory.

Is it possible to create a directory to contain all these files purely based on a list of their CIDs - without first downloading them locally and adding them myself?

If I was to pin them intially, could I create a wrapping directory just by referring to a list of the CIDs I wanted to include in this directory?

If so, how?

Thanks.

See ipfs object patch or perhaps, more simply, add CIDs to a folder on MFS and then get the CID of that folder (see ipfs files --help). Since you only want to do it based on their cids, object patch add-link is the way.

Thanks Hector.
Yes - I realized I could do this via my local MFS, but it seemed to be an unnecessary extra step, when it appeared I had everything that should be required already.

Just reading the docs ipfs object patch add-link docs. Is it possible to provide multiple refs to a single link? I guess I’ll have to play about with it, to test this …

Having played about with this it is possible to do exactly what I requested…

Although - patching the directory object with a new item to contain within it, changes the CID of the directory.

So it requires iteratively looping through the CIDs of all the items you want to contain within the directory, (as I don’t think you can add many at once) saving the new CID of the directory itself after each addition (because it’s changed) and feeding that CID in the command for the next addition.

Thus - as you suggested - it is probably no less hassle to simply use your local MFS!

But … the more you know! :upside_down_face:

There is another way…

  1. Create a folder and put some stuff with it. Add to ipfs.
  2. ipfs dag get <folder>. You get a dag node serialized as json. This gives you an idea of what you need to create for your “custom” folder.
  3. Create a json with the names/CIDs/sizes for your folder in the same way as the above. In my test, the folder has a "data": "CAE=", you probably need to keep this (guessing is the encoded protobuf for “unixfs folder”
  4. ipfs dag put --format protobuf yourfolder.json

You will get the CID of your folder.

The above will not work for very large folders. IPFS does additional sharding when folder block sizes are too big (i.e. if your json is over 1MiB or so).

Interesting. I guess scripting some JSON is no more work than writing a shell script to loop through the CIDS. Might be less work.

Thanks.