|
| 1 | +# IPFS file system |
| 2 | + |
| 3 | +The official name is [UnixFS](https://docs.ipfs.io/guides/concepts/unixfs/). It allows files and directories of any size |
| 4 | +to be added and retrieved from IPFS via the [FileSystem](xref:Ipfs.CoreApi.IFileSystemApi) |
| 5 | +and [Object](xref:Ipfs.CoreApi.IObjectApi) API. |
| 6 | + |
| 7 | +## Files |
| 8 | + |
| 9 | +A file has a unique [content id (CID)](xref:Ipfs.Cid) which is the cryptographic hash of the content; see |
| 10 | +[CID concept](https://docs.ipfs.io/guides/concepts/cid/) for background information. The file's content is not just the file's |
| 11 | +data but is encapsulated with a [protocol buffer](https://en.wikipedia.org/wiki/Protocol_Buffers) encoding of the |
| 12 | +[PBNode](https://github.com/ipfs/go-ipfs/blob/0cb22ccf359e05fb5b55a9bf2f9c515bf7d4dba7/merkledag/pb/merkledag.proto#L31-L39) |
| 13 | +and [UnixFS Data](https://github.com/ipfs/go-ipfs/blob/0cb22ccf359e05fb5b55a9bf2f9c515bf7d4dba7/unixfs/pb/unixfs.proto#L3-L20). |
| 14 | + |
| 15 | +Where |
| 16 | +- `PBNode.Data` contains unixfs message Data |
| 17 | +- unixfs `Data.Data` contans file's data |
| 18 | + |
| 19 | +When the file's data exceeds the [chunking size](xref:Ipfs.CoreApi.AddFileOptions.ChunkSize), multiple [blocks](xref:Ipfs.CoreApi.IBlockApi) |
| 20 | +are generated. The returned CID points to a block that has `PBNode.Links` and no `PBNode.Data`. |
| 21 | + |
| 22 | +### Adding a file |
| 23 | + |
| 24 | +[AddAsync](xref:Ipfs.CoreApi.IFileSystemApi.AddAsync*) is used to add a stream of data to IPFS. It returns a |
| 25 | +[FileSystemNode](xref:Ipfs.IFileSystemNode) which |
| 26 | +describes the added the data. Of particular import is its [CID](xref:Ipfs.IDataBlock.Id). The helper methods |
| 27 | +[AddTextAsync](xref:Ipfs.CoreApi.IFileSystemApi.AddTextAsync*) and [AddFileAsync](xref:Ipfs.CoreApi.IFileSystemApi.AddFileAsync*) are also available. |
| 28 | + |
| 29 | +All the Add methods accept [options](xref:Ipfs.CoreApi.AddFileOptions) to control how the data is added to IPFS. |
| 30 | + |
| 31 | +```csharp |
| 32 | +var fsn = await ipfs.FileSystem.AddTextAsync("hello world"); |
| 33 | +Console.WriteLine((string)fsn.Id) |
| 34 | + |
| 35 | +// Qmf412jQZiuVUtdgnB36FXFX7xg5V6KEbSJ4dpQuhkLyfD |
| 36 | +``` |
| 37 | + |
| 38 | +### Reading a file |
| 39 | + |
| 40 | +[ReaFileAsync](xref:Ipfs.CoreApi.IFileSystemApi.ReadFileAsync*) is used to read a stream of data from IPFS. It returns a |
| 41 | +[Stream](xref:System.IO.Stream) containing just the file's data NOT the protocol buffer encoded data. |
| 42 | + |
| 43 | +```csharp |
| 44 | +string path = "Qmf412jQZiuVUtdgnB36FXFX7xg5V6KEbSJ4dpQuhkLyfD"; |
| 45 | +using (var stream = await ipfs.FileSystem.ReadFileAsyc(path)) |
| 46 | +{ |
| 47 | + // Do something with the data |
| 48 | +} |
| 49 | +``` |
| 50 | + |
| 51 | +### Getting a CID |
| 52 | + |
| 53 | +Normally, you get the CID by [adding](xref:Ipfs.CoreApi.IFileSystemApi.AddAsync*) the file to IPFS. You can avoid adding it |
| 54 | +to IPFS by using the [OnlyHash option](xref:Ipfs.CoreApi.AddFileOptions.OnlyHash). |
| 55 | + |
| 56 | +```csharp |
| 57 | +var options = new AddFileOptions { OnlyHash = true }; |
| 58 | +var fsn = await ipfs.FileSystem.AddTextAsync("hello world", options); |
| 59 | +Console.WriteLine((string)fsn.Id) |
| 60 | + |
| 61 | +// Qmf412jQZiuVUtdgnB36FXFX7xg5V6KEbSJ4dpQuhkLyfD |
| 62 | +``` |
| 63 | + |
0 commit comments