s3dol.uploads¶
Direct-to-S3 browser uploads: a presigned POST scoped to a key prefix.
A browser can upload straight to S3 without the server relaying bytes: the server issues a presigned POST form whose signed policy pins
the exact key (server-generated
<prefix><random token>: the client never picks a filename, so it can neither overwrite a sibling object nor smuggle..segments; a form holds one object, and on an unversioned bucket re-posting with it only overwrites that object, so the byte budget cannot be multiplied; see “Versioned buckets” below),a content-length range (
min_bytes..``max_bytes``),the content type (optional), and
an expiry (the 7-day SigV4 cap; an STS session’s own expiry still ends it sooner).
Before signing, an optional budget callable is asked to reserve the bytes
(budget(max_bytes) raises UploadBudgetExceeded to refuse, and may return a release() callable that is called if signing fails), so a
daily per-tenant byte budget is enforced before any form exists.
DailyByteBudget is the ready-made one; any callable works.
Versioned buckets. The budget caps what one form can store only when the
bucket keeps one version per key. With versioning Enabled, every re-post of
the same form within its expiry stores a new full version, so one reservation
of max_bytes can buy N * max_bytes of storage. Use an unversioned
bucket for uploads, or one with a lifecycle rule that expires noncurrent
versions promptly. When a budget is given, presigned_upload() asks
the bucket for its versioning status and, per on_versioned_bucket, warns
(the default, VersionedBucketWarning), refuses
(ConfigurationError) or skips the check ('ignore', no extra call).
The function is host-agnostic: it takes the store, the prefix and the limits as arguments, and the web app mounts it behind its own auth:
form = s3dol.presigned_upload(
store, key_prefix='tenant-7/', max_bytes=5_000_000, content_type='image/png')
form.url, form.fields # hand both to the browser's <form>/FormData
>>> from s3dol.uploads import DailyByteBudget
>>> budget = DailyByteBudget(100, today=lambda: '2026-01-01')
>>> _ = budget(60); _ = budget(40)
>>> budget(1)
Traceback (most recent call last):
...
s3dol.uploads.UploadBudgetExceeded: daily upload budget exceeded: 100 of 100 bytes already reserved, 1 requested
Module Attributes
long enough to pick a file, short enough to not be a standing upload credential. |
|
What |
Functions
|
A presigned POST form for ONE new object under |
Classes
|
A per-day byte budget (one instance per tenant); call it to reserve. |
|
What the browser needs: POST |
Exceptions
The tenant's byte budget cannot cover this upload; no form was signed. |
|
A budgeted upload form was signed for a bucket with versioning enabled. |
- s3dol.uploads.DEFAULT_UPLOAD_EXPIRES_IN = 900¶
long enough to pick a file, short enough to not be a standing upload credential.
- Type:
Default form lifetime (seconds)
- class s3dol.uploads.DailyByteBudget(limit_bytes, *, ledger=None, today=<function DailyByteBudget.<lambda>>)[source]¶
Bases:
objectA per-day byte budget (one instance per tenant); call it to reserve.
ledgeris anyMutableMapping[str, int](default: in-memory) keyed by day, so persistence is a one-argument swap (adolstore, a file…).todayis the clock seam. Reservation is optimistic (the maximum the form allows), and a form holds exactly one key, so re-posting cannot multiply the budget on an unversioned bucket (on a versioned one each re-post stores a new version: see the module docstring).
- s3dol.uploads.ON_VERSIONED_BUCKET_CHOICES = ('warn', 'raise', 'ignore')¶
What
presigned_upload()does when a budget is given and the bucket has versioning enabled: warn, refuse, or skip the check (no API call).
- class s3dol.uploads.PresignedUpload(url, fields=dict(), key='', key_prefix='', max_bytes=0)[source]¶
Bases:
objectWhat the browser needs: POST
fields+ afilepart tourl.
- exception s3dol.uploads.UploadBudgetExceeded[source]¶
Bases:
S3ErrorThe tenant’s byte budget cannot cover this upload; no form was signed.
- exception s3dol.uploads.VersionedBucketWarning[source]¶
Bases:
S3DolWarningA budgeted upload form was signed for a bucket with versioning enabled.
- s3dol.uploads.presigned_upload(store, *, key_prefix, max_bytes, content_type, min_bytes=0, expires_in=900, budget=None, on_versioned_bucket='warn')[source]¶
A presigned POST form for ONE new object under
key_prefix.content_typeis required: pass the one type to accept, orNoneto explicitly accept any (then a holder can storetext/html: do not serve such objects from a trusted origin).key_prefixmust end with the store’s delimiter (tenant-7/cannot matchtenant-77/) and is in the store’s key space (the store’s own prefix is prepended, like every other s3dol key). The object key is server-generated (PresignedUpload.key). ReturnsNonefor an anonymous store (no signing capability, ass3dol.urls()does).When
budgetis given, the bucket’s versioning status is read first (oneGetBucketVersioningcall): with versioning enabled a re-posted form stores a new version each time, soon_versioned_bucketdecides:'warn'(default) emitsVersionedBucketWarning,'raise'refuses withConfigurationError,'ignore'skips the call.Raises
ConfigurationErrorfor a bad prefix, size range,expires_inoron_versioned_bucket, andUploadBudgetExceededifbudgetrefuses.- Return type: