The first version of every upload feature streams the file through the API server. It works in the demo, then production meets a 200 MB video on hotel Wi-Fi: workers pinned for minutes, memory spikes, timeouts. The server was never supposed to be in this path.
Presigned flow
- Client asks your API: “I want to upload
video/mp4, 180 MB.” - API authorizes, then asks S3/R2 for a presigned PUT URL (short expiry, key + content-type pinned).
- Client PUTs the file directly to storage.
- Client tells the API “done”; server verifies with a HEAD and enqueues processing.
const url = await getSignedUrl(s3, new PutObjectCommand({
Bucket: 'uploads',
Key: `u/${userId}/${uuid}.mp4`,
ContentType: 'video/mp4',
ContentLength: declaredSize,
}), { expiresIn: 300, signableHeaders: new Set(['content-type']) })The parts that keep it safe
- Pin content-type and the exact Content-Length when you sign, or someone uploads an .exe named video.mp4 with a 5 GB body.
- Never trust “done”. HEAD the object; verify size and type server-side before it enters your pipeline.
- Keys are yours to structure: prefix by user, never accept client-chosen keys (path traversal by another name).
- Processing (transcode, thumbnail, scan) happens from a queue, not the request.
Your API server goes back to doing what it is good at: signing permissions and recording facts.