Sub topic 5
Presigned URLs and POST policies: granting narrow, short-lived access
This chapter assumes you know HTTP methods and headers, what S3-style object storage is, and how a client talks to an API server. It builds on the previous chapter, where the client uploads straight to storage so your servers stay out of the data path.
The problem they solve
The client needs permission to write to your bucket, but you cannot ship long-lived credentials to a browser. A presigned request solves this. Your server signs a request with its own credentials and hands the result to the client. The client uses it for one narrow job, and it stops working after a short time.
There are two forms, and they give different levels of control.
| Form | HTTP method | Controls you get | Typical use |
|---|---|---|---|
| Presigned URL | PUT (or GET, or `upload_part`) | Exact key, method, expiry, and any signed headers | Simple uploads, multipart parts, downloads |
| Presigned POST policy | POST with multipart/form-data | Everything above, plus size range and field conditions | Browser uploads where you must cap file size |
Presigned POST in practice
A presigned PUT URL cannot limit file size in S3. A POST policy can, so it is the usual choice for untrusted browser uploads. The server builds a policy with conditions, and storage rejects any upload that breaks them.
import boto3
s3 = boto3.client("s3")
post = s3.generate_presigned_post(
Bucket="example-uploads",
Key="incoming/3f2a9c/avatar.jpg",
Fields={"Content-Type": "image/jpeg"},
Conditions=[
{"Content-Type": "image/jpeg"},
["content-length-range", 1, 5 * 1024 * 1024],
],
ExpiresIn=300,
)
# Return post["url"] and post["fields"] to the client.This policy pins the object key and content type, accepts 1 byte to 5 MiB, and expires after five minutes. The server chooses the key. Never let the client choose it, or one user could overwrite another user's file.
The browser then sends the returned fields followed by the file.
async function upload(post, file) {
const form = new FormData();
for (const [name, value] of Object.entries(post.fields)) {
form.append(name, value);
}
form.append("file", file); // the file field must come last
const res = await fetch(post.url, { method: "POST", body: form });
if (!res.ok) {
throw new Error(`Upload failed with status ${res.status}`);
}
}The file field must be last because storage stops reading policy fields once it reaches the file.
Narrowing the grant
Apply these limits to every presigned request.
- Short expiry. Use minutes for single uploads. Allow longer only when needed, such as an hour for multipart parts on slow connections.
- Pinned key. The key is part of the signature, so the URL works for that object only.
- Pinned content type. This stops a client declaring
text/htmlfor a file you expected to be an image. - Size range. Use
content-length-rangein a POST policy. - Least-privilege signer. The credentials that sign the request should be allowed to write only under the upload prefix, such as
incoming/. A presigned request cannot exceed the permissions of its signer.
Common errors
- Expiry exceeds credential lifetime. When you sign with temporary credentials, such as an IAM role, the request stops working when those credentials expire, even if
ExpiresInis longer. - Wrong field order. Putting
filebefore the policy fields makes storage reject the upload. - Header mismatch. For presigned PUT, any signed header, such as
Content-Type, must be sent with exactly the same value. - Trusting the grant alone. A policy checks the declared type, not the content. You still validate the file afterward, which later chapters cover.
- Logging URLs. A presigned URL is a bearer token until it expires. Keep it out of logs and analytics.
Exercise
A browser uploads avatars using a presigned PUT URL. A user sends a 2 GB file. Why can't you stop this with the URL alone, and what should you switch to?
Show answer
A presigned PUT URL in S3 has no way to limit the body size. Switch to a presigned POST policy with a content-length-range condition, such as 1 byte to 5 MiB, so storage rejects larger files.