The Koii Storage Client is a JavaScript library that provides functionality for interacting with Koii Storage, allowing users to upload and retrieve files to and from the Koii network.
You can install the Koii Storage Client library using npm:
npm i @_koii/storage-task-sdk
To use the Koii Storage Client in your project, import it into your code:
import {KoiiStorageClient} from '@_koii/storage-task-sdk';
Then, create an instance of the KoiiStorageClient class and use its methods to upload and retrieve files from Koii Storage:
const storageClient = KoiiStorageClient.getInstance(options);
// Upload a file
const fileUploadResponse = await storageClient.uploadFile('path/to/file');
// Retrieve a file
const fileBlob = await storageClient.getFile(fileUploadResponse.cid, 'filename');
You can instantiate the KoiiStorageClient class by providing optional parameters:
- storageTaskId: The ID of the storage task. Defaults to a default task ID if not provided.
- k2URL: The URL of the K2 network. Defaults to the testnet URL if not provided.
- debug: Boolean flag indicating whether to enable debugging mode. Defaults to false.
const storageClient = new KoiiStorageClient(storageTaskId?, k2URL?, debug?);
uploadFile(file: string | File | File[]): Promise
Uploads a file or an array of files to Koii Storage.
-
file
: The file(s) to upload. Can be a file path (string), a File object, or an array of File objects.
Returns a promise that resolves to a FileUploadResponse
object containing information about the uploaded file(s).
getFile(cid: string, filename: string): Promise
Retrieves a file from Koii Storage.
-
cid
: The content identifier (CID) of the file to retrieve. -
filename
: The name of the file to retrieve.
Returns a promise that resolves to a Blob object containing the retrieved file data.
saveBlobAsFile(blob: Blob, filename: string): Promise
Saves a Blob object as a file on the local filesystem.
-
blob
: The Blob object to save as a file. -
filename
: The name of the file to save.
Returns a promise that resolves when the file is successfully saved.
To get text from the Blob object, you can use the text()
method like this:
const fileContent = await getFile(cid, filename);
console.log(await fileContent.text());
import KoiiStorageClient from 'koii-storage-client';
const storageClient = new KoiiStorageClient();
async function uploadAndRetrieveFile() {
// Upload a file
const fileUploadResponse = await storageClient.uploadFile('path/to/file');
// Retrieve the uploaded file
const fileBlob = await storageClient.getFile(fileUploadResponse.cid, 'filename');
// Save the retrieved file locally
await storageClient.saveBlobAsFile(fileBlob, 'retrieved-file');
// Incase the retrieved blob is a text/json file
// Get text from the retrieved file
const fileContent = await fileBlob.text();
}
uploadAndRetrieveFile();