Sub topic 2
The simple path: multipart uploads through your API server
This chapter assumes you know HTTP methods, headers, and status codes, and that you have written a server endpoint in one language. It also assumes you know roughly what object storage such as Amazon S3 is.
What a proxied upload is
In a proxied upload, the browser sends the file to your API server, and the server forwards it to storage. The browser sends it as a multipart/form-data request. This is the same encoding an HTML <form enctype="multipart/form-data"> produces. The body is split into parts by a boundary string. Each part has its own headers, such as Content-Disposition with the field name and file name, and its own Content-Type.
A minimal example
This Flask endpoint accepts an avatar, checks it, and streams it to S3.
import uuid
import boto3
from flask import Flask, jsonify, request
app = Flask(__name__)
app.config["MAX_CONTENT_LENGTH"] = 5 * 1024 * 1024 # larger bodies get 413
s3 = boto3.client("s3")
ALLOWED = {"image/jpeg", "image/png"}
@app.post("/avatar")
def upload_avatar():
file = request.files.get("file")
if file is None:
return jsonify(error="missing file"), 400
if file.mimetype not in ALLOWED:
return jsonify(error="unsupported type"), 415
key = f"incoming/{uuid.uuid4().hex}/avatar"
s3.upload_fileobj(
file.stream,
"example-uploads",
key,
ExtraArgs={"ContentType": file.mimetype},
)
return jsonify(key=key), 201The server picks the storage key itself and never uses the client's file name in it. upload_fileobj reads the stream in chunks, so the server does not hold the whole file in memory.
Why it is a good starting point
- One request, one place to enforce rules. Authentication, size limits, and validation all live in code you already own.
- No browser-side storage logic. The client needs no credentials, no policy handling, and no second request.
- Easy to test. It is a normal endpoint.
For small files such as avatars, this is often the right choice.
Where it breaks down
Every byte passes through your server, so the costs grow with file size and traffic.
| Problem | Effect |
|---|---|
| Worker occupied during the transfer | A slow mobile client holds a worker or connection for the whole upload. |
| Bandwidth paid twice | The bytes travel client to server, then server to storage. |
| Request timeouts | Load balancers and proxies often cut off long requests. |
| No resume | A dropped connection at 95% means starting again. |
| Body limits | Proxies and frameworks cap request size, and raising the cap invites abuse. |
Mistakes to avoid
- Trusting the declared type.
file.mimetypecomes from the client. It is a hint, not proof. Later chapters cover checking content signatures. - Buffering whole files. Reading
file.read()into memory lets a few large uploads exhaust RAM. Stream instead. - Enforcing limits late. Set the body limit at the framework or proxy so oversized requests are rejected before the server reads them all.
- Using client file names as keys. They can contain path tricks or collide with other users' files.
Exercise
A proxied upload endpoint works well in testing but fails for users on slow mobile networks uploading 200 MB videos. Name two causes tied to the proxied design.
Show answer
The upload ties up a server worker and connection for the whole slow transfer, so a few clients can exhaust capacity. Long requests may also hit proxy or load balancer timeouts and body-size limits, and a dropped connection forces the whole upload to restart because there is no resume.