@_koii/storage-task-sdk
TypeScript icon, indicating that this package has built-in type declarations

1.2.7 • Public • Published

Koii Storage Client

Introduction

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.

Installation

You can install the Koii Storage Client library using npm:

npm i @_koii/storage-task-sdk

Usage

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

Constuctor

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

Methods

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

Example

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

Readme

Keywords

none

Package Sidebar

Install

npm i @_koii/storage-task-sdk

Weekly Downloads

18

Version

1.2.7

License

MIT

Unpacked Size

34.6 kB

Total Files

15

Last publish

Collaborators

  • sid09
  • syedghazanfer
  • muscular_developer
  • npm-koii
  • josh-koii