How to create an IPFS folder without having original files?

I want to publish a new folder with some files inside, using only CIDs of files.

For example, I have files

 echo "file1" | ipfs add
added Qmae3RedM7SNkWGsdzYzsr6svmsFdsva4WoTvYYsWhUSVz

echo "file2" | ipfs add
added QmUtkGLvPf63NwVzLPKPUYgwhn8ZYPWF6vKWN3fZ2amfJF

I want to create a new folder with files:

  • file1.txt with CID = Qmae3RedM7SNkWGsdzYzsr6svmsFdsva4WoTvYYsWhUSVz
  • file2.txt with CID = QmUtkGLvPf63NwVzLPKPUYgwhn8ZYPWF6vKWN3fZ2amfJF

Is it possible to created such folder having only these 2 CIDs without downloading original files?

1 Like

Welcome to the IPFS forum, Sergio,

If I understand you correctly, you would like to create a CID representing a folder containing the two files.

Is it possible to created such folder having only these 2 CIDs without downloading original files?

Technically, that should be possible.

As far as I know, creating a CID for the folder containing the two files will have its hash calculated based on the CIDs of the children. So having just the CIDs should be sufficient without downloading the file.

However, you’ll probably need to write code for this because I don’t think it can be done using the Kubo cli.

Try this:

> ipfs files mkdir /test
> ipfs files cp /ipfs/Qmae3RedM7SNkWGsdzYzsr6svmsFdsva4WoTvYYsWhUSVz /test/file1.txt
> ipfs files cp /ipfs/QmUtkGLvPf63NwVzLPKPUYgwhn8ZYPWF6vKWN3fZ2amfJF /test/file2.txt
> ipfs files ls -l /test
file1.txt	Qmae3RedM7SNkWGsdzYzsr6svmsFdsva4WoTvYYsWhUSVz	6
file2.txt	QmUtkGLvPf63NwVzLPKPUYgwhn8ZYPWF6vKWN3fZ2amfJF	6
> ipfs files ls -l /
test/	QmW85q9pmby55reZb9YrcLRr5L2gHcvTFGoSmjADCbhq1w	0
> 

:blush:

6 Likes

If you want something a little more “hands on”, first create the following json file (I named mine dag.json):

{
	"Data": {
		"/": {
			"bytes": "CAE"
		}
	},
	"Links": [
		{
			"Hash": {
				"/": "Qmae3RedM7SNkWGsdzYzsr6svmsFdsva4WoTvYYsWhUSVz"
			},
			"Name": "file1.txt",
			"Tsize": 14
		},
		{
			"Hash": {
				"/": "QmUtkGLvPf63NwVzLPKPUYgwhn8ZYPWF6vKWN3fZ2amfJF"
			},
			"Name": "file2.txt",
			"Tsize": 14
		}
	]
}

Then add it to your node:

> ipfs dag put --store-codec=dag-pb dag.json
bafybeidtuvmy7ratlnfkuh5pkz4whjvqa3367bok43ozcm4oet2epypgwy
> ipfs cid format -v=0 bafybeidtuvmy7ratlnfkuh5pkz4whjvqa3367bok43ozcm4oet2epypgwy
QmW85q9pmby55reZb9YrcLRr5L2gHcvTFGoSmjADCbhq1w

As you can see, you end up with the same CID for the folder.
:blush:

3 Likes

You could do this with ipfs object patch add-link using the empty folder CID as root, but the ipfs object subcommands have been deprecated. Anyway there are examples if you search the forums.

The way to do it now is the ipfs files way explained above.

ipfs object path add-link is a bit dangerous to work with which is why it’s been deprecated in favor of other commands such as those in ipfs files.

Basically, ipfs object worked on dag-pb data structures rather than UnixFS files and directories. This meant commands like add-link did not use sharded directories and could result in blocks of data that would not be transferrable over bitswap (and that even if they could be would’ve been sad for doing directory traversals).

@ylempereur DAG json that’s what I was looking for!! Thanks!!!

How would we write the JSON if we wanted both blocks in a single file rather than directory? The thing that is tripping me up is the block size array because it is not translated to a human readable form in the JSON. How would I encode it?

This is the spec:

1 Like