Work with Filehub
Filehub allows users to store files on-chain and retrieve them using the Filehub NPM package. A Gateway is also available for easy file access, supporting various use cases such as images, videos, and static websites.
Filehub client
Fetching a file
To retrieve a file stored in Filehub, use the following code:
const { Filehub } = require("filehub");
const filehub = new Filehub({
directoryNodeUrlPool: DIRECTORY_NODE_URL_POOL,
blockchainRid: FILEHUB_BLOCKCHAIN_RID,
});
const file = await filehub.getFile(fileHash);Storing a file
To store a file in Filehub, utilize the following example:
const { Filehub, FsFile } = require("filehub");
const filehub = new Filehub({
directoryNodeUrlPool: DIRECTORY_NODE_URL_POOL,
blockchainRid: FILEHUB_BLOCKCHAIN_RID,
});
const file = FsFile.fromData(buffer, { contentType: "image/jpeg" });
await filehub.storeFile(ft4Session, file);Payment model
Filehub operates on a one-time payment model, charging users $0.10 per megabyte for permanent file storage. Unlike traditional storage solutions that may rely on random hard drives with uncertain longevity, Filehub ensures that files are fully stored on-chain, providing a guarantee of long-term accessibility.
Filehub APIs
Filehub offers a robust API for managing Filechains, facilitating file storage, and configuring payments. Key operations include:
- Registering a Filechain: Assign a new Filechain with administrative privileges.
- Enabling/Disabling Filechains: Control the availability of Filechains based on storage requirements.
- Managing Payments: Enable or disable payments for file storage and configure the assets used for transactions.
Example
Here’s how to register a new Filechain using the Filehub Administrator API:
const filehub = new Filehub({
directoryNodeUrlPool: DIRECTORY_NODE_URL_POOL,
blockchainRid: FILEHUB_BLOCKCHAIN_RID,
});
const admin = newSignatureProvider({ privKey: FILEHUB_ADMIN_PRIVKEY });
const filehubAdministrator = new FilehubAdministrator(filehub, admin);
// Register a Filechain
await filehubAdministrator.registerFilechain(FILECHAIN_BLOCKCHAIN_RID, FILECHAIN_ADMIN_EVM_ADDRESS);