fs module of Blaxel SDK. This module provides essential operations for creating, reading, writing, copying, and deleting files and directories.
Basic file system operations
Learn more about authentication on Blaxel
Learn more about authentication on Blaxel
The Blaxel SDK requires two environment variables to authenticate:The Blaxel SDK does not accept credentials as constructor arguments. Credentials must come from environment variables, a
You can create an API key from the Blaxel console. Your workspace name is visible in the URL when you log in to the console (e.g.
app.blaxel.ai/{workspace}).Set them as environment variables or add them to a .env file at the root of your project:.env file, or a local CLI login session (see below).When developing locally, you can also log in to your workspace with Blaxel CLI (as shown above). This allows you to run Blaxel SDK functions that will automatically connect to your workspace without additional setup. When you deploy on Blaxel, authentication is handled automatically — no environment variables needed.Create directory
Create a new directory at a specific path in the sandbox:List files
List files in a specific path:Address file paths over HTTP
Filesystem paths are part of the URL when you call the sandbox API directly over HTTP. The API resolves a path without a leading slash relative to the filesystem service’s working directory. To address an absolute path, URL-encode the leading slash as%2F.
For example, if the filesystem working directory is /app:
The CLI’s double-slash form, such as
--path /filesystem//workspace, is the CLI equivalent for an absolute path. For a direct HTTP request, use %2F for the leading slash:
/workspace/archive.tar, uploads one part, and completes the upload. Repeat the part request with increasing partNumber values for additional parts, then include every returned etag in the completion request.
/workspace.
Find files and directories
Blaxel’s Sandbox API uses an optimized
find() method, which is faster than using the native find tool.Search for text content within files
Blaxel’s Sandbox API uses an optimized
grep() method, which is faster than using the native grep tool.Read file
Read a file from a specific filepath:Read many files
Discover relative file paths withfs.find, then read text files in batches of four. This returns flat path/content pairs with one discovery call plus one read call per file, not a single bulk-read request.
find defaults to 20 results, so do not assume its default response includes every file. Request one more than your chosen file cap and reject the result if that extra match is returned. excludeDirs (exclude_dirs in Python) matches directory names, not path globs; apply file exclusions by filtering paths separately.
Export a whole directory as one archive
Create an archive on disk withtar, then download it with one binary read. This uses two calls for creation and download, plus a cleanup call. Your sandbox image must include tar. Keep the temporary archive outside the directory you are exporting.
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.
Read binary file
Read a binary file from the sandbox filesystem:Write binary file
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
If you’re calling the sandbox filesystem API directly over HTTP instead of using an SDK, a single request is capped at 5 MB. Requests larger than this are rejected with an HTTP 413 before they reach the sandbox. Therefore, if using HTTP, larger files must be uploaded in parts via the sandbox’s multipart upload endpoints rather than in a single request. Blaxel SDKs handle this automatically: SDK functions like
write(), writeBinary(), and write_binary() automatically split files over 5 MB and send via multipart upload in 5 MB chunks.Download file to host
Download a file from the sandbox filesystem to the host: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.