Reference / Blobs API for Python
On this page

Reference / Blobs API for Python

Blobs API for Python

Every public class, function and type of the Blobs engine in tinystore, generated from its source. The Blobs guide explains how to use them, and the Bun and Node, Go and Rust pages list the same API.

FileInfo

python
@dataclass(frozen=True, slots=True)
class FileInfo:
    path: str  # its path within the folder it was asked from
    size: int
    etag: str  # the SHA-256 of its bytes in hex, quoted, as an HTTP ETag is
    content_type: str
    last_modified: datetime
    expires: datetime | None  # None for a file that does not expire
    meta: dict[str, str] = field(default_factory=dict[str, str])

What a file carries: what serving it needs.

Fill

python
@dataclass(frozen=True, slots=True)
class Fill:
    data: Body | None = None  # bytes, text as UTF-8, a file object opened for bytes, or an async iterator of bytes
    path: str | os.PathLike[str] | None = field(default=None, kw_only=True)
    content_type: str | None = field(default=None, kw_only=True)
    meta: Mapping[str, str] | None = field(default=None, kw_only=True)

What a get_or_create's function gives: the file's bytes, or a file of this machine to read them from.

Its content_type and meta take the place of the call's:

Fill(pdf, content_type="application/pdf")
Fill(path="/tmp/7f3a.pdf")  # streamed, never whole in memory

FilePage

python
@dataclass(frozen=True, slots=True)
class FilePage:
    files: list[FileInfo]
    next: str | None  # None on the last page

A page of a folder's files in the byte order of their paths, and where the next starts.

Usage

python
@dataclass(frozen=True, slots=True)
class Usage:
    count: int
    size: int
    max_size: int | None = None  # the set's ceiling, None when it keeps none
    evictions: int = 0  # the files the ceiling removed since the store opened

The files of a folder and their bytes, as it is now, and the set's ceiling and what it evicted.

Trim

python
@dataclass(frozen=True, slots=True)
class Trim:
    evictions: int
    freed: int

What a trim removed: its files, and their sizes as usage counts them.

Files

python
class Files:
    name: str
    opener: Opener  # what opens the files' handle on a connection
    home: Home | None  # the database whose file keeps the files; the store's own when None

A named set of files by path, or a folder of it, whose calls are the folder's.

A path in a folder is the folder's segments and then its own. Files come from store.files and db.files; inside a database's transaction, from tx.with_(files), whose writes go through the transaction.

Files.folder

python
def folder(*segments: str | int) -> Files: ...

The folder of these segments, one each: folder("users", 42) is users/42, never users/420.

Files.through

python
def through(via: Via) -> Files: ...

The same files, their calls through a database's transaction.

Files.put

python
async def put(
    path: str,
    body: Body,
    *,
    content_type: str | None = None,
    meta: dict[str, str] | None = None,
    ttl: Duration | None = None,
    if_match: str | None = None,
    size: int | None = None,
) -> FileInfo: ...

Writes the file whole and replaces whatever the path held.

It returns once the file is on disk as the store's durability says. Nobody sees half of it, while it is written or after a crash.

Files.create

python
async def create(
    path: str,
    body: Body,
    *,
    content_type: str | None = None,
    meta: dict[str, str] | None = None,
    ttl: Duration | None = None,
    size: int | None = None,
) -> bool: ...

Writes the file only where the path holds none, and says whether it did.

Files.get

python
async def get(path: str) -> StoredFile | None: ...

What the file carries, and its bytes; None when the path holds none.

A file up to about a megabyte comes with them, and a larger one's are read when they are read, on its ETag, as S3's GET with If-Match reads them.

Files.get_or_create

python
async def get_or_create(
    path: str,
    fn: Callable[[], Fill | Awaitable[Fill]],
    *,
    content_type: str | None = None,
    meta: dict[str, str] | None = None,
    ttl: Duration | None = None,
    timeout: Duration | None = None,
) -> StoredFile: ...

The file at the path, or, on a miss, the one fn makes, written once while every other call waits.

While one call runs fn, every other get_or_create of the path, in this process or another, waits for it and gets the file it wrote. The store claims the path for the run and renews the claim while fn runs, up to timeout, 10 minutes unless given; what fn gives lands only while the claim still holds the path and no file is there. A put of the path meanwhile wins, and the calls get the put's file; a delete ends the claim, what fn gave is dropped, and one of the calls runs it again, so fn may run more than once. An exception of fn is raised here, and every call that waited for that run gets an ConflictError with its text, or the store's own error as it is; the next call runs fn again:

