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
Set up authentication to Blaxel
Set up authentication to Blaxel
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
.envfile (BL_WORKSPACEandBL_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)
Create directory
Create a new directory at a specific path in the sandbox:List files
List files in a specific path:Read file
Read a file from a specific filepath:Write file
Create a file in a specific path:Write multiple files
You can write multiple files or directories simultaneously. The second path parameter inwriteTree specifies the base directory for writing the file tree, eliminating the need to repeat the full path for each file.
Write binary
Write binary content to a file in the sandbox filesystem:- Buffer: Node.js Buffer object
- Blob: Web API Blob object
- File: Web API File object
- Uint8Array: Typed array containing binary data
Copy file
Copy a file from a path to another path:Delete file or directory
Delete a file or directory by specifying its path:Watch filesystem for events
Thewatch 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
Watch sub-directories
Watch all sub-directories recursively with/**:
Ignore files or directories
You can ignore changes in certain files or directories by providing an array of filepaths to ignore:withContent: true so the events include the actual file contents.