Skip to main content

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.

PyPI License: Apache-2.0 Docs

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

  • ProtectedFileField and ProtectedImageField, 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, b2 or gcs-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_URL wins over a preset's endpoint, AWS_S3_ADDRESSING_STYLE over its addressing style, KARNI_STORAGE_UPLOAD_METHOD over its upload method.
  • storage= on one field, thumbnails=False on an image field.
  • KARNI_THUMBNAILER (core) replaces the thumbnail rendering.
  • The row's viewset decides who may download and upload: its access.
  • karni_storage.views.serve(name) hands out a file to code that checked access itself (a paid download).
  • A path() of your own before the include replaces one endpoint.

Admin

No admin.py: the thumbnails' admin is derived from AdminMeta.

Metadata

Release files for karni-storage 0.6.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for karni-storage 0.6.0
File Size Uploaded Provenance Actions
karni_storage-0.6.0.tar.gz 36.9 kB Without provenance Details for karni_storage-0.6.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for karni-storage 0.6.0
File Interpreter ABI Platform Actions
karni_storage-0.6.0-py3-none-any.whl Python 3 none any Details for karni_storage-0.6.0-py3-none-any.whl

Total release size: 85.1 kB

Release files / karni_storage-0.6.0.tar.gz

Download URL karni_storage-0.6.0.tar.gz
Size 36.9 kB
Tags Source
SHA-256 checksum
How to use checksums
64fb2a2b9be2851e1d679854518361daa773d859265831ef74dadb1b9808c0d0
BLAKE2b-256 checksum
How to use checksums
725055d92cc752cb8a06575ee7b7d30f3513a333b2bc5088116fccc8712aa656
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.6.0-py3-none-any.whl

Download URL karni_storage-0.6.0-py3-none-any.whl
Size 48.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
eb9aea629550a8c3b050ac07a1d7bfa3d6b67fb4dc62d841e902a05256e15d79
BLAKE2b-256 checksum
How to use checksums
e338e9391bfb95fa5f4a72b8e1ffe0a685941669a94933b415ca5f37590a82ea
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.6.0

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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page