How can attach cid to new node

cidA fie
cidB dir with sub cids

how can i make a new is

(new root cid)
- name1 = cidA
- dir2 = cidB

ipfs.files.write looks like not allow content is cid

Welcome bluelovers!

You can create a directory with ipfs files mkdir /directory

If you (or someone else) already got a file/directory somewhere you can ‘map’ it with ipfs files cp /ipfs/<CID> /directory/filename

This operation won’t copy any data, it just add a refer under a new name to the data already stored in the IPFS.

If you run such an operation on remote data, it won’t be stored on your node - this is called lazy copy. Only data you access will be fetched and stored from the directory/file.

This way you can ‘mount’ extremely large directory in an instant.

If you want to store the data locally you can pin it:

ipfs pin add /ipfs/<CID>

Note that unpinning it won’t have any effect on the ability to garbage collect the data again from your client, until you removed all references from the MFS, too.

1 Like

There is also ipfs object patch API which enables you to create arbitrary unixfs DAGs in a programmatic way.

Example:

To add a file QmbWqxBEKC3P8tqsKc98xmWNzrzDtRLMiMPL8wBuTGsMnR to an empty directory QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn under name guardian.jpg:

$ ipfs object patch add-link QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn guardian.jpg QmbWqxBEKC3P8tqsKc98xmWNzrzDtRLMiMPL8wBuTGsMn
QmdHY9TnXaXez2bSsQqanm2WaAineyiaFydwqqkEk6kyzs # CID of new root  with one file named guardian.jpg

@bluelovers in your case you want to do something like:

$ ipfs object patch add-link QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn name1 cidA
QmRoot1

$ ipfs object patch add-link QmRoot1 dir2 cidB
QmRoot2

QmRoot2 will be the CID of a directory with both name1 and dir2

2 Likes

And well, you could theoretically use pipe if you insist on writing a file with ipfs files write. This might make sense if you want to alter the data with e.g. sed or you want to use a different chunker/hash for the file.

ipfs cat /ipfs/<CID> | ipfs files write --create /folder/file

If you don’t want to alter the data, this is by far the slowest solution, since the data is pulled and pushed to ipfs, which needs to lookup the whole file content, and rehash it.

thx, looks like only can add one entry each time

i have get the new cid for next call is that will slow? or didn’t need care it

export function addSourceToTarget(source: {
	cid: ICIDValue,
	name: string,
}, target : {
	cid: ICIDValue,
}, ipfs: IIPFSPromiseApi)
{
	return ipfs.object.patch.addLink(target.cid.toString(), {
		name: source.name,
		cid: source.cid,
	})
}