karni-storage
Protected files for Karni: files a row owns, in S3 or an S3-compatible service, served only to whoever may read that row, with direct uploads and thumbnails.
Files are kept in S3 or an S3-compatible service (or on disk in development) and handed only to whoever may read their row. It is experimental: its names may change in a minor release, with a CHANGELOG entry.
The full guide is docs/storage.md.
Install
pip install "karni-storage[images]" # the images extra brings Pillow, for thumbnails
karni add storage
Then add the URLs once to config/urls.py:
path("api/", include("karni_storage.urls")),
and run make migrate (the module has one table, its thumbnails).
Use
from karni_storage.fields import ProtectedFileField, ProtectedImageField
class Product(BaseModel):
manual = ProtectedFileField(upload_to="products/manuals/", blank=True,
max_size=5 * 1024 * 1024, content_types=["application/pdf"])
photo = ProtectedImageField(upload_to="products/photos/", blank=True)
In karni.yaml the same fields are manual: "file:protected?" and
photo: "image:protected?".
The API gives manual as /api/products/<id>/files/manual/. A GET there
retrieves the row through ProductViewSet first: 401 or 403 from its gate,
404 for another user's row, a soft-deleted row or a missing one. Then it
redirects to a signed URL in the bucket (KARNI_STORAGE=s3) or streams the
file (KARNI_STORAGE=local). ?size=small serves a thumbnail of an image.
Main features
ProtectedFileFieldandProtectedImageField, downloaded only through the row's own viewset- Short-lived signed URLs, or the file streamed
- One setting for the service:
aws,r2,minio,spaces,b2orgcs-interop - Direct uploads with a presigned POST or PUT, and a confirm step before a row points at the file
- Thumbnails made by a Celery task
- React components in
@karni/storage-ui
Settings
| Setting | Env | Default | Meaning |
|---|---|---|---|
KARNI_STORAGE_PROVIDER |
same | aws |
The service: aws, r2, minio, spaces, b2, gcs-interop. Sets the endpoint, region and addressing style |
KARNI_STORAGE_CREDENTIALS |
same | keys |
keys (AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY) or chain (boto3's own: an AWS role or profile) |
KARNI_STORAGE_BUCKET |
same | "" |
The bucket of protected files. Empty: AWS_STORAGE_BUCKET_NAME |
KARNI_STORAGE_LOCATION |
same | protected |
The key prefix inside the bucket |
KARNI_STORAGE_ROOT |
same | "" |
The local directory. Empty: protected next to MEDIA_ROOT |
KARNI_STORAGE_URL_EXPIRY |
same | 300 |
Seconds a signed download URL lives |
KARNI_STORAGE_UPLOAD_EXPIRY |
same | 900 |
Seconds a presigned upload lives |
KARNI_STORAGE_UPLOAD_METHOD |
same | "" |
post or put. Empty: the preset's |
KARNI_STORAGE_MAX_SIZE |
same | 10485760 |
The size limit of a field without max_size |
KARNI_STORAGE_THUMBNAIL_SIZES |
same (JSON) | {"small": [160, 160], "medium": [640, 640]} |
Thumbnail names and bounding boxes |
KARNI_STORAGE_THUMBNAIL_FORMAT |
same | WEBP |
The thumbnails' format |
KARNI_STORAGE_THROTTLE_RATES |
same (JSON) | {"download": "1000/hour", "upload": "120/hour", "confirm": "120/hour"} |
Rate limits per caller, scopes storage.download, storage.upload, storage.confirm. A rate under the scope in REST_FRAMEWORK["DEFAULT_THROTTLE_RATES"] wins |
KARNI_STORAGE_ALIAS |
same | protected |
A STORAGES alias that, when defined, replaces the module's storage |
R2_ACCOUNT_ID |
same | "" |
Cloudflare R2's account id (the r2 preset builds its endpoint from it) |
Credentials stay in the environment: AWS_ACCESS_KEY_ID,
AWS_SECRET_ACCESS_KEY, AWS_STORAGE_BUCKET_NAME, AWS_S3_REGION_NAME and,
for R2, R2_ACCOUNT_ID. karni check fails (STORE001 to STORE006) on an
unknown preset, missing credentials, a missing region, account id, endpoint
or bucket, and a missing URL include.
Escape hatches
STORAGES["protected"]replaces the module's storage with any backend.AWS_S3_ENDPOINT_URLwins over a preset's endpoint,AWS_S3_ADDRESSING_STYLEover its addressing style,KARNI_STORAGE_UPLOAD_METHODover its upload method.storage=on one field,thumbnails=Falseon an image field.KARNI_THUMBNAILER(core) replaces the thumbnail rendering.- The row's viewset decides who may download and upload: its
access. - A
path()of your own before the include replaces one endpoint.
Admin
No admin.py: the thumbnails' admin is derived from AdminMeta.
Links
- Protected files: https://cbsshekhawat18-lab.github.io/karni/storage/
- React components: https://www.npmjs.com/package/@karni/storage-ui
- Karni, the framework this package is part of: https://github.com/cbsshekhawat18-lab/karni
- Changelog: https://github.com/cbsshekhawat18-lab/karni/blob/main/CHANGELOG.md
- Issues: https://github.com/cbsshekhawat18-lab/karni/issues
- License: Apache-2.0. karni-storage is released with every other Karni package, at one version.
Metadata
Release files for karni-storage 0.5.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| karni_storage-0.5.1.tar.gz | 36.5 kB | Without provenance | Details for karni_storage-0.5.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| karni_storage-0.5.1-py3-none-any.whl | Python 3 | none | any | Details for karni_storage-0.5.1-py3-none-any.whl |
Total release size: 84.4 kB
Release files / karni_storage-0.5.1.tar.gz
| Download URL | karni_storage-0.5.1.tar.gz |
|---|---|
| Size | 36.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3504f8825a6430cc68147c8b446c98b49c1ec7e5b77e31b1a3b74bba22bf7c19
|
|
BLAKE2b-256 checksum How to use checksums |
48788437ab2c22bf8e4472b52959973fa811720f24d0598c70894f956f203403
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Release files / karni_storage-0.5.1-py3-none-any.whl
| Download URL | karni_storage-0.5.1-py3-none-any.whl |
|---|---|
| Size | 47.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
b32a1275926932e352c1be9ca4576311675622b12510e8ee8c6113ae0bdca0af
|
|
BLAKE2b-256 checksum How to use checksums |
565ce1160edadec3a81b14796a3c61f011fc660674497cece3ea434c7c03d1a5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Security for karni-storage 0.5.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.