Filesystem
ComputeSDK provides filesystem operations for managing files and directories within sandboxes. All filesystem operations are accessed through the sandbox.filesystem object.
Quick Start
Section titled “Quick Start”import { createCompute } from 'computesdk'
import { e2b } from '@computesdk/e2b'
// Create a compute instance with the provider configuration
const compute = createCompute({
provider: e2b({ apiKey: process.env.E2B_API_KEY })
})
const sandbox = await compute.sandbox.create({
runtime: 'python',
timeout: 300000, // 5 minutes
metadata: {
userId: 'user-123',
jobType: 'data-processing'
}
})
try {
// Write a file
await sandbox.filesystem.writeFile('/app/config.json', JSON.stringify({ debug: true }))
// Read a file
const content = await sandbox.filesystem.readFile('/app/config.json')
// List directory contents
const files = await sandbox.filesystem.readdir('/app')
// Check if a file exists
const exists = await sandbox.filesystem.exists('/app/config.json')
// Create a directory
await sandbox.filesystem.mkdir('/app/data')
// Remove a file or empty directory
await sandbox.filesystem.remove('/app/old-config.txt')
} finally {
// Always clean up
await sandbox.destroy()
}File Operations
Section titled “File Operations”Reading Files
Section titled “Reading Files”// Read a file as text
const content = await sandbox.filesystem.readFile('/path/to/file.txt')
// With error handling
try {
const content = await sandbox.filesystem.readFile('/nonexistent.txt')
} catch (error) {
console.error('Failed to read file:', error.message)
}Writing Files
Section titled “Writing Files”// Write a text file
await sandbox.filesystem.writeFile('/path/to/file.txt', 'Hello, World!')
// Write JSON data
const data = { key: 'value' }
await sandbox.filesystem.writeFile('/path/to/data.json', JSON.stringify(data))Working with Directories
Section titled “Working with Directories”// List directory contents
const entries = await sandbox.filesystem.readdir('/app')
entries.forEach(entry => {
console.log(`${entry.isDirectory ? '📁' : '📄'} ${entry.name} (${entry.size} bytes)`)
})
// Create a directory
await sandbox.filesystem.mkdir('/app/new-directory')
// Create nested directories
await sandbox.filesystem.mkdir('/app/nested/directory/structure')File Information
Section titled “File Information”// Check if a file or directory exists
const exists = await sandbox.filesystem.exists('/path/to/file')
// Get file information from readdir
const entries = await sandbox.filesystem.readdir('/app')
const fileInfo = entries.find(entry => entry.name === 'config.json')
if (fileInfo) {
console.log(`File size: ${fileInfo.size} bytes`)
console.log(`Last modified: ${fileInfo.lastModified}`)
}Removing Files and Directories
Section titled “Removing Files and Directories”// Remove a file
await sandbox.filesystem.remove('/app/old-file.txt')
// Remove an empty directory
await sandbox.filesystem.remove('/app/empty-directory')FileEntry Interface
Section titled “FileEntry Interface”When listing directory contents, the readdir method returns an array of FileEntry objects:
interface FileEntry {
name: string; // Name of the file or directory
path: string; // Full path to the entry
isDirectory: boolean; // Whether this is a directory
size: number; // Size in bytes
lastModified: Date; // Last modified timestamp
}Error Handling
Section titled “Error Handling”All filesystem operations will throw an Error object if something goes wrong. Always use try/catch blocks to handle potential errors:
try {
await sandbox.filesystem.readFile('/nonexistent.txt')
} catch (error) {
console.error('Error:', error.message)
}Best Practices
Section titled “Best Practices”- Always use try/catch blocks around filesystem operations
- Check if files/directories exist before accessing them when appropriate
- Clean up temporary files when they’re no longer needed
- Use absolute paths for reliability
- Be mindful of file size limitations in your environment