/gridfs/. Files are stored inside MongoDB itself: metadata in the
<bucket>.files collection, content in 255KB chunks in <bucket>.chunks.
Uses pymongo’s native async API.
Install the extra:
Config
READ and WRITE modes are supported. Small files are fine: a file
smaller than the chunk size is stored as a single chunk document, so tiny
config files and large binaries go through the same path.
Filesystem Layout
GridFS filenames are used as slash-separated keys, exactly like S3 object keys. Directories are prefix-based;mkdir creates a zero-byte key/
marker document so empty directories stay visible.
/gridfs/ exposes:
Revisions
GridFS filenames are not unique: every write uploads a new revision and keeps the old ones. The resource always reads and lists the newest revision per filename, and snapshot-pinned reads resolve old revisions by their file_id, so time-travel reads work natively. mv retags every revision’s
filename server-side (history moves with the file), while rm deletes all
revisions of the file.
Server-side find
Unlike object stores that page through every key, GridFS metadata lives in an ordinary MongoDB collection, sofind pushes its filters into the
fs.files query itself:
-name/-inameglobs become anchored regexes on the filename-type f/-type dfilter on the directory-marker shape-sizebounds map to the storedlength
find /gridfs/ -name '*.csv' -size +1M narrows inside MongoDB and only
matching metadata comes back, so find stays cheap even on large buckets.
Semantics are identical to every other backend: the shared predicate
evaluation still runs on the results.
Cache
The GridFS resource usesIndexCacheStore with index_ttl = 600
(10 minutes). Directory listings are cached before being refreshed from
MongoDB, reducing queries for repeated traversals.
Example
Shell Commands
The GridFS resource supports the same full command surface as the S3 resource: read commands (cat, head, tail,
grep, rg, jq, wc, stat, find, tree, du, checksums), text
processing (awk, sed, sort, cut, diff, …), file operations
(cp, mv, rm, mkdir, touch, tee, split, …), compression, and
encoding. Range reads (head -c, tail -c) seek chunk-wise, so large
files are never downloaded whole.
Columnar data files (.parquet, .feather, .orc, .hdf5) render as
tabular text for cat, head, tail, wc, stat, cut, grep, ls,
and file.
Scoping a resource to a key prefix
Passkey_prefix to GridFSConfig to transparently scope every operation
to a subpath of the bucket, mirroring the S3 key_prefix behavior:
/data/notes.md; the underlying GridFS
filename is users/{user_id}/data/notes.md. Leading slashes are stripped
and a trailing slash is added automatically; None and an empty string
mean “no prefix.”
Use Cases
- Files next to your data: keep agent-visible files in the same MongoDB deployment as the rest of your application state, with one backup story
- Revision-aware storage: writes never destroy old content; snapshots can pin reads to the exact revision they saw
- Cheap find on big buckets: metadata queries replace full listings
- Multi-tenant mounts: one bucket, per-tenant
key_prefixscoping