AlgoViz

File upload APIs

medium 26 min

Small 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

Our systemPOST /uploadcopyClientUpload APIObject storage

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, 62, 73, 84, 95, 10
Chunks 1-5donedonedonedonedone
Chunks 6-10donewaitingdonedonedone

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

    critical

    Ten people uploading 500 MB need 5 GB of memory. The server falls over long before the disk is full.

    Wrong
    data = request.read()storage.put(key, data)
    Right
    for piece in request.stream(64_000):    writer.write(piece)
  • Trusting the name and type from the client

    critical

    A name like ../../app.py can write outside the folder, and a program can claim to be a picture.

    Wrong
    open("/uploads/" + filename, "wb")
    Right
    key = str(uuid4())kind = detect_from_bytes(head)
  • A signed URL without limits

    major

    A plain signed PUT does not limit size unless the length is signed. A link valid for days can be shared and used by anyone.

    Wrong
    sign_put(key, expires=604800)
    Right
    sign_put(key, kind, size, expires=900)# or a POST policy with a size range
  • Keeping upload sessions in one server's memory

    major

    The next chunk may reach another server, or the server restarts. The session must live in a shared store.

    Wrong
    sessions[upload_id] = {...}   # a dict
    Right
    db.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.