Files
Move files in and out with the SDK, fdtn cp or fdtn collect.
Write and read a file
Python · TypeScript · CLI
from fdtn import Sandbox sbx = Sandbox.create("base") sbx.files.write("/home/user/data/in.txt", "3\n1\n2\n") sbx.commands.run("sort /home/user/data/in.txt > /home/user/data/out.txt") print(sbx.files.read("/home/user/data/out.txt")) # 1 2 3 sbx.kill()
import { Sandbox } from '@fdtn/sdk' const sbx = await Sandbox.create('base') await sbx.files.write('/home/user/data/in.txt', '3\n1\n2\n') await sbx.commands.run('sort /home/user/data/in.txt > /home/user/data/out.txt') console.log(await sbx.files.read('/home/user/data/out.txt')) // 1 2 3 await sbx.kill()
$ fdtn create base -d --name worker $ fdtn cp ./in.txt @worker:/home/user/data/in.txt $ fdtn exec @worker bash -c 'sort /home/user/data/in.txt > /home/user/data/out.txt' $ fdtn cp @worker:/home/user/data/out.txt ./out.txt
A write creates missing parent folders and replaces an existing file.
Files are deleted when the sandbox ends.
SDK file methods
A relative path starts at the home folder.
| Task | Python | TypeScript |
|---|---|---|
| Write one file | write(path, data) | write(path, data) |
| Write many files | write_files([...]) | write([...]) |
| Read a file | read(path, format=...) | read(path, { format }) |
| List a folder | list(path, depth=1) | list(path, { depth }) |
| Check a path | exists(path) | exists(path) |
| File details | get_info(path) | getInfo(path) |
| Make a folder | make_dir(path) | makeDir(path) |
| Move or rename | rename(old, new) | rename(old, new) |
| Remove (with contents) | remove(path) | remove(path) |
| Watch a folder | watch_dir(path) | watchDir(path, onEvent) |
Each method also takes user. See Python, TypeScript.
Write data
An open file or a Blob uploads in pieces.
# text and bytes sbx.files.write("/app/config.json", '{"debug": true}') sbx.files.write("/app/logo.png", open("logo.png", "rb").read()) # a large local file, sent in pieces with open("dataset.csv", "rb") as f: sbx.files.write("/app/dataset.csv", f) # many files in one call sbx.files.write_files([ {"path": "/app/a.py", "data": "print('a')"}, {"path": "/app/b.py", "data": "print('b')"}, ])
import { openAsBlob } from 'node:fs' // text await sbx.files.write('/app/config.json', '{"debug": true}') // a large local file, sent in pieces await sbx.files.write('/app/dataset.csv', await openAsBlob('dataset.csv')) // many files in one call await sbx.files.write([ { path: '/app/a.py', data: "print('a')" }, { path: '/app/b.py', data: "print('b')" }, ])
Read data
Reads return text. Use bytes for binary, stream for large files.
text = sbx.files.read("/app/out.txt") image = sbx.files.read("/app/chart.png", format="bytes") with open("big.tar", "wb") as f: for chunk in sbx.files.read("/app/big.tar", format="stream"): f.write(chunk)
import { createWriteStream } from 'node:fs' import { Readable } from 'node:stream' import { pipeline } from 'node:stream/promises' const text = await sbx.files.read('/app/out.txt') const image = await sbx.files.read('/app/chart.png', { format: 'bytes' }) const stream = await sbx.files.read('/app/big.tar', { format: 'stream' }) await pipeline(Readable.fromWeb(stream), createWriteStream('big.tar'))
fdtn cp
Write the sandbox side as <id>:/path or @name:/path.
$ fdtn cp ./input.json @worker:/app/input.json # upload one file $ fdtn cp -r ./src @worker:/app/src # upload a folder $ fdtn cp -r @worker:/app/results ./results # download a folder $ fdtn cp --force @worker:/app/out.txt ./out.txt # replace
- Without
--force, the copy stops at the first file that exists. - A folder copy puts its contents into the destination folder.
- The copy stops at a symbolic link. On macOS, write
/private/tmp, not/tmp. - A copy to or from a paused sandbox resumes it.
- For a local path with a colon, use
fdtn files.
fdtn files
ls, pull, push, info, mkdir, move, remove. CLI
Collect from many sandboxes
fdtn collect downloads one folder from each sandbox with the given metadata, into a local folder per sandbox ID. manifest.json holds a SHA-256 checksum per file.
$ fdtn collect --metadata run=eval-1 --path /app/results --destination ./results-1--destinationmust not exist (elseEEXIST).- Paused sandboxes resume for the download.
- On exit code 1, run
fdtn collect --retry-failed --destination ./results-1to retry only the failed sandboxes.
Full flow: Many sandboxes.
Size limits
Free disk is 5, 10 or 20 GiB by template memory, at no charge (sizes). Put large shared inputs in a template.