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.
.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; both are
required. bucket, prefix, endpoint,
force_path_style, and read_only cover other layouts and stores. Requests
name the bucket in the host name, as AWS expects; stores such as MinIO that
take the bucket in the path need force_path_style=True. A bucket name with a
dot always goes in the path. The bucket must already exist. The
video highlights example reads its input this way.
Memory for mounts
A container runs one mounter for all its volumes, plus one per distinct cloud bucket. Cloud buckets that differ only inprefix share one. Each mounter
reserves 768 MiB of memory on top of the container’s memory, billed as memory,
so a function with two volumes and one cloud bucket reserves 1.5 GiB beyond what
it asks for.
Delete stored data
reports.remove("latest.json") deletes one file and reports.delete() the whole
volume. delete() fails while a container that hasn’t stopped mounts the volume.
A workload that still declares the volume creates it empty on its next run. The
file commands work from a terminal too.