Skip to main content

File Storage Agent - JS/TS

Read and write an external file server.

Logic TypeAvailable
Generic logic
Aggregator logic

File Storage Agent Configuration

See: Agent Configuration

A File Storage Agent Configuration defines an external file server that will be allowed to be accessed from LOC runtime.

The File Storage Agent requires a configuration reference name so that it can access files on the file server. The reference name can be added to a logic while creating or editing a data process.

Import and Usage

import {
FileStorageAgent,
} from "@fstnetwork/loc-logic-sdk";

export async function run(ctx) {
const fileClient = await FileStorageAgent.acquire("file-config-ref");
const file = await fileClient?.simpleGet("dir/file.txt");
}

export async function handleError(ctx, error) {
// ... same
}

Class Reference

Type

  • FileStorageAgent

Method: Acquire File Storage Client

async acquire(configurationName: string): Promise<FileStorageAgentClient>
ParameterDescription
configurationNameFile Storage Agent Configuration reference name

Acquire a File Storage Client using a configuration reference name. Throws an error if the configuration cannot be found.

File Storage Client

Type

  • FileStorageAgentClient

Importable from @fstnetwork/loc-logic-sdk

Method: Read File

async simpleGet(path: string): Promise<Uint8Array>
ParameterTypeDescription
pathstringFile name and path (no / prefix)

Read a file from the file server. Returns a Uint8Array array, which can be converted to string using Web API TextDecoder.

Method: Write File

async simplePut(path: string, data: Uint8Array | string, options?: FileStorage.PutOptions): Promise<number>

Write a file to the file server.

ParameterTypeDescription
pathstringFile name and path (no / prefix)
dataUint8Array | stringFile content
options?FileStorage.PutOptionsOptional; for SMB (Server Message Block) storages to ensure the directory exists

FileStorage.PutOptions

{
ensureDir?: boolean;
}

Method: Delete File

async delete(path: string): Promise<void>
ParameterTypeDescription
pathstringFile name and path (no / prefix)

Delete a file from the file server.

Method: List Directories and Files

async list(path: string): Promise<Array<FileStorage.FileType>>
ParameterTypeDescription
pathstringFile name and path (no / prefix)

List directories and files at a given path on the file server.

Returns an array of type FileStorage.FileType.

FileStorage.FileType

FieldsTypeDescription
namestringItem name
type"file" | "directory" | "symbolicLink"Item type

The type indicates that an item is a file, a directory or a symbolic link (in SMB).

Method: Create Directory

async createDirAll(path: string): Promise<void>
ParameterTypeDescription
pathstringDirectory path (no / prefix)

Create a directory at a given path on the file server.

warning

Not supported on Amazon S3 storages.

Examples

Read a File

const fileClient = await FileStorageAgent.acquire("file-config-ref");

const file = await fileClient?.simpleGet("dir/file.txt");

const data = new TextDecoder().decode(file);

Write a File

const data = `The chances of finding out what really is going on
are so absurdly remote that the only thing to do is
to say hang the sense of it and keep yourself occupied`;

const fileClient = await FileStorageAgent.acquire("file-config-ref");

await fileClient?.simplePut("dir/file.txt", data);

List Directories and Files

const fileClient = await FileStorageAgent.acquire("file-config-ref");

const fileList = await fileClient?.list("dir");

// iterate through all items in the list
fileList.forEach((item) => {
const name = item.name;
const type = item.type;
// ...
});