Add resumable multipart uploads for large files

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>
This commit is contained in:
Shaun Kruger
2026-12-01 17:24:52 -07:00
co-authored by Claude Opus 5.5
parent af18031a14
commit 1327f929e1
20 changed files with 2055 additions and 69 deletions
+24
View File
@@ -56,6 +56,9 @@ Settings live in the `privatestudio.settings` package. Choose one with `DJANGO_S
|---|---|---|
| `STORAGE_PATH` | all | File storage root (default `<repo>/storage`) |
| `S3_CREDENTIAL_TTL_HOURS` | all | Lifetime of `/api/s3keys` credentials (default `1`) |
| `UPLOAD_PART_SIZE_MB` | all | Part size for browser uploads (default `16`). Files that would need over 10,000 parts use larger parts |
| `UPLOAD_MAX_SIZE` | all | Largest multipart upload in bytes (default `0`, no limit) |
| `UPLOAD_EXPIRY_DAYS` | all | `cleanup_uploads` removes unfinished uploads idle for this many days (default `7`) |
| `DJANGO_TIME_ZONE` | all | Time zone (default `UTC`) |
| `SECRET_KEY` | production | Django secret key |
| `ALLOWED_HOSTS` | production | Comma-separated host names |
@@ -82,6 +85,11 @@ All endpoints except `/health` need `Authorization: Bearer <API_KEY>`. The S3 en
| Method | Path | Description |
|---|---|---|
| PUT | `/s3/{bucket}/{key}` | Upload: either a raw body, or multipart with a `file` field |
| POST | `/s3/{bucket}/{key}?uploads` | Start a multipart upload |
| PUT | `/s3/{bucket}/{key}?partNumber=N&uploadId=ID` | Upload a part |
| GET | `/s3/{bucket}/{key}?uploadId=ID` | List received parts |
| POST | `/s3/{bucket}/{key}?uploadId=ID` | Complete a multipart upload |
| DELETE | `/s3/{bucket}/{key}?uploadId=ID` | Abort a multipart upload |
| GET | `/s3/{bucket}/{key}` | Download |
| HEAD | `/s3/{bucket}/{key}` | Object size |
| DELETE | `/s3/{bucket}/{key}` | Delete |
@@ -108,6 +116,22 @@ s3 = boto3.client("s3", endpoint_url="http://localhost:8000/s3",
s3.upload_file("video.mp4", "demo", "videos/video.mp4")
```
`upload_file` sends files over 8 MB as a multipart upload. Uploads and downloads are streamed in 1 MB blocks, so memory use does not grow with file size. Signed requests must include `X-Amz-Content-Sha256` in their signed headers (boto3 and other S3 SDKs do), and the body is checked against it. boto3's `aws-chunked` uploads with CRC32 trailers are supported; the `STREAMING-AWS4-HMAC-SHA256-PAYLOAD` signing mode is not.
### Browser uploads
The studio page (`/studio/{bucket}/`) has an uploader for signed-in users. It sends files in parts, three at a time, and retries failed parts. If an upload is interrupted, picking the same file again (same name, size and modification time) sends only the missing parts. It uses these session-authenticated JSON endpoints, which need the `X-CSRFToken` header:
| Method | Path | Description |
|---|---|---|
| POST | `/studio/{bucket}/uploads/` | Start: `{"filename", "folder", "size", "last_modified", "overwrite"}` |
| GET | `/studio/{bucket}/uploads/{id}/` | Status and received part numbers |
| PUT | `/studio/{bucket}/uploads/{id}/parts/{n}/` | A part as the raw request body |
| POST | `/studio/{bucket}/uploads/{id}/complete/` | Join the parts (`{"overwrite": true}` to replace a file created meanwhile) |
| DELETE | `/studio/{bucket}/uploads/{id}/` | Cancel |
Parts are kept in `STORAGE_PATH/.multipart/` until the upload completes, so a file temporarily needs twice its size in free space. Run `python manage.py cleanup_uploads` daily to remove abandoned uploads (see [README_SERVE.md](README_SERVE.md)).
### Transcode profiles and jobs (per studio)
| Method | Path | Body |