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
+48
View File
@@ -1,4 +1,6 @@
"""Django models for the video transcoding service."""
import uuid
from django.conf import settings
from django.contrib.auth.models import Group
from django.db import models
@@ -91,6 +93,52 @@ class UploadedAsset(models.Model):
return f"{self.studio.bucket_name}/{self.s3_path}"
class UploadStatus(models.TextChoices):
ACTIVE = "active", "Active"
COMPLETED = "completed", "Completed"
ABORTED = "aborted", "Aborted"
class MultipartUpload(models.Model):
"""An upload sent in parts, from an S3 client or the browser."""
upload_id = models.UUIDField(default=uuid.uuid4, unique=True, editable=False)
studio = models.ForeignKey(Studio, on_delete=models.CASCADE, related_name="multipart_uploads")
created_by = models.ForeignKey(
settings.AUTH_USER_MODEL, on_delete=models.SET_NULL, null=True, blank=True, related_name="multipart_uploads",
)
key = models.TextField()
filename = models.TextField()
# Browser uploads declare these. S3 clients don't, so they are null for those.
total_size = models.BigIntegerField(null=True, blank=True)
part_size = models.BigIntegerField(null=True, blank=True)
last_modified = models.BigIntegerField(null=True, blank=True)
overwrite = models.BooleanField(default=True)
status = models.CharField(max_length=20, choices=UploadStatus.choices, default=UploadStatus.ACTIVE)
etag = models.CharField(max_length=64, blank=True, default="")
created_at = models.DateTimeField(default=timezone.now)
def __str__(self):
return f"{self.studio.bucket_name}/{self.key} ({self.status})"
class UploadPart(models.Model):
"""A received part of a multipart upload."""
upload = models.ForeignKey(MultipartUpload, on_delete=models.CASCADE, related_name="parts")
part_number = models.PositiveIntegerField()
size = models.BigIntegerField()
etag = models.CharField(max_length=32)
uploaded_at = models.DateTimeField(default=timezone.now)
class Meta:
ordering = ["part_number"]
constraints = [
models.UniqueConstraint(fields=["upload", "part_number"], name="unique_part_per_upload"),
]
def __str__(self):
return f"Part {self.part_number} of {self.upload_id}"
class TranscodeProfile(models.Model):
"""Named encoding profile (YAML definition) belonging to a studio."""
studio = models.ForeignKey(Studio, on_delete=models.CASCADE, related_name="transcode_profiles")