Skip to main content

IPFS

The IPFS module allows you to interact with the IPFS protocol from the Alfa sdk.

MFS - Mutable File System

The ipfs module implements the IPFS MFS, which allows you to interact with the IPFS file system in a similar way to an operating system file system.

Methods

Get a file from IPFS

The get method allows you to get a file from IPFS.

const ipfs = sdk.ipfs;

const ipfsHash = new IPFSHash('QmWJzrpSQmZySFkyMu6faFRsQnAVHHZjsYcbpEg6ZM7N56')

const result = await ipfs.get(ipfsHash);

Getting a file from a directory in IPFS

The getPath method allows you to get a file from an ipfs directory.

const ipfs = sdk.ipfs;

const ipfsPath = new IPFSPath('/myfolder/QmWJzrpSQmZySFkyMu6faFRsQnAVHHZjsYcbpEg6ZM7N56')

const result = await ipfs.getPath(ipfsPath);

Pin a file or directory to IPFS

The pin method allows you to pin a file or directory to IPFS.

const ipfs = sdk.ipfs;

const content = new Blob(["Hello world"]);

const result = await ipfs.pin({ content }, { filename: "helloWord.txt" });
Content

The content is of type Content and can be a string, Blob, ArrayBuffer, Uint8Array, Iterable<Uint8Array>.

Read a file from MFS

The read method allows you to read a file from MFS.

const ipfs = sdk.ipfs;

const path = new MFSPath('/images/hello.jpg')

const result = await ipfs.read(path);

List files from a directory in MFS

The ls method allows you to list the files in a directory in MFS.

const ipfs = sdk.ipfs;

const path = new MFSPath('/images')

const result = await ipfs.ls(path);

Write a file to MFS

The write method allows you to write a file to MFS.

const ipfs = sdk.ipfs;

const path = new MFSPath("/images");

const file = new File([], "hello.jpg");

const result = await ipfs.write(path, file);

Delete a file or directory from MFS

The rm method allows you to remove a file or directory from MFS.

const ipfs = sdk.ipfs;

const path = new MFSPath("/images/hello.jpg");

const result = await ipfs.rm(path);

Create a directory in MFS

The mkdir method allows you to create a directory on MFS.

const ipfs = sdk.ipfs;

const path = new MFSPath("/myFiles");

const result = await ipfs.mkdir(path);

Get information from a file or directory in MFS

The stat method allows you to get information about a file or directory in MFS.

const path = new MFSPath("/images");

const result = await ipfs.stat(path);

Move a file or directory in MFS

The mv method allows you to move a file or directory in MFS.

const ipfs = sdk.ipfs;

const from = new MFSPath("/images/hello.jpg");

const to = new MFSPath('/myFiles/')

const result = await ipfs.mv(from, to);

Copy a file or directory in MFS

The cp method allows you to copy a file or directory into MFS.

const ipfs = sdk.ipfs;

const from = new MFSPath("/images/hello.jpg");

const to = new MFSPath('/myFiles/copyHello.jpg')

const result = await ipfs.cp(from, to);