Sub topic 1
Start from requirements: file sizes, types, latency, and trust
Most upload designs go wrong because the team picked a mechanism first (a form, a library, a storage service) and discovered the requirements later. This chapter works in the opposite order. You will turn vague goals like "users can upload photos" into concrete numbers and constraints. Those numbers decide which architecture the rest of this tutorial recommends.
Prerequisites
This chapter assumes you already know:
- HTTP basics: methods, headers, and status codes such as 200, 403, and 413.
- How a web application is split into a client, an API server, and a database.
- One server-side language and browser JavaScript, enough to read short code examples.
- What object storage such as Amazon S3 is: a service that stores files as named objects in buckets and serves them over HTTP.
You do not need to know presigned URLs, CDNs, or video encoding yet. Later chapters introduce them.
Why requirements come first
An upload feature has two halves. The upload path moves bytes from a client to your storage. The delivery path moves bytes from storage to viewers. Both halves are shaped by the same four questions:
- How big are the files?
- What types of files will you accept?
- How fast must uploads and views feel?
- How much do you trust the people uploading, and the people viewing?
Each answer rules out some designs. A profile-photo feature and a video-sharing product both "accept uploads", but they should not share an architecture.
Question 1: file sizes
Size is the strongest driver because it determines what a single HTTP request can reasonably carry. Write down three numbers, not one:
- Typical size. The median upload, for example 2 MB for a phone photo.
- Maximum size. The largest file you will accept, for example 5 MiB for avatars or 5 GiB for video.
- Total volume. Uploads per day multiplied by typical size. This drives storage and bandwidth cost.
Small files (under a few MB) can survive a single request and a simple retry. Large files change the problem:
- A single request of several GB can take minutes or hours on a slow connection. If the connection drops at 95%, the user starts over.
- Web servers, load balancers, and proxies have request body limits and timeouts. A request that exceeds them fails with a 413 or a gateway timeout, often with an unhelpful error.
- Holding a large body in memory or on local disk on your API server ties up resources for the whole transfer.
As a rough rule, anything over about 100 MB should be planned as a chunked, resumable upload. Chapter 7 covers the mechanics. Here you only need to record whether you need it.
Question 2: file types
List the types you accept, and be specific. "Images" is not a requirement. "JPEG, PNG, and WebP, up to 5 MiB, up to 8000 pixels on the longest side" is.
An allowlist is easier to defend than a blocklist. You name what you accept and reject everything else. Different types carry different risks and different processing needs:
| Type | Typical risk | Typical processing |
|---|---|---|
| Raster images (JPEG, PNG, WebP) | Mislabeled files, oversized dimensions, metadata with GPS location | Resize, strip metadata, convert to modern formats |
| SVG | Can contain scripts, so it is executable content | Sanitize, or serve only as a download |
| PDF and office documents | Embedded scripts, malware | Scan, often serve as download |
| Video | Very large, expensive to process | Transcode to several bitrates, segment for streaming |
| Archives (ZIP) | Zip bombs, path traversal on extraction | Usually reject unless a feature needs them |
Note that the type a client declares (the Content-Type header or the file extension) is only a claim. Your design must decide where the claim gets checked against the actual bytes. Chapter 6 shows how.
Question 3: latency and user experience
"Latency" here means two different things, and you should set a target for each.
- Upload latency. How long the user waits before they can continue. If a user uploads an avatar, they expect to see it within a second or two of the upload finishing. For a 4K video they will accept a progress bar and an email later.
- Delivery latency. How long a viewer waits for the first byte and for the full file. Viewers are spread across the world, and distance to your storage region adds delay on every request.
A useful distinction is synchronous versus asynchronous completion. Synchronous means the upload is usable the moment the request returns. Asynchronous means the file is accepted first and processed later, so the UI shows a "processing" state. Resizing a small image can be synchronous. Transcoding video cannot. Deciding this early shapes your API: an asynchronous design needs a status field on the record and a way to tell the client when processing finishes.
Also record the network conditions. Mobile users on unstable connections need retry and resume behavior. Office users on fast wired connections may not.
Question 4: trust
Trust has two sides.
Trust in the uploader. Treat every uploaded file as hostile until proven otherwise. A signed-in employee uploading to an internal tool is a lower risk than anonymous users on a public site, but neither is zero risk, because accounts get compromised. Ask:
- Who is allowed to upload? Anonymous users, signed-in users, or a specific role?
- How many files and how many bytes may one user upload per day? Without a quota, one account can fill your bucket.
- Will other users view this content? If so, a malicious file becomes an attack on your other users, not only on your servers.
Trust in the viewer. Decide who may fetch each file. Public marketing images can be cached by anyone. A private invoice must be visible only to its owner. These two cases need different delivery designs, covered in chapters 14 and 15.
From answers to architecture
The answers feed a few decisions. The diagram shows a simplified version of how the upload path forks.
Read the diagram as a starting point, not a rule. A small internal tool with ten users may use a proxied upload even for 50 MB files, because simplicity beats scale. A public product with millions of users should not.
Capturing requirements in a checklist
Write the answers down in a form your team can review and your code can use. A short, executable specification also doubles as the source of truth for server-side checks later:
from dataclasses import dataclass
@dataclass(frozen=True)
class UploadPolicy:
name: str
allowed_types: tuple
max_bytes: int
needs_resumable: bool
processed_async: bool
public_delivery: bool
AVATAR = UploadPolicy(
name="avatar",
allowed_types=("image/jpeg", "image/png"),
max_bytes=5 * 1024 * 1024,
needs_resumable=False,
processed_async=False,
public_delivery=True,
)
VIDEO = UploadPolicy(
name="video",
allowed_types=("video/mp4",),
max_bytes=5 * 1024 ** 3,
needs_resumable=True,
processed_async=True,
public_delivery=False,
)
for policy in (AVATAR, VIDEO):
print(policy.name, policy.max_bytes, policy.needs_resumable)Keeping one policy object per upload kind means the limits you show in the UI, enforce on the server, and encode in storage policies come from the same place.
Common mistakes at the requirements stage
- One policy for everything. Using the video limits for avatars wastes money, and using avatar limits for video blocks real users. Define a policy per upload kind.
- Ignoring the delivery side. Teams design uploads carefully, then discover that serving the files from the origin is slow and expensive.
- Forgetting failure. Decide now what happens when an upload is abandoned halfway, or when processing fails. Abandoned data costs money until you clean it up (chapter 16).
- Treating "trusted users" as a security boundary. Validate even for logged-in users.
- No numbers. "Large files" and "fast" cannot be tested. "5 GiB" and "first byte under 200 ms" can.
Exercise
A photo-sharing app lets signed-in users upload JPEG and PNG photos up to 20 MB each. Users are mostly on mobile networks, and photos are visible to anyone with the link. Using the four questions from this chapter, write one requirement for each (size, type, latency, trust). Then say whether you would plan for resumable uploads.
Show answer
Size: typical about 3 MB, maximum 20 MB, with total volume estimated from uploads per day. Type: allowlist of image/jpeg and image/png, checked against the file's actual bytes, not only the declared type. Latency: the upload should feel complete within a few seconds on a good connection, thumbnails can be produced asynchronously, and delivery should come from a cache close to viewers. Trust: only signed-in users may upload, with a per-user daily quota, and files are treated as hostile until validated. Resumable uploads are optional at 20 MB, but mobile networks make retry behavior valuable, so plan at least automatic retry of the whole upload and consider chunking if failure rates are high.