Let's create a little directory to import:
> cd /tmp> mkdir foo> echo 'hello' > foo/bar> echo 'world' > foo/quux Copy
> cd /tmp> mkdir foo> echo 'hello' > foo/bar> echo 'world' > foo/quux
And write the importing logic:
import { importer } from 'ipfs-unixfs-importer'import { MemoryBlockstore } from 'blockstore-core/memory'import * as fs from 'node:fs'// Where the blocks will be storedconst blockstore = new MemoryBlockstore()// Import path /tmp/foo/const source = [{ path: '/tmp/foo/bar', content: fs.createReadStream('/tmp/foo/bar')}, { path: '/tmp/foo/quxx', content: fs.createReadStream('/tmp/foo/quux')}]for await (const entry of importer(source, blockstore)) { console.info(entry)} Copy
import { importer } from 'ipfs-unixfs-importer'import { MemoryBlockstore } from 'blockstore-core/memory'import * as fs from 'node:fs'// Where the blocks will be storedconst blockstore = new MemoryBlockstore()// Import path /tmp/foo/const source = [{ path: '/tmp/foo/bar', content: fs.createReadStream('/tmp/foo/bar')}, { path: '/tmp/foo/quxx', content: fs.createReadStream('/tmp/foo/quux')}]for await (const entry of importer(source, blockstore)) { console.info(entry)}
When run, metadata about DAGNodes in the created tree is printed until the root:
{ cid: CID, // see https://github.com/multiformats/js-cid path: 'tmp/foo/bar', unixfs: UnixFS // see https://github.com/ipfs/js-ipfs-unixfs}{ cid: CID, // see https://github.com/multiformats/js-cid path: 'tmp/foo/quxx', unixfs: UnixFS // see https://github.com/ipfs/js-ipfs-unixfs}{ cid: CID, // see https://github.com/multiformats/js-cid path: 'tmp/foo', unixfs: UnixFS // see https://github.com/ipfs/js-ipfs-unixfs}{ cid: CID, // see https://github.com/multiformats/js-cid path: 'tmp', unixfs: UnixFS // see https://github.com/ipfs/js-ipfs-unixfs} Copy
{ cid: CID, // see https://github.com/multiformats/js-cid path: 'tmp/foo/bar', unixfs: UnixFS // see https://github.com/ipfs/js-ipfs-unixfs}{ cid: CID, // see https://github.com/multiformats/js-cid path: 'tmp/foo/quxx', unixfs: UnixFS // see https://github.com/ipfs/js-ipfs-unixfs}{ cid: CID, // see https://github.com/multiformats/js-cid path: 'tmp/foo', unixfs: UnixFS // see https://github.com/ipfs/js-ipfs-unixfs}{ cid: CID, // see https://github.com/multiformats/js-cid path: 'tmp', unixfs: UnixFS // see https://github.com/ipfs/js-ipfs-unixfs}
Example
Let's create a little directory to import:
And write the importing logic:
When run, metadata about DAGNodes in the created tree is printed until the root: