IPFS file system
The official name is UnixFS. It allows files and directories of any size to be added and retrieved from IPFS via the FileSystem and Object API.
Files
A file has a unique content id (CID) which is the cryptographic hash of the content; see CID concept for background information. The file's content is not just the file's data but is encapsulated with a protocol buffer encoding of the PBNode and UnixFS Data.
Where
PBNode.Data
contains unixfs message Data- unixfs
Data.Data
contans file's data
When the file's data exceeds the chunking size, multiple blocks
are generated. The returned CID points to a block that has PBNode.Links
and no PBNode.Data
.
Adding a file
AddAsync is used to add a stream of data to IPFS. It returns a FileSystemNode which describes the added the data. Of particular import is its CID. The helper methods AddTextAsync and AddFileAsync are also available.
All the Add methods accept options to control how the data is added to IPFS.
var fsn = await ipfs.FileSystem.AddTextAsync("hello world");
Console.WriteLine((string)fsn.Id)
// Qmf412jQZiuVUtdgnB36FXFX7xg5V6KEbSJ4dpQuhkLyfD
Reading a file
ReaFileAsync is used to read a stream of data from IPFS. It returns a Stream containing just the file's data NOT the protocol buffer encoded data.
string path = "Qmf412jQZiuVUtdgnB36FXFX7xg5V6KEbSJ4dpQuhkLyfD";
using (var stream = await ipfs.FileSystem.ReadFileAsyc(path))
{
// Do something with the data
}
Getting a CID
Normally, you get the CID by adding the file to IPFS. You can avoid adding it to IPFS by using the OnlyHash option.
var options = new AddFileOptions { OnlyHash = true };
var fsn = await ipfs.FileSystem.AddTextAsync("hello world", options);
Console.WriteLine((string)fsn.Id)
// Qmf412jQZiuVUtdgnB36FXFX7xg5V6KEbSJ4dpQuhkLyfD