The Filesystem module provides full file I/O within a sandbox. Access it via sandbox.files.
Reading files
// Read as text
const content = await sandbox.files.read('/app/config.json');
const config = JSON.parse(content);
// Read as bytes
const bytes = await sandbox.files.readBytes('/app/image.png');
console.log(`File size: ${bytes.length} bytes`);
sandbox.files.read(path)
Reads a file as a UTF-8 string.
Absolute path to the file.
Returns: Promise<string>
sandbox.files.readBytes(path)
Reads a file as raw bytes.
Absolute path to the file.
Returns: Promise<Uint8Array>
Writing files
// Write text
await sandbox.files.write('/app/hello.txt', 'Hello, World!');
// Write binary
const encoder = new TextEncoder();
await sandbox.files.write('/app/data.bin', encoder.encode('binary data'));
sandbox.files.write(path, content)
Writes a string or binary content to a file. Creates parent directories if needed.
Absolute path to the file.
content
string | Uint8Array
required
File content to write.
Returns: Promise<void>
Listing directories
const entries = await sandbox.files.list('/app');
for (const entry of entries) {
console.log(`${entry.isDir ? 'DIR ' : 'FILE'} ${entry.name} (${entry.size} bytes)`);
}
sandbox.files.list(path?)
Lists the contents of a directory.
Returns: Promise<EntryInfo[]>
EntryInfo
| Field | Type | Description |
|---|
name | string | File or directory name |
isDir | boolean | true if this entry is a directory |
path | string | Full path to the entry |
size | number | Size in bytes (0 for directories) |
Managing files and directories
sandbox.files.makeDir(path)
Creates a directory and any necessary parent directories.
await sandbox.files.makeDir('/app/src/components');
sandbox.files.remove(path)
Deletes a file or directory.
await sandbox.files.remove('/app/temp.txt');
sandbox.files.exists(path)
Checks whether a file or directory exists.
if (await sandbox.files.exists('/app/package.json')) {
console.log('Node.js project detected');
}
Examples
Upload and run a script
await sandbox.files.write('/tmp/script.sh', `#!/bin/bash
echo "Running setup..."
apt-get update -qq
echo "Done!"
`);
const result = await sandbox.exec.run('bash /tmp/script.sh');
console.log(result.stdout);
Copy files between paths
const content = await sandbox.files.readBytes('/app/original.dat');
await sandbox.files.write('/backup/original.dat', content);