Move files in and out of a BoxLite sandbox — upload a file, run a script on it, and download the results using three different methods.
Most real workloads need data. This tutorial shows you how to get files into a sandbox, process them, and get results back out. You’ll learn three methods and when to use each one.
The most common approach. Upload a file, process it, download the results.
Python
Node.js
file_transfer.py
import asyncioimport boxliteasync def main(): async with boxlite.SimpleBox(image="python:slim") as box: # Upload a script into the box # Note: destination must be a directory path, not a file path await box.exec("mkdir", "-p", "/workspace") await box.copy_in("/host/script.py", "/workspace/") # Run the script result = await box.exec("python", "/workspace/script.py") print(f"Exit code: {result.exit_code}") print(result.stdout) # Download the results await box.copy_out("/workspace/output.json", "/host/")if __name__ == "__main__": asyncio.run(main())
The Node.js SDK does not currently expose copyIn() / copyOut() methods. Use exec with shell commands to write files, and exec with cat to read them back.
file_transfer.js
import { SimpleBox } from '@boxlite-ai/boxlite';async function main() { const box = new SimpleBox({ image: 'python:slim' }); try { // Create workspace and write a script using a heredoc await box.exec('sh', '-c', `mkdir -p /workspace && cat > /workspace/script.py << 'EOF'import jsondata = {"result": 42}print(json.dumps(data))EOF`); // Run the script const result = await box.exec('python', '/workspace/script.py'); console.log(`Exit code: ${result.exitCode}`); console.log(result.stdout); // Read a file back from the box via exec const output = await box.exec('cat', '/workspace/script.py'); console.log(output.stdout); } finally { await box.stop(); }}main();
When to use: Dynamic per-request files — scripts, input data, configuration that varies across runs.
When to use: Shared datasets or configuration that multiple boxes need access to. Use read-only mode for input data to prevent accidental modification.
Volume mounts give the guest direct access to host files. Always use read-only mode (True) for input data. Only use False for designated output directories.