File Storage
Named storage disks with local filesystem and Amazon S3 drivers behind one facade.
Alata abstracts file storage behind named disks. Each disk pairs a driver — local filesystem or Amazon S3 — with its own configuration, and your code talks to all of them through the same Storage facade. Swap a disk from local files in development to S3 in production without touching application code.
from alata.support.storage import Storage
Storage.disk().put("reports/output.csv", csv_bytes) # default disk
Storage.disk("s3").put("reports/output.csv", csv_bytes) # a named diskConfiguring disks
Disks are declared in config/filesystem.py as the DISKS dict. The special default key names the disk used when you call Storage.disk() with no argument:
DISKS = {
"default": "local",
"local": {
"driver": "file",
"path": "storage/app",
},
"s3": {
"driver": "s3",
"client": "", # AWS access key id
"secret": "", # AWS secret access key
"bucket": "local-bucket",
"region": "us-east-1",
"endpoint": None, # override for S3-compatible stores
"use_path_style": False, # path-style addressing (MinIO, LocalStack)
},
}You can declare as many disks as you like — several S3 buckets, several local roots — each with any name. The driver key selects the implementation: "file" or "s3".
At boot, StorageProvider reads filesystem.disks, builds the Storage manager with both driver classes registered, and binds it in the service container as storage. If the config namespace is missing entirely, boot fails with a clear error telling you to add config/filesystem.py.
The Storage facade
Storage.disk(name) resolves the manager from the container and returns the driver configured for that disk, ready to use:
from alata.support.storage import Storage
disk = Storage.disk() # the default disk
s3 = Storage.disk("s3") # a specific diskRequesting a disk that isn't configured raises ValueError, as does a disk whose driver key has no registered implementation.
One driver instance per disk
Each disk gets its own driver instance, built on first use and cached by disk name — so repeated calls for the same disk hand back the same object, and different disks never share one. A handle is bound to its disk for life:
private = Storage.disk("private")
public = Storage.disk("public") # resolving another disk cannot re-point `private`
await something_slow() # other tasks may resolve other disks meanwhile
await private.aput("payroll.csv", data) # still lands on the private diskYou can retain a disk handle across await points; resolving another disk does not change it.
Storage (the manager, not the facade) also accepts driver classes through add_driver("name", DriverClass) when you register your own driver; a driver instance is still accepted and used as a prototype, cloned per disk. Custom drivers should accept (application, options) in their constructor — the older set_options(options) setter also still works.
The plain storage methods are synchronous — they block while reading, writing, or talking to S3. In async code, use the a-prefixed twins (Async usage): await Storage.disk("s3").aput(key, data). Use plain methods in synchronous handlers, synchronous jobs, or console commands.
Driver API
Both drivers expose the same core methods, so code written against one disk works against another.
Writing files
disk = Storage.disk()
# Write text or bytes; parent directories are created automatically
disk.put("reports/2026/summary.txt", "quarterly numbers")
disk.put("reports/2026/summary.pdf", pdf_bytes)put(path, content) accepts str or bytes and returns the content it wrote.
For FastAPI uploads, read the UploadFile and pass its bytes to aput. Generate the storage key yourself:
from uuid import uuid4
from fastapi import UploadFile
from alata.support.storage import Storage
async def save_upload(upload: UploadFile) -> str:
key = f"uploads/{uuid4().hex}"
await Storage.disk().aput(key, await upload.read())
return keyAn endpoint accepting multipart uploads also needs python-multipart (uv add python-multipart).
put_file(directory, file, name=None) supports a different file interface: an object with .name whose get_content() returns text or bytes, or a bytes-compatible object with .name. It returns a generated path, preserving the extension. FastAPI's UploadFile does not implement that interface; use the example above.
append(path, content) adds to the end of a file; prepend(path, content) adds to the beginning.
Reading files
text = disk.get("reports/2026/summary.txt") # str, or None if missing
raw = disk.get_bytes("reports/2026/summary.pdf") # bytes, or None if missingBoth return None for a missing file instead of raising — check for None rather than catching exceptions.
Checking, listing, and removing
disk.exists("reports/2026/summary.txt") # True / False
disk.missing("reports/2026/summary.txt") # the inverse
disk.get_files("reports/2026") # relative paths of files in a directory
disk.get_files() # files at the disk root
disk.delete("reports/2026/summary.txt") # no error if already goneOn S3, get_files() lists objects by prefix across the whole "directory" tree; on the local driver it lists the files directly inside the given directory.
Copying and moving
disk.copy("reports/draft.txt", "reports/final.txt")
disk.move("reports/final.txt", "reports/archived.txt")Both operate within a single disk. On the local driver, create the destination directory before moving a file into a new directory. On S3, move is implemented as copy-then-delete. To transfer between disks, read from one and write to the other:
data = Storage.disk("local").get_bytes("exports/data.csv")
if data is None:
raise FileNotFoundError("exports/data.csv")
Storage.disk("s3").put("exports/data.csv", data)MIME detection helper
For serving or validating uploads, alata.utils.filesystem.mime_type sniffs a MIME type from magic bytes (PDF, PNG, and JPEG are recognized; anything else is application/octet-stream):
from alata.utils.filesystem import mime_type
data = disk.get_bytes("uploads/file.bin")
if data is not None:
kind = mime_type(data) # e.g. "image/png"Async usage
Every I/O method has an a-prefixed async twin — aget, aget_bytes, aput, aexists, adelete, acopy, amove, astore, aget_files, and the rest — following the framework-wide convention: plain name is sync, prefix a to await.
content = await disk.aget_bytes("reports/2026/summary.pdf")The twins offload the synchronous driver call to a worker thread, so a download inside an async route never stalls the event loop. Calling the sync method from async code does — a multi-megabyte get_bytes() freezes every task on that worker until the transfer finishes. In async code, always use the twin.
A custom driver gets the async surface for free by implementing the sync methods and mixing in alata.storage.async_ops.AsyncStorageOps.
The local driver
The file driver stores files under the disk's path, resolved relative to the process working directory — the project root under the ./alata and uvicorn conventions. The scaffolded disk points at storage/app, so:
Storage.disk("local").put("avatars/1.png", data)
# → <project>/storage/app/avatars/1.pngMissing parent directories are created on write. make_directory(path) creates a directory explicitly, and get_path(key) returns the absolute filesystem path for a stored file when you need to hand it to another library.
Keys are confined to the disk root
Every key is resolved against the disk root and verified to land inside it. A key that escapes never touches the filesystem — it raises StoragePathError (a ValueError subclass) naming the offending key:
from alata.storage.storage import StoragePathError
disk.get("../../etc/passwd") # StoragePathError
disk.get("/etc/passwd") # StoragePathError
disk.put("../../../tmp/evil", data) # StoragePathErrorThe guarantee covers every method — get, get_bytes, put, put_file, append, prepend, copy, move, delete, exists, missing, store, make_directory, get_files — and holds for:
..segments anywhere in the key, including ones spelled with backslashes;- absolute paths, which are accepted only when they already point inside the root;
- symlinks that resolve out of the root — the check compares fully resolved paths, so a link inside the disk cannot be used as a door out of it.
Ordinary nested keys are unaffected, and .. that stays inside the root is fine:
disk.put("reports/2026/q1/summary.txt", data) # fine
disk.put("reports/2026/../2025/summary.txt", data) # fine — still inside the rootPath checks confine keys to a disk; they do not authorize access to individual files. Generate upload keys and enforce ownership before reading, replacing, or deleting user files.
The S3 driver
Install the S3 extra, then configure a disk:
uv add "alata[s3]"The s3 driver uses these options:
| Option | Meaning |
|---|---|
client | AWS access key id. |
secret | AWS secret access key. |
bucket | Bucket name — every operation targets this bucket. |
region | AWS region for the session. |
endpoint | Optional endpoint URL — point it at MinIO, LocalStack, or another S3-compatible store. |
use_path_style | Use path-style addressing instead of virtual-hosted (required by many S3-compatible stores). |
Each disk can target a different bucket, region, or AWS account.
S3 "not found" responses (404 / NoSuchKey) are translated to the same conventions as the local driver — get and get_bytes return None, exists returns False. Any other AWS error propagates as an exception.
There is no streaming API: get_bytes loads the whole object into memory and put uploads from an in-memory value. For very large files, work with the disk's underlying tools directly — the local driver's get_path() gives you a real filesystem path, and for S3 you can build your own boto3 client for multipart transfers.