Declare and use a volume
Save this asreports.py:
Upload inputs before the first run
A local setup script can seed a volume before any workload runs:volumes=[models] then reads /models/weights.pt. create()
reuses an existing volume. Setup like this belongs behind
if __name__ == "__main__":, so importing the module never uploads anything.
Read and write from Python
Inside a container a volume is a directory. Locally, theVolume object reads
and writes it:
put(), write_text(), write_bytes(), read_bytes(), list_path(), and
move() cover the rest. Multi-gigabyte uploads go in parts through
create_multipart_upload() and its companions.
Share files between containers
Every container that mounts a volume sees the same files. Under the directory is object storage, which sets a few rules:- A file appears to other containers, and becomes durable, when its writer closes it.
- Two writers to one path don’t merge. The last close wins, so each call writes
its own path, such as
/reports/{task_id}.json. - There are no locks. A queue or map coordinates turns.
- Appending to a large file rewrites it, so logs fit better as many small files.
- Large reads are cached on the machine, which makes volumes good for model weights.
.part and renaming means no container ever sees a half-written
file. Two containers starting together may both download, which wastes one
download but corrupts nothing.
Mount an existing cloud bucket
CloudBucket mounts an S3-compatible bucket you own, reading and writing it in
place.
access_key and secret_key name secrets. Without them,
the mount uses the machine’s own credentials, such as an instance role in your
AWS account. bucket, prefix, endpoint,
force_path_style, and read_only cover other layouts and stores. The bucket
must already exist. See the Parquet example.
Delete stored data
reports.remove("latest.json") deletes one file and reports.delete() the whole
volume. A workload that still mounts it recreates it empty on its next run. The
file commands work from a terminal too.