DFS Interface¶
Interface Name | DfsInterface |
---|---|
Source | dfs-interface.ts |
The DfsInterface is used to add or get files from the distributed file system. It is the only class, that has to be used before having access to a runtime, when using the createDefaultRuntime.
Internally the modules use the DfsInterface to access data as well. As the actual implementation of the file access may vary, an instance of the interface has to be created beforehand and passed to the createDefaultRuntime function. An implementation called Ipfs, that relies on the IPFS framework is included as in the package.
add¶
dfs.add(name, data);
add content to dfs file content is converted to Buffer (in NodeJS) or an equivalent “polyfill” (in browsers)
Parameters¶
name
-string
: name of the added filedata
-buffer
: data (as buffer) of the added file
Returns¶
string
: hash of the data.
Example¶
const fileHash = await runtime.dfs.add(
'about-maika-1.txt',
Buffer.from('we have a cat called "Maika"', 'utf-8'),
);
console.log(fileHash);
// Output:
// 0x695adc2137f1f069ff697aa287d0eae486521925a23482f180b3ae4e6dbf8d70
addMultiple¶
dfs.addMultiple(files);
Multiple files can be added at once. This way of adding should be preferred for performance reasons, when adding files, as requests are combined.
Parameters¶
files
-FileToAdd[]
: array with files to add
Returns¶
Promise
resolves to string[]
: hash array of the data.
Example¶
const fileHashes = await runtime.dfs.addMultiple([{
path: 'about-maika-1.txt',
content: Buffer.from('we have a cat called "Maika"', 'utf-8'),
}, {
path: 'about-maika-2.txt',
content: Buffer.from('she can be grumpy from time to time"', 'utf-8'),
}
]);
console.dir(fileHashes);
// Output:
// [ '0x695adc2137f1f069ff697aa287d0eae486521925a23482f180b3ae4e6dbf8d70',
// '0x6b85c8b24b59b12a630141143c05bbf40a8adc56a8753af4aa41ebacf108b2e7' ]
get¶
dfs.get(hash, returnBuffer);
get data from dfs by hash
Parameters¶
hash
-string
: hash (or bytes32 encoded) of the datareturnBuffer
-bool
: should the function return the plain buffer, defaults tofalse
Returns¶
Promise
resolves to string | buffer
: data as text or buffer.
Example¶
const fileBuffer = await runtime.dfs.get('0x695adc2137f1f069ff697aa287d0eae486521925a23482f180b3ae4e6dbf8d70');
console.log(fileBuffer.toString('utf-8'));
// Output:
// we have a cat called "Maika"
remove¶
dfs.remove(hash);
removes data by hash reference
Parameters¶
hash
-string
: hash (or bytes32 encoded) of the data
Returns¶
Promise
resolves to void
: resolved when done
Example¶
const fileBuffer = await runtime.dfs.remove('0x695adc2137f1f069ff697aa287d0eae486521925a23482f180b3ae4e6dbf8d70');