Skip to main content
Version: LOC v0.10 (legacy)

File Storage Agent

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

Accessing and writing files. Supports the following storages:

  • FTP (File Transfer Protocol)
  • Amazon S3
  • SMB (Server Message Block)

Availability

  • ✓ Generic logic
  • ✗ Aggregator logic
info

This agent requires agent configuration to work in a data process.

Acquire File Client

async FileStorageAgent.acquire(configurationName: string): Promise<FileStorageAgentClient>

Returns a FileStorageAgentClient object based on provided agent configuration name, which connects to a pre-defined file storage with confidential information (if needed).

The configuration name is the reference field set in Studio or name field set in CLI config files.

Throws an error if the configuration cannot be found.

Example

const fileClient = await FileStorageAgent.acquire("my-file-configuration");

File Storage Client

Read from File Storage

async fileClient.simpleGet(path: string): Promise<Uint8Array>

Read a remote file.

ParameterTypeDescription
pathstringFile path (no / prefix)

Example

const path = "dir/test.txt";

const fileClient = await FileStorageAgent.acquire("my-file-configuration");

// get file
const file = await fileClient?.simpleGet(path);

// decode file to string
const data = new TextDecoder().decode(file);

Write to File Storage

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

Write a file to remote storage.

ParameterTypeDescription
pathstringFile path (no / prefix)
dataUint8Array | stringFile content
optionsFileStorage.PutOptions, which is { ensureDir?: boolean; }(Optional) For SMB

Example

// data
const path = "dir/test.txt";

const data = `Hello World!
This is line two`;

// write to file storage
await fileClient?.simplePut(path, data);

Example using options

await fileClient.simplePut(path, data, { ensureDir: true });

Delete File

async fileClient.delete(path: string): Promise<void>

Delete a file from file storage.

Create Directory

async fileClient.createDirAll(path: string): Promise<void>

Create a directory at a remote path. Not supported on Amazon S3 storages.

List Directories and Files

async fileClient.list(path: string): Promise<Array<FileStorage.FileType>>

List directories and files at a remote path. Not supported on Amazon S3 storages.

Returns an array of type FileStorage.FileType. A FileType object has two fields:

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

Example

const fileList = await fileClient?.list(path);

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