Skip to main content

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.

note

Currently, Filehub operates on a general-purpose cluster, which is not specifically optimized for storage needs. As a result, the available storage capacity is somewhat limited. The initial release of Filehub aims to support projects on Chromia while providing early adopters an opportunity to explore its capabilities and potential applications. Looking ahead, we anticipate the introduction of dedicated storage-optimized clusters on the mainnet, which would enable us to significantly reduce storage costs for users.

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);