File upload APIs
medium 26 minSmall files go up in one multipart request.
See it run
One request, chunks, resuming and signed URLs: what each costs and what happens when the connection drops.
Operations
One request
A form post with multipart/form-data: the simplest upload, and the one that starts again from zero.
Step 1 of 4
One body, several parts
The boundary separates the parts: here a text field and a file. It must not occur inside the data.
Step 1: One body, several parts
POST /uploadContent-Type: multipart/form-data; boundary=XYZ--XYZContent-Disposition: form-data; name="description"My holiday photo--XYZContent-Disposition: form-data; name="file"; filename="photo.jpg"Content-Type: image/jpeg(binary data)--XYZ--- After a drop
- Start again
- Requests
- 1
- Good for
- files below about 10 MB
The server does not have to hold the whole file in memory. Good parsers stream it to disk or to storage.
In the real world
The same file upload apis doing real jobs. Pick one and step through it.
The client says complete, but chunk 7 never came.
Step 1 of 3
Complete, says the client
| 1, 6 | 2, 7 | 3, 8 | 4, 9 | 5, 10 | |
|---|---|---|---|---|---|
| Chunks 1-5 | done | done | done | done | done |
| Chunks 6-10 | done | waiting | done | done | done |
complete?
It lost track of chunk 7 after a crash.
Step 1: Complete, says the client
Common mistakes
The bugs interviewers watch for, with the fix beside each.
Reading the whole file into memory
criticalTen people uploading 500 MB need 5 GB of memory. The server falls over long before the disk is full.
Wrongdata = request.read()storage.put(key, data)Rightfor piece in request.stream(64_000): writer.write(piece)Trusting the name and type from the client
criticalA name like ../../app.py can write outside the folder, and a program can claim to be a picture.
Wrongopen("/uploads/" + filename, "wb")Rightkey = str(uuid4())kind = detect_from_bytes(head)A signed URL without limits
majorA plain signed PUT does not limit size unless the length is signed. A link valid for days can be shared and used by anyone.
Wrongsign_put(key, expires=604800)Rightsign_put(key, kind, size, expires=900)# or a POST policy with a size rangeKeeping upload sessions in one server's memory
majorThe next chunk may reach another server, or the server restarts. The session must live in a shared store.
Wrongsessions[upload_id] = {...} # a dictRightdb.insert("uploads", upload_id, ...)
Interview tips
How to explain it
- Small file: one request.
- Big file: numbered pieces that can be retried.
- Bad network: ask the server how far it is, then continue.
- Many files: signed URLs, so bytes skip our servers.
Memory trick: MCPR
- M: multipart, the simple form.
- C: chunked, split and send in parallel.
- P: pausable, resume after a failure.
- R: redirect, straight to the cloud.
Always say
- Size limit and allowed types.
- Checksum per piece.
- Scan before publishing.
- Clean up unfinished uploads.
Numbers to know
- S3 parts: 5 MiB to 5 GiB, the last may be smaller.
- S3: up to 10,000 parts.
- 5 to 10 MB is a good chunk for most networks.
- Signed URL: 5 to 60 minutes.