Writing
Fullstack English

File Uploads That Scale: Presigned URLs to S3/R2

Your API server should never proxy a 200 MB video. Presigned PUTs, content-type pinning, and the async processing pipeline after.

  • Published
  • 1 min read

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

  1. Client asks your API: “I want to upload video/mp4, 180 MB.”
  2. API authorizes, then asks S3/R2 for a presigned PUT URL (short expiry, key + content-type pinned).
  3. Client PUTs the file directly to storage.
  4. Client tells the API “done”; server verifies with a HEAD and enqueues processing.
typescript
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.