Manage files and directories within sandboxes through the fs module of Blaxel SDK. This module provides essential operations for creating, reading, writing, copying, and deleting files and directories.

Complete code examples demonstrating all operations are available on Blaxel’s GitHub: in TypeScript and in Python.

Basic file system operations

Create directory

Create a new directory at a specific path in the sandbox:


import { SandboxInstance } from "@blaxel/core";

const sandbox = await SandboxInstance.get("my-sandbox")

await sandbox.fs.mkdir(`/Users/user/Downloads/test`)

List files

List files in a specific path:


const dir = await sandbox.fs.ls(`/Users/user/Downloads`);

if (dir.files?.length && dir.files?.length < 1) {
  throw new Error("Directory is empty");
}

Read file

Read a file from a specific filepath:


const content = await sandbox.fs.read(`/Users/user/Downloads/test.txt`)

Write file

Create a file in a specific path:


await sandbox.fs.write(`/Users/user/Downloads/test.txt`, "Hello world");

Copy file

Copy a file from a path to another path:


await sandbox.fs.cp(`/Users/user/Downloads/test.txt`, `/Users/user/Documents/private/test.txt`);

Delete file or directory

Delete a file or directory by specifying its path:


await sandbox.fs.rm(`/Users/user/Downloads/test.txt`);