Transfer files between your local machine and sandboxes, or manage files directly inside the sandbox filesystem.
File operations use the sandbox proxy URL: https://<sandbox-id>.sandbox.tensorlake.ai
Copy Files
from tensorlake.sandbox import SandboxClient
client = SandboxClient()
sandbox = client.create_and_connect()
# Write a file into the sandbox
sandbox.write_file("/workspace/data.csv", b"name,score\nAlice,95\nBob,87")
# Read a file from the sandbox
content = sandbox.read_file("/workspace/data.csv")
print(bytes(content).decode("utf-8"))
# Copy a local file into the sandbox
tl sbx cp data.csv <sandbox-id>:/workspace/data.csv
# Copy a file from the sandbox to local
tl sbx cp <sandbox-id>:/workspace/data.csv ./data.csv
# Copy an entire directory into the sandbox
tl sbx cp ./my-project <sandbox-id>:/workspace/project
# Copy a directory from the sandbox to local
tl sbx cp <sandbox-id>:/workspace/output ./output
# Write a file
curl -X PUT "https://<sandbox-id>.sandbox.tensorlake.ai/api/v1/files?path=/workspace/data.csv" \
-H "Authorization: Bearer $TL_API_KEY" \
-H "Content-Type: application/octet-stream" \
--data-binary "name,score\nAlice,95\nBob,87"
# Read a file
curl "https://<sandbox-id>.sandbox.tensorlake.ai/api/v1/files?path=/workspace/data.csv" \
-H "Authorization: Bearer $TL_API_KEY"
Read Files
# Read text content
content = sandbox.read_file("/workspace/data.csv")
print(bytes(content).decode("utf-8"))
# Read binary content (e.g., images, archives)
image_bytes = sandbox.read_file("/workspace/chart.png")
with open("chart.png", "wb") as f:
f.write(image_bytes)
# Copy a file from the sandbox to view locally
tl sbx cp <sandbox-id>:/workspace/data.csv ./data.csv
# Or print file contents directly
tl sbx exec <sandbox-id> cat /workspace/data.csv
# Read a text file
curl "https://<sandbox-id>.sandbox.tensorlake.ai/api/v1/files?path=/workspace/data.csv" \
-H "Authorization: Bearer $TL_API_KEY"
# Download a binary file
curl "https://<sandbox-id>.sandbox.tensorlake.ai/api/v1/files?path=/workspace/chart.png" \
-H "Authorization: Bearer $TL_API_KEY" \
-o chart.png
Write Files
# Write text content
sandbox.write_file("/workspace/config.json", b'{"debug": true, "port": 8080}')
# Write binary content
with open("model.pkl", "rb") as f:
sandbox.write_file("/workspace/model.pkl", f.read())
# Copy a local file into the sandbox
tl sbx cp config.json <sandbox-id>:/workspace/config.json
# Copy multiple files by copying a directory
tl sbx cp ./data <sandbox-id>:/workspace/data
# Write from a string
curl -X PUT "https://<sandbox-id>.sandbox.tensorlake.ai/api/v1/files?path=/workspace/config.json" \
-H "Authorization: Bearer $TL_API_KEY" \
-H "Content-Type: application/octet-stream" \
--data-binary '{"debug": true, "port": 8080}'
# Upload a local file
curl -X PUT "https://<sandbox-id>.sandbox.tensorlake.ai/api/v1/files?path=/workspace/model.pkl" \
-H "Authorization: Bearer $TL_API_KEY" \
-H "Content-Type: application/octet-stream" \
--data-binary @model.pkl
List Directory Contents
entries = sandbox.list_directory("/workspace")
for entry in entries.entries:
print(f"{entry.name} ({entry.size} bytes)")
tl sbx exec <sandbox-id> ls -la /workspace
curl "https://<sandbox-id>.sandbox.tensorlake.ai/api/v1/files/list?path=/workspace" \
-H "Authorization: Bearer $TL_API_KEY"
Response:{
"path": "/workspace",
"entries": [
{"name": "data.csv", "is_dir": false, "size": 30, "modified_at": 1710000000000},
{"name": "src", "is_dir": true, "size": null, "modified_at": 1710000000000}
]
}
Delete Files
sandbox.run("rm", ["-rf", "/workspace/temp"])
tl sbx exec <sandbox-id> rm -rf /workspace/temp
curl -X DELETE "https://<sandbox-id>.sandbox.tensorlake.ai/api/v1/files?path=/workspace/temp" \
-H "Authorization: Bearer $TL_API_KEY"
Organize Files
# Create directories
sandbox.run("mkdir", ["-p", "/workspace/src/components"])
# Move files
sandbox.run("mv", ["/workspace/old.txt", "/workspace/new.txt"])
# Create directories
tl sbx exec <sandbox-id> mkdir -p /workspace/src/components
# Move files
tl sbx exec <sandbox-id> mv /workspace/old.txt /workspace/new.txt
Best Practices
- Use
/workspace as the default directory for application files.
- Use absolute paths to avoid ambiguity.
- For large transfers, copy a directory rather than individual files.
- Use
write_file / read_file (Python) for programmatic access. Use tl sbx cp (CLI) for bulk file transfers.
Learn More