page = await pages.get_or_create(f"{hash}.pdf", lambda: Fill(convert(doc), content_type="application/pdf"))

Files.head

python
async def head(path: str) -> FileInfo | None: ...

What the file carries and none of its bytes; None when the path holds none.

Files.delete

python
async def delete(path: str, *, if_match: str | None = None) -> None: ...

Deletes the file, or with if_match only that version of it; no file there is not an error.

Files.copy

python
async def copy(source: str, to: str, *, if_match: str | None = None) -> FileInfo: ...

A copy at to that shares the bytes, which are not copied.

A file at to is ConflictError unless if_match names the version to replace.

Files.rename

python
async def rename(source: str, to: str, *, if_match: str | None = None) -> FileInfo: ...

The file moved to to in one write.

A file there is ConflictError unless if_match names the version to replace, and none at source NotFoundError.

Files.expire

python
async def expire(path: str, after: Duration | datetime) -> bool: ...

Sets when the file expires, a span from now or a time; says whether there was a file.

Files.list

python
async def list(
    *,
    prefix: str = '',
    after: str | None = None,
    limit: int | None = None,
) -> FilePage: ...

A page of the files in the folder and in the folders in it, in the byte order of their paths.

Files.all

python
async def all(*, prefix: str = '') -> AsyncIterator[FileInfo]: ...

Every file of the folder, a page at a time, holding nothing between pages.

Files.usage

python
async def usage() -> Usage: ...

The files in the folder and their bytes, as it is now, with the set's ceiling and its evictions.

It renews nothing.

Files.set_max_size

python
async def set_max_size(
    size: Size,
    *,
    if_current: Size | None = None,
    when_full: WhenFull | None = None,
) -> None: ...

Changes the set's ceiling for every handle, "none" removing it; a lower one evicts at once.

With if_current it changes only while the set keeps that ceiling, and another is ConflictError; one the set keeps already writes nothing:

await pages.set_max_size("1GiB", if_current="512MiB")

Files.trim

python
async def trim(*, to: Size) -> Trim: ...

Removes files in the eviction's order until the set holds at most to; its ceiling stays.

A set whose ceiling refuses is InvalidError:

trimmed = await pages.trim(to="300MiB")  # Trim(evictions=184, freed=220000000)

Files.clear

python
async def clear() -> None: ...

Removes the folder's every file and every folder in it, at once for every reader.

Files.upload

python
def upload(
    path: str,
    *,
    content_type: str | None = None,
    meta: dict[str, str] | None = None,
    ttl: Duration | None = None,
    if_match: str | None = None,
    size: int | None = None,
) -> Upload: ...

A file written a piece at a time, which appears at commit and never before.

async with files.upload(path) as upload, and leaving the block without commit aborts it.

Upload

python
class Upload:
    ...

A file written a piece at a time, as files.upload begins it.

Bytes go in with write, the file appears at commit and never before, and an upload that ends otherwise leaves nothing.

Upload.write

python
async def write(data: bytes | str) -> None: ...

Takes bytes, text as UTF-8, waiting while the store catches up.

Upload.commit

python
async def commit() -> FileInfo: ...

Publishes the file whole, replacing what the path held as put does, once it is on disk.

Upload.abort

python
async def abort() -> None: ...

Ends the upload, which leaves nothing; after commit it does nothing.

FileBytes

python
class FileBytes:
    size: int

Bytes of a file, read when they are read: async for chunk in file, or all of them with read().

FileBytes.read

python
async def read() -> bytes: ...

Every byte, held in memory: a large file is read a chunk at a time with async for.

FileBytes.text

python
async def text() -> str: ...

FileBytes.json

python
async def json() -> Any: ...

StoredFile

python
class StoredFile(FileBytes):
    info: FileInfo
    path: str
    etag: str
    content_type: str
    last_modified: datetime
    expires: datetime | None
    meta: dict[str, str]

A file as get found it: what it carries, and its bytes.

A whole read is checked against its SHA-256 and fails CorruptError before its last bytes when they changed; slice reads a range, which is not checked.

StoredFile.slice

python
def slice(start: int | None = None, end: int | None = None) -> FileBytes: ...

A range of its bytes, as Python slices them: slice(-500) is the last 500 bytes.

Generated from the source Updated 17 November 2026

esc
↑↓ navigate ↵ open ⇥ filter 0 results