Stream uploads and downloads in 1 MB blocks so memory use does not grow with file size, under both WSGI and ASGI. - S3 multipart upload API (create, upload part, list parts, complete, abort), so boto3 upload_file works for files over 8 MB. - Browser uploader on the studio page: sends 16 MB parts three at a time with retries, and resumes when the same file is picked again. Uses session-authenticated, CSRF-protected JSON endpoints under /studio/<bucket>/uploads/. - Request bodies are checked against the signed X-Amz-Content-Sha256 instead of being read into memory; signed requests must include it. boto3's aws-chunked bodies with CRC32 trailers are decoded. - Objects are written to a temporary file and renamed into place, so a failed upload leaves the existing file intact. - Downloads stream with FileResponse, or an async iterator under ASGI. - SigV4 verification uses the encoded path, fixing keys with spaces. - cleanup_uploads command removes abandoned uploads and temp files. - DATA_UPLOAD_MAX_MEMORY_SIZE is capped at 10 MB again. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
101 lines
4.3 KiB
Markdown
101 lines
4.3 KiB
Markdown
# Running and Managing Private Studio
|
|
|
|
Private Studio is a standard Django project. All administration goes through `manage.py`, which uses `privatestudio.settings.dev` unless `DJANGO_SETTINGS_MODULE` is set.
|
|
|
|
## Setup
|
|
|
|
```bash
|
|
python3 -m venv venv
|
|
source venv/bin/activate
|
|
pip install -e ".[dev]"
|
|
python manage.py migrate
|
|
```
|
|
|
|
## Access Model
|
|
|
|
- **Users** are standard `django.contrib.auth` users.
|
|
- **Studios** each own one unique `bucket_name` and point to one Django **Group**.
|
|
- A user can access a studio (its S3 bucket, profiles and jobs) when they are in that studio's group. Superusers can access every studio.
|
|
- **API keys** belong to a user and authenticate as that user. Only a SHA256 hash of each key is stored.
|
|
|
|
## Commands
|
|
|
|
### Create users
|
|
|
|
```bash
|
|
python manage.py createsuperuser # admin user
|
|
python manage.py shell -c "from django.contrib.auth.models import User; User.objects.create_user('alice')"
|
|
```
|
|
|
|
You can also create users in the admin at `/admin/`.
|
|
|
|
### create_studio
|
|
|
|
```bash
|
|
python manage.py create_studio <bucket_name> --group <group> [--name "Display Name"] [--add-user USER ...]
|
|
```
|
|
|
|
This creates the group if it doesn't exist, adds the given users to it, and creates the studio. Bucket names must be lowercase letters, digits and hyphens.
|
|
|
|
### create_key
|
|
|
|
```bash
|
|
python manage.py create_key <username>
|
|
```
|
|
|
|
This prints a new API key. The key is shown only once. Users can also create and revoke their own keys through `/api/keys`.
|
|
|
|
### Run the server
|
|
|
|
```bash
|
|
python manage.py runserver 0.0.0.0:8000
|
|
```
|
|
|
|
In production, set `DJANGO_SETTINGS_MODULE=privatestudio.settings.production` and serve `privatestudio.wsgi:application` or `privatestudio.asgi:application` with your WSGI/ASGI server of choice. See the configuration table in [README.md](README.md).
|
|
|
|
### cleanup_uploads
|
|
|
|
```bash
|
|
python manage.py cleanup_uploads [--days 7] [--dry-run]
|
|
```
|
|
|
|
This removes unfinished uploads with no activity for `--days` (default `UPLOAD_EXPIRY_DAYS`), part directories without an active upload, and temporary files older than a day. Run it daily, for example from cron.
|
|
|
|
## Serving large uploads
|
|
|
|
Uploads and downloads are streamed, but the server in front of Django must allow them:
|
|
|
|
- **WSGI** (e.g. gunicorn): request bodies are read straight from the socket. A slow upload occupies a worker thread, so use threaded workers (`-k gthread --threads 8`) and a `--timeout` longer than the slowest part and the final join of a large file. A chunked request without `Content-Length` (boto3 over HTTPS) needs a server that sets `wsgi.input_terminated`, as gunicorn does; otherwise it is refused with 411.
|
|
- **ASGI** (e.g. uvicorn): Django writes each request body to a temporary file before the view runs. Point `TMPDIR` at a disk with room for the largest single upload. Browser parts are 16 MB, but a plain S3 `PUT` of a whole file needs its full size.
|
|
- **nginx**: set `client_max_body_size` above the part size (or `0` for large single PUTs), use `proxy_request_buffering off` for WSGI so bodies are not buffered twice, and raise `proxy_read_timeout` so completing a large upload is not cut off.
|
|
- **Locking** uses `flock` on files in `STORAGE_PATH`. Run all app servers on the host that owns the storage, or on a filesystem where `flock` works across hosts.
|
|
|
|
## Typical Workflow
|
|
|
|
```bash
|
|
python manage.py migrate
|
|
python manage.py createsuperuser --username admin
|
|
python manage.py shell -c "from django.contrib.auth.models import User; User.objects.create_user('alice')"
|
|
python manage.py create_studio demo --name "Demo Studio" --group demo-team --add-user alice
|
|
python manage.py create_key alice
|
|
python manage.py runserver
|
|
|
|
curl -H "Authorization: Bearer $KEY" http://localhost:8000/api/userinfo
|
|
curl -X PUT -H "Authorization: Bearer $KEY" --data-binary @video.mp4 http://localhost:8000/s3/demo/video.mp4
|
|
curl -H "Authorization: Bearer $KEY" http://localhost:8000/s3/demo
|
|
```
|
|
|
|
## Tests
|
|
|
|
```bash
|
|
pytest # all tests
|
|
pytest privatestudio/studio/tests/test_s3.py -k signature
|
|
pytest --cov=privatestudio
|
|
```
|
|
|
|
## Troubleshooting
|
|
|
|
- **401 Unauthorized**: the API key is wrong or revoked, or the S3 credentials have expired (by default they last 1 hour, set by `S3_CREDENTIAL_TTL_HOURS`).
|
|
- **403 on a bucket**: the user isn't in the studio's group. Add them in the admin.
|
|
- **404 on a bucket**: no studio has that `bucket_name`.
|