The Bucket methods, their Python options and what they return. Each section links to the concept page with the behaviour and defaults.
Building the client, and the async AsyncBucket, are on Overview. Every method below exists on both, with the same arguments; on AsyncBucket it is awaited. Options are keyword-only.
put#
Every option is optional. Behaviour and defaults are on Writing.
| Option | Type | Description |
|---|---|---|
content_type | str | What the object is stored as. |
content_types | Sequence[str] | An allow list such as ["image/*", "application/pdf"]. A bare string raises a TypeError. See Constraints. |
max_size | Size | Refuses a larger body with too_large. |
cache | CacheOption | The Cache-Control this object is stored with. See Caching. |
metadata | Mapping[str, str] | Custom key-value pairs. A key with an uppercase letter is refused with invalid_input rather than lowercased for you. See Metadata. |
size | Size | The exact length in bytes, for an iterable whose size is not otherwise known. |
allow_overwrite | bool | False refuses the write if something is already at the path. See Conditional writes. |
if_unchanged | str | An etag. The write fails with conflict if the object changed since you read it. |
multipart | MultipartOption | Where a body stops being one PUT. See Large bodies. |
The result is a CompletedBlob:
Bodies#
PutBody is one of these. If the body does not carry its own length or type, declare size or content_type yourself.
| Body | Carries its length | Carries a content type |
|---|---|---|
bytes, bytearray, memoryview | Yes | No |
str | Yes, once UTF-8 encoded | No |
pathlib.Path, or any os.PathLike | Yes | Guessed from the file name |
| Binary file object | When it is seekable: what is left from its position | Guessed from its name, when it has one |
Iterable of bytes, such as a generator | No | No |
Async iterable of bytes | No | No. AsyncBucket only. |
Async file object, such as one from aiofiles | No | Guessed from its name, when it has one. AsyncBucket only. |
An explicit content_type wins over a guess.
- A
strbody is the text itself, never a file name. Wrap a file name inPath(...). - A file object is not closed for you. A file opened in text mode, or an iterable that yields
str, raises aTypeError. So does a body of any other type. AsyncBucketreads a sync file object on a worker thread, so the event loop is never blocked on disk. An async file object, such as one fromaiofiles, cannot report its length.- There is no request body type, so
putnever raisesempty_body. An emptybytesstores a zero-byte object.
Framework uploads#
A framework’s uploaded file is a file object, but a type guessed from its name is not reliable: FastAPI’s spooled file has no name, and on Flask name is the form field. Pass content_type from what the framework parsed: file.content_type on FastAPI’s UploadFile, f.mimetype on Flask’s request.files[...], f.content_type on Django’s request.FILES[...].
The bytes pass through your server here, under your platform’s body limit. For anything large, use an upload handler instead.
Iterables#
An iterable with neither raises length_required. Why is on Streams and unknown lengths. One that ends short of size, or runs past it, raises invalid_input.
On AsyncBucket, an async iterable works the same way.
unique_path#
Exported from upstash_blob. The suffix rules are on Unique paths.
update_json#
| Option | Type | Description |
|---|---|---|
max_attempts | int | How many read-transform-write rounds to try before raising conflict. Anything but a positive int raises invalid_input. |
cache | CacheOption | The Cache-Control the rewritten object is stored with. |
metadata | Mapping[str, str] | Replaces the metadata on the object. |
fn gets the parsed document, or None, and returns what json.dumps can write. On Bucket it is a plain function. On AsyncBucket it may also be async def. The result is a CompletedBlob, with content_type set to application/json.
The loop and its defaults are on Updating JSON.
copy and move#
| Option | Type | Description |
|---|---|---|
content_type | str | What the destination is stored as. |
cache | CacheOption | The Cache-Control the destination is stored with. |
metadata | Mapping[str, str] | Replaces the metadata outright. |
What carries over from the source is on copy and move.
signed_upload_url#
Returns a SignedUploadUrl with url: str, headers: dict[str, str] and expires_at: datetime. Send headers verbatim.
| Option | Type | Description |
|---|---|---|
expires_in | Duration | How long the link should live. |
content_type | str | The Content-Type the upload must send, and what the object is stored as. |
cache | CacheOption | The Cache-Control the object is stored with. |
metadata | Mapping[str, str] | Written as x-amz-meta-*, under the same rules as put. |
size | Size | Pins the body’s exact length. |
allow_overwrite | bool | False refuses the upload if something is already at the path. |
Defaults and lifetimes are on Signed upload URLs.
get#
Unlike the TypeScript SDK, get reads the whole body into memory before it returns. content is bytes; text() decodes it, UTF-8 by default, and json() parses it:
A missing object raises not_found. It never returns None. See get.
stream#
For an object too big to hold in memory, stream() is get with the body left on the wire. It is a context manager: the record is ready on entry, the bytes are read inside the block, and the connection is released on exit. A missing object raises not_found on entry.
The BlobStream it yields has the same fields as BlobDownload, minus content, plus iter_bytes(chunk_size=None) and read(). On AsyncBucket it is an async context manager yielding an AsyncBlobStream:
AsyncBlobStream.read() is awaited.
info and exists#
list#
Returns a ListPage with blobs: list[BlobObject] and cursor: str | None.
| Option | Type | Description |
|---|---|---|
prefix | str | Only keys starting with this. |
limit | int | Page size. |
cursor | str | The cursor from the previous page. |
Defaults and paging are on list.
public_url#
None on a private bucket. An empty path, or one with a . or .. segment, raises a ValueError. On AsyncBucket it is awaited, because the first call fetches credentials. See Public URLs.
signed_read_url#
Returns a SignedReadUrl with url: str and expires_at: datetime. Cache the link until expires_at, never until a deadline you compute from expires_in.
| Option | Type | Description |
|---|---|---|
expires_in | Duration | How long to ask for. |
download_as | str | Save as this filename instead of displaying inline. |
content_type | str | What storage answers with as Content-Type, overriding what was stored. |
Defaults and lifetimes are on Signed read URLs.
delete#
del is a Python keyword, so the method is delete. Pass a path or a list positionally, or prefix=; both or neither raises invalid_input. A path with a . or .. segment raises a ValueError. What each shape does and raises is on Deleting.
Multipart cleanup#
MultipartUpload is a dataclass with path: str, upload_id: str and initiated_at: datetime | None. initiated_at is None when storage sent a date that does not parse, and such an upload is never treated as stale.
abort_multipart_upload takes the record, not two strings, because a swapped pair would abort nothing and report success. Anything else is refused with invalid_input. To abort an upload whose id you stored from an upload handler, build the record:
older_than is required. See Incomplete multipart uploads.
s3#
Short-lived credentials for boto3 or any S3 client. See Using an S3 client.
Records#
Records are frozen dataclasses. They nest. What each field means is on Records.
| Type | Fields | Returned by |
|---|---|---|
BlobObject | path, url, versioned_url, size, etag, uploaded_at | list(), copy(), move() |
CompletedBlob | BlobObject plus content_type | put(), update_json(), on_upload_complete |
BlobInfo | BlobObject plus content_type and metadata | info() |
BlobDownload | BlobInfo plus content: bytes, text(), json() | get() |
BlobStream | BlobInfo plus iter_bytes(), read() | stream() on Bucket |
AsyncBlobStream | BlobInfo plus aiter_bytes(), read() | stream() on AsyncBucket |
| Field | Type |
|---|---|
path | str |
url | str | None |
versioned_url | str | None |
size | int |
etag | str |
uploaded_at | datetime, timezone-aware UTC |
content_type | str |
metadata | dict[str, str] |
All of them are exported from upstash_blob.