Skip to main content

The Bucket methods, their Python options and what they return. Each section links to the concept page with the behaviour and defaults.

app/blob.py

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#

app/reports.py

Every option is optional. Behaviour and defaults are on Writing.

OptionTypeDescription
content_typestrWhat the object is stored as.
content_typesSequence[str]An allow list such as ["image/*", "application/pdf"]. A bare string raises a TypeError. See Constraints.
max_sizeSizeRefuses a larger body with too_large.
cacheCacheOptionThe Cache-Control this object is stored with. See Caching.
metadataMapping[str, str]Custom key-value pairs. A key with an uppercase letter is refused with invalid_input rather than lowercased for you. See Metadata.
sizeSizeThe exact length in bytes, for an iterable whose size is not otherwise known.
allow_overwriteboolFalse refuses the write if something is already at the path. See Conditional writes.
if_unchangedstrAn etag. The write fails with conflict if the object changed since you read it.
multipartMultipartOptionWhere 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.

BodyCarries its lengthCarries a content type
bytes, bytearray, memoryviewYesNo
strYes, once UTF-8 encodedNo
pathlib.Path, or any os.PathLikeYesGuessed from the file name
Binary file objectWhen it is seekable: what is left from its positionGuessed from its name, when it has one
Iterable of bytes, such as a generatorNoNo
Async iterable of bytesNoNo. AsyncBucket only.
Async file object, such as one from aiofilesNoGuessed from its name, when it has one. AsyncBucket only.

An explicit content_type wins over a guess.

  • A str body is the text itself, never a file name. Wrap a file name in Path(...).
  • A file object is not closed for you. A file opened in text mode, or an iterable that yields str, raises a TypeError. So does a body of any other type.
  • AsyncBucket reads a sync file object on a worker thread, so the event loop is never blocked on disk. An async file object, such as one from aiofiles, cannot report its length.
  • There is no request body type, so put never raises empty_body. An empty bytes stores a zero-byte object.

Framework uploads#

app/main.py

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#

OptionTypeDescription
max_attemptsintHow many read-transform-write rounds to try before raising conflict. Anything but a positive int raises invalid_input.
cacheCacheOptionThe Cache-Control the rewritten object is stored with.
metadataMapping[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#

OptionTypeDescription
content_typestrWhat the destination is stored as.
cacheCacheOptionThe Cache-Control the destination is stored with.
metadataMapping[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.

OptionTypeDescription
expires_inDurationHow long the link should live.
content_typestrThe Content-Type the upload must send, and what the object is stored as.
cacheCacheOptionThe Cache-Control the object is stored with.
metadataMapping[str, str]Written as x-amz-meta-*, under the same rules as put.
sizeSizePins the body’s exact length.
allow_overwriteboolFalse 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#

See info and exists.


list#

Returns a ListPage with blobs: list[BlobObject] and cursor: str | None.

OptionTypeDescription
prefixstrOnly keys starting with this.
limitintPage size.
cursorstrThe 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.

OptionTypeDescription
expires_inDurationHow long to ask for.
download_asstrSave as this filename instead of displaying inline.
content_typestrWhat 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.

TypeFieldsReturned by
BlobObjectpath, url, versioned_url, size, etag, uploaded_atlist(), copy(), move()
CompletedBlobBlobObject plus content_typeput(), update_json(), on_upload_complete
BlobInfoBlobObject plus content_type and metadatainfo()
BlobDownloadBlobInfo plus content: bytes, text(), json()get()
BlobStreamBlobInfo plus iter_bytes(), read()stream() on Bucket
AsyncBlobStreamBlobInfo plus aiter_bytes(), read()stream() on AsyncBucket
FieldType
pathstr
urlstr | None
versioned_urlstr | None
sizeint
etagstr
uploaded_atdatetime, timezone-aware UTC
content_typestr
metadatadict[str, str]

All of them are exported from upstash_blob.