Metadata-Version: 2.5
Name: karni-storage
Version: 0.7.0
Summary: 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. Karni is an open-source Django + React framework for building web apps and web games with AI coding agents. Your agent writes only your domain or your game. The API, admin, permissions, React screens and game engine come from packages you upgrade. One rule set covers every coding agent. The build fails when an agent hand-writes what Karni ships.
Project-URL: Homepage, https://github.com/cbsshekhawat18-lab/karni
Project-URL: Documentation, https://cbsshekhawat18-lab.github.io/karni/storage/
Project-URL: Repository, https://github.com/cbsshekhawat18-lab/karni
Project-URL: Issues, https://github.com/cbsshekhawat18-lab/karni/issues
Project-URL: Changelog, https://github.com/cbsshekhawat18-lab/karni/tree/main/docs/migrations
Author-email: Chandrabhan Shekhawat <cbsshekhawat18@gmail.com>
License-Expression: Apache-2.0
License-File: LICENSE
Keywords: django,karni,minio,r2,react,s3,storage,uploads
Classifier: Development Status :: 3 - Alpha
Classifier: Environment :: Web Environment
Classifier: Framework :: Django
Classifier: Framework :: Django :: 5.2
Classifier: Intended Audience :: Developers
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Topic :: Internet :: WWW/HTTP
Classifier: Typing :: Typed
Requires-Python: >=3.12
Requires-Dist: karni[s3]<0.8,>=0.7
Provides-Extra: images
Requires-Dist: karni[images]<0.8,>=0.7; extra == 'images'
Description-Content-Type: text/markdown

# 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](https://img.shields.io/pypi/v/karni-storage)](https://pypi.org/project/karni-storage/) [![License: Apache-2.0](https://img.shields.io/badge/license-Apache--2.0-blue)](https://github.com/cbsshekhawat18-lab/karni/blob/main/LICENSE) [![Docs](https://img.shields.io/badge/docs-online-FB5D08)](https://cbsshekhawat18-lab.github.io/karni/storage/)

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](https://cbsshekhawat18-lab.github.io/karni/storage/).

## Install

```bash
pip install "karni-storage[images]"   # the images extra brings Pillow, for thumbnails
karni add storage
```

Then add the URLs once to `config/urls.py`:

```python
path("api/", include("karni_storage.urls")),
```

and run `make migrate` (the module has one table, its thumbnails).

## Use

```python
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`](https://www.npmjs.com/package/@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`.

## 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.
