Sub topic 1
Designing File Uploads and Media Delivery
Prerequisites
Assume you know HTTP methods, headers, status codes, and the roles of a browser, API server, database, and object storage.
Start from Requirements
Choose an upload design from the file and trust requirements, not from habit.
| Requirement | Design question |
|---|---|
| File size | Can the API server hold the request and its bandwidth cost? |
| File type | Which types are allowed, and how will you verify them? |
| Latency | Must the user wait for processing before continuing? |
| Trust | Is the file untrusted until scanning and validation finish? |
Small profile images may pass through an API server. Large videos usually should not. The server also needs a plan for failed connections, duplicate uploads, storage limits, and abandoned files.
The Simple Path: Upload Through Your API
In a proxied upload, the browser sends a multipart/form-data request to your API. The API authenticates the user, checks the request, and streams the file to object storage.
This path gives the server complete control. It is useful when files are small, validation must happen immediately, or the application needs to transform the data before storing it. Its cost is that your server handles every byte. Large files consume connections, bandwidth, memory if buffering occurs, and operational capacity.
A safe server should stream rather than load the entire file into memory. It should reject oversized requests before storing them and create a database record that tracks the upload state.
Object Storage and Key Design
Object storage keeps files as objects addressed by keys. It scales better for media than a database filesystem column and usually integrates with CDNs and background processors.
Use keys that avoid user-controlled names and collisions. A useful pattern is:
tenant-id/random-id/original-extension
Keep the display filename in the database. Treat it as metadata, not as the storage identity. A random identifier also prevents one user from overwriting another user’s object.
Direct Uploads
In a direct upload, your API authorizes an upload and returns a short-lived, narrowly constrained permission. The client then sends the file directly to object storage. Your API stays out of the data path, reducing server bandwidth and improving large-file throughput.
The authorization should bind the request to a specific key, size limit, and expected type. Store the upload as untrusted until later validation and scanning.
Exercise
When is a proxied upload usually preferable to a direct upload?
Show answer
When files are small or the server needs immediate, close control over validation or transformation.