Blobs / Blobs
Blobs
The Blobs engine keeps files by path in named sets, under blobs/ of the
store's directory: avatars, attachments, exports. A file is written whole or
not at all, replaced whole, checked against its SHA-256 when it is read
whole, and served with an ETag. Folders group files by owner, so deleting a
user's files is one call.
const avatars = store.files('avatars', { maxFileSize: '5MiB' })
await avatars.folder('users', 42).put('avatar.png', request.body!, { contentType: 'image/png' })
const file = await avatars.folder('users', 42).get('avatar.png') // StoredFile | undefined
await avatars.folder('users', 42).clear() // every file of user 42avatars = store.files("avatars", max_file_size="5MiB")
await avatars.folder("users", 42).put("avatar.png", request.stream(), content_type="image/png")
file = await avatars.folder("users", 42).get("avatar.png") # StoredFile | None
await avatars.folder("users", 42).clear() # every file of user 42avatars, err := blobs.OpenFiles(store, "avatars", blobs.MaxFileSize(5<<20))
_, err = avatars.Folder("users", 42).Put(ctx, "avatar.png", r.Body, blobs.ContentType("image/png"))
file, found, err := avatars.Folder("users", 42).Get(ctx, "avatar.png") // io.ReadSeekCloser
err = avatars.Folder("users", 42).Clear(ctx) // every file of user 42let avatars = store.files("avatars").max_file_size(5 << 20).open()?;
avatars.folder("users").folder(42).key("avatar.png").content_type("image/png").put(&png)?;
let file = avatars.folder("users").folder(42).get("avatar.png")?; // Option<StoredFile>
avatars.folder("users").folder(42).clear()?; // every file of user 42What it guarantees
- A file appears whole at its commit, or not at all: an upload that is cut, cancelled or of the wrong size leaves nothing (contract BLOB-01).
- A whole read is checked against the file's SHA-256 and fails
corruptbefore its end if a byte changed (contract BLOB-02). - A reader keeps the version it opened through a replace or a delete (contract BLOB-03).
- Of two writes that read the same ETag, one conflicts, unless the first put back the very bytes it read: an ETag names the bytes alone (contract BLOB-04).
- A copy shares the bytes and outlives its source (contract BLOB-05).
- A path is its own bytes on every file system:
Photo.jpgandphoto.jpgare two files everywhere (contract BLOB-08). - A set of files opened from a database,
db.files(…), commits with the database's rows in its transaction (contract DUR-06). See Files in a database. - A set with
idlekeeps a file for its term from its lastgetorhead, and a write past a set'smaxSizeremoves the files used least recently, or withwhenFull: 'refuse'fails and removes nothing; the set keeps its ceiling (contracts BLOB-17 to BLOB-20, BLOB-23, BLOB-24). See Caching.
In these guides
| Page | Covers |
|---|---|
| Uploads | streams, upload and commit, size, maxFileSize |
| Serving files | ETags, ranges, content-disposition, Go's ServeContent |
| Copies and expiry | copy, rename, ttl, expire |
| Integrity | checked reads, the scrub, ifMatch and create |
| Caching | idle, maxSize, whenFull, setMaxSize, trim |
| Directories | a folder of the machine listed and read, scan, changes |
New to Blobs? Attachments in the Handbook stores and serves a chat's uploads.
Async Rust
Rust only: with the async feature, files opened from
tinystore::aio::Store have the same calls, awaited. Rust with
async explains the async store, what a
dropped future does and how to cross between the two APIs.
// blocking
let avatars = store.files("avatars").max_file_size(5 << 20).open()?;
let info = avatars.key("42.png").content_type("image/png").put(&png)?;
let file = avatars.get("42.png")?; // Option<StoredFile>
avatars.folder("users").folder(42).clear()?;// async
use tinystore::Options;
use tinystore::aio::Store;
let store = Store::open("data", Options::default())?; // opening stays a plain call
let avatars = store.files("avatars").max_file_size(5 << 20).open()?;
let info = avatars.key("42.png").content_type("image/png").put(png).await?; // png: Vec<u8>, moved in
let file = avatars.get("42.png").await?; // Option<StoredFile>
avatars.folder("users").folder(42).clear().await?;put and create take owned bytes, such as a Vec<u8>, a String or
Bytes, because a file larger than 16 KiB is written to disk on the store's
own threads, and the future may be gone by then.
The file that get returns is a Stream of its bytes, 256 KiB per piece.
Hand it to a response as it is:
let Some(file) = avatars.get("42.png").await? else { return Ok(not_found()) };
let body = axum::body::Body::from_stream(file); // checked against its SHA-256 as it streamsA stream that starts at the first byte is checked. If a byte changed on
disk, the last piece fails with a corrupt error (ErrorKind::Corrupt)
instead of arriving, so a client never gets the whole changed file (contract
BLOB-02). After file.seek(...) the stream is a range, and a range is not
checked. read_all().await reads the whole file, checked, and
read_at(&mut buffer, offset).await reads a range.
An upload takes each piece with write, and the file appears at commit:
let mut upload = avatars.key("43.png").content_type("image/png").upload().await?;
while let Some(chunk) = request.next().await { // request: a Stream of the body's chunks
upload.write(&chunk?).await?;
}
let info = upload.commit().await?; // dropped without it, the upload aborts and leaves nothingA list's page is awaited, and all is a Stream that reads one page at a
time:
| Blocking | Async |
|---|---|
avatars.put(path, &bytes)? | avatars.put(path, bytes).await?, the bytes owned |
file.read_all()? | file.read_all().await? |
std::io::copy(&mut file, &mut response)? | the file is a Stream: Body::from_stream(file) |
std::io::copy(&mut request, &mut upload)? | upload.write(&chunk).await? for each piece |
for info in avatars.all() | while let Some(info) = files.next().await |
tinystore::aio::blobs::Files::from(files) and files.blocking() cross
between the two APIs, and give the same set of files.
Maintenance has no async form. The store runs it by itself every minute.
Only a test that opens its store with background: false calls it, and that
call blocks: tinystore::blobs::maintain(store.blocking())?.
Where the work runs
| Call | Where it runs | If you drop its future after it was polled |
|---|---|---|
head | in the first poll when a reader is free, else on the store's threads | it answers in that poll, or stops |
get | as head, but a file on disk is always opened on the store's threads | it answers in that poll, or stops |
a list's page, all, usage | on the store's threads | it stops |
a file's read_all, read_at and its stream | in the poll for a file kept inline, else on the store's threads | it stops |
delete, copy, rename, expire, clear | queued in the first poll, committed on the store's threads | it still commits |
put, create up to 16 KiB | waits for the store's memory without a thread, then queued as delete is | it still commits, after its wait for memory |
put, create past 16 KiB | waits for the store's memory without a thread, then written and committed on the store's threads | it still commits, after its wait for memory |
upload | waits for the store's memory without a thread | it leaves the line and holds nothing |
an upload's write | in the poll up to 16 KiB, then on the store's threads | its piece is still written |
an upload's commit | queued in the poll up to 16 KiB, else written and committed on the store's threads | it still commits |
A small put, create or commit also runs on the store's threads when
TinyStore must first save a new block of ids for file contents. That happens
for the first file after the store opens, and then about once every 1,000
files.
A file on disk is opened, read and written on the store's threads. A polled
put commits even if you drop its future while it still waits for memory
(contract DUR-11). A whole read that you drop stops between two pieces of
256 KiB (contract STORE-09).
Reference
Bun and Node · Python · Go · Rust