Storage

The Fleek Platform SDK allows you to interface with the storage service to store files in a decentralized manner. Our service supports IPFS as our main storage protocol, complemented by Arweave and Filecoin as a backup layer. This approach ensures a high-performing and highly available service. Filecoin acts as the default backup layer, but modifications can be implemented in the storage settings.

Methods

Here is a list of the available methods for the Fleek Platform SDK Storage Service:

Method                      Description
-----------------------------------------------------------------------------------------------------------------
uploadFile                  Upload a file to IPFS
uploadDirectory             Upload a directory to IPFS
uploadVirtualDirectory      Upload a virtual directory to IPFS
get                         Get a file by CID
getByFilename               Get a file by Filename
list                        List files
delete                      Delete a file by CID

UploadFile

The uploadFile is an asynchronous function designed to upload a file to Fleek Platform Storage Service.

Function signature

async ({ file, onUploadProgress }: UploadRawFileArgs): Promise<UploadPinResponse>

Parameters

The file parameter is an instance of IpfsFile, a type that represents the file to be uploaded.

type UploadRawFileArgs = {
  file: FileLike;
  // A callback or handler for the upload progress
  onUploadProgress?: (uploadProgress: UploadProgress) => void;
};
type UploadProgress = {
  loadedSize: number;
  totalSize: number;
};
type FileLike = {
  name: string;
  stream: () => ReadableStream;
};

Returns

type UploadPinResponse = {
  pin: {
    cid: string;
    size: number;
  };
  duplicate: boolean;
};

Usage example

import { FleekSdk, PersonalAccessTokenService } from '@fleek-platform/sdk/node';

// The Fleek SDK should be authenticated
// with a valid Project ID
const accessTokenService = new PersonalAccessTokenService({
  personalAccessToken: '<PAT>',
  projectId: '<PROJECT-ID>',
});

const fleekSdk = new FleekSdk({
  accessTokenService,
});

const result = await fleekSdk.storage().uploadFile({
  file,
  onUploadProgress,
});

const onUploadProgress = ({ loadedSize, totalSize }: UploadProgress) => {
  if (loadedSize > 0) {
    const currentTime = DateTime.now();
    const elapsedMillis = currentTime.diff(startedTime).as('milliseconds');

    const bytesPerSecond = loadedSize / (elapsedMillis / 1000);
    const remainingBytes = totalSize - loadedSize;
    const remainingSeconds = remainingBytes / bytesPerSecond;
    const remainingTime = remainingSeconds * 1000;

    // Does something with remainingTime
  }
};

list

The list is an asynchronous function designed to get a list of files uploaded to a projects storage.

Function signature

async (): Promise<StoragePin[]>

Parameters

Not required.

Returns

type StoragePin = {
  cid?: string;
  size?: number;
  filename?: string;
  filecoinDealIds?: string;
  arweavePin?: ArweavePin;
  arweaveId?: string;
};
type ArweavePin = {
  cid: string;
  bundlrId: string;
  price: runtime.Decimal;
  createdAt: Date;
};

Usage example

import { FleekSdk, PersonalAccessTokenService } from '@fleek-platform/sdk/node';

// The Fleek SDK should be authenticated
// with a valid Project ID
const accessTokenService = new PersonalAccessTokenService({
  personalAccessToken: '<PAT>',
  projectId: '<PROJECT-ID>',
});

const fleekSdk = new FleekSdk({
  accessTokenService,
});

const result = await fleekSdk.storage().list();

Get

The get is an asynchronous function to retrieve a file by it’s CID.

Function Signature

async ({ cid }: GetPinArgs): Promise<StoragePin>

Parameters

type GetPinArgs = {
  cid: string;
};

Returns

type StoragePin = {
  cid?: string;
  size?: number;
  filename?: string;
  filecoinDealIds?: string;
  arweavePin?: ArweavePin;
  arweaveId?: string;
};

Usage Example

import { FleekSdk, PersonalAccessTokenService } from '@fleek-platform/sdk/node';

// The Fleek SDK should be authenticated
// with a valid Project ID
const accessTokenService = new PersonalAccessTokenService({
  personalAccessToken: '<PAT>',
  projectId: '<PROJECT-ID>',
});

const fleekSdk = new FleekSdk({
  accessTokenService,
});

const res = await fleekSdk.storage().get({
  cid: '<CID>',
});

GetByFilename

The getByFilename is an asynchronous function to retrieve a file by it’s name and extension.

Function Signature

async ({ filename, extension }: GetPinByFilenameArgs): Promise<StoragePin[]>

Parameters

type GetPinByFilenameArgs = {
  filename: string;
  extension: string;
};

Returns

type StoragePin = {
  cid?: string;
  size?: number;
  filename?: string;
  filecoinDealIds?: string;
  arweavePin?: ArweavePin;
  arweaveId?: string;
}[];

Usage Example

import { FleekSdk, PersonalAccessTokenService } from '@fleek-platform/sdk/node';

// The Fleek SDK should be authenticated
// with a valid Project ID
const accessTokenService = new PersonalAccessTokenService({
  personalAccessToken: '<PAT>',
  projectId: '<PROJECT-ID>',
});

const fleekSdk = new FleekSdk({
  accessTokenService,
});

const res = await fleekSdk.storage().getByFilename({
  filename: '<FILENAME>',
  extension: '<EXTENSION>',
});

Delete

The delete is an asynchronous function designed to delete a storage file by it’s CID.

Function signature

async ({ cid }: DeletePinArgs)

Parameters

type DeletePinArgs = {
  cid: string;
};

Returns

type Response = {
  status: number;
  // TODO: provide type
  body: Record<string, unknown>;
};

Usage example

import { FleekSdk, PersonalAccessTokenService } from '@fleek-platform/sdk/node';

// The Fleek SDK should be authenticated
// with a valid Project ID
const accessTokenService = new PersonalAccessTokenService({
  personalAccessToken: '<PAT>',
  projectId: '<PROJECT-ID>',
});

const fleekSdk = new FleekSdk({
  accessTokenService,
});

const result = await fleekSdk.storage().delete();

UploadDirectory

Upload a directory to Fleek Platform Storage IPFS.

Function signature

async ({ path, options, onUploadProgress }: UploadDirectoryArgs): Promise<UploadPinResponse>

Parameters

type UploadDirectoryArgs = {
  path: string;
  options?: UploadDirectoryOptions;
  onUploadProgress?: (uploadProgress: UploadProgress) => void;
};

Returns

type UploadPinResponse = {
  pin: {
    cid: string;
    size: number;
  };
  duplicate: boolean;
};

UploadVirtualDirectory

Upload a virtual directory to Fleek Platform Storage IPFS.

Function signature

async ({
    files,
    directoryName,
    onUploadProgress,
  }: UploadVirtualDirectoryArgs): Promise<UploadPinResponse>

Parameters

type UploadVirtualDirectoryArgs = {
  files: FileLike[];
  directoryName: string;
  onUploadProgress?: (uploadProgress: UploadProgress) => void;
};
type FileLike = {
  name: string;
  stream: () => ReadableStream;
};

Returns

type UploadPinResponse = {
  pin: {
    cid: string;
    size: number;
  };
  duplicate: boolean;
};