A simple file system interface for managing files in sandboxes.
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.
The Blaxel SDK authenticates with your workspace using credentials from these sources, in priority order:
when running on Blaxel, authentication is handled automatically
variables in your .env file (BL_WORKSPACE and BL_API_KEY, or see this page for other authentication options).
environment variables from your machine
configuration file created locally when you log in through Blaxel CLI (or deploy on Blaxel)
When developing locally, the recommended method is to just log in to your workspace with Blaxel CLI. This allows you to run Blaxel SDK functions that will automatically connect to your workspace without additional setup. When you deploy on Blaxel, this connection persists automatically.
When running Blaxel SDK from a remote server that is not Blaxel-hosted, we recommend using environment variables as described in the third option above.
You can write multiple files or directories simultaneously. The second path parameter in writeTree specifies the base directory for writing the file tree, eliminating the need to repeat the full path for each file.
The watch function monitors all file system changes in the specified directory. You can also watch subdirectories by passing a /my/directory/** pattern.
By default (whenΒ withContent: false), the events will only include metadata about the changes, not the actual file contents. Hereβs what youβllΒ get in the callbackΒ events:
For ALL operations (CREATE, WRITE, DELETE, etc.), youβll receive:
op: TheΒ operation type (e.g., βCREATEβ, βWRITEβ, βDELETEβ)
path: The directory path whereΒ the change occurred
name: The name of the file/directory thatΒ changed
You will NOT receive:
The actual content of the files
File contents for CREATE or WRITE operations
Copy
Ask AI
import { SandboxInstance } from "@blaxel/core";// Test the default watch functionality:async function testWatch(sandbox: SandboxInstance) { try { const user = process.env.USER; const testDir = `/Users/${user}/Downloads/watchtest`; const testFile = `/file.txt`; // Ensure correct type for fs const fs = sandbox.fs; // Clean up before test try { await fs.rm(testDir, true); } catch {} await fs.mkdir(testDir); // Watch without content const events: string[] = [] const contents: string[] = [] const handle = fs.watch("/", (fileEvent) => { events.push(fileEvent.op) if (fileEvent.op === "WRITE") { contents.push(fileEvent.content ?? "") } }, { withContent: true }); await new Promise((resolve) => setTimeout(resolve, 100)); await fs.write(testFile, "content"); await new Promise((resolve) => setTimeout(resolve, 100)); await fs.write(testFile, "new content"); await new Promise((resolve) => setTimeout(resolve, 100)); await fs.rm(testFile) await new Promise((resolve) => setTimeout(resolve, 100)); handle.close(); // Clean up after test await fs.rm(testDir, true); if (!events.includes("CREATE") || !events.includes("WRITE") || !events.includes("REMOVE")) { throw new Error("Watch callback not consistent with expected events: " + events.join(", ")); } if (!contents.includes("content") || !contents.includes("new content")) { throw new Error("Watch callback not consistent with expected contents: " + contents.join(", ")); } console.log("testWatch passed"); } catch (e) { console.error("There was an error => ", e); }}async function main() { try { const sandbox = await SandboxInstance.get("my-sandbox") await testWatch(sandbox) } catch (e) { console.error("There was an error => ", e); }}main() .catch((err) => { console.error("There was an error => ", err); process.exit(1); }) .then(() => { process.exit(0); })