Tutorial

S3 Presigned Upload

Let the browser upload straight to S3 through a presigned URL so your server never proxies the bytes, with a short-lived PUT and a content-type constraint.

On this page

  1. Introduction
  2. Mint the URL server-side
  3. PUT from the browser
  4. Lock the constraints
  5. Confirm the upload
  6. Short expiry
  7. Test the constraints
  8. Sources

Introduction

This guide covers presigned S3 uploads direct from the browser, a recurring concern in the file-upload scenario. Proxying uploads through your server doubles your bandwidth and ties up a worker for the whole transfer. A presigned URL lets the browser PUT the file straight to S3, and the signature can lock the content type and size so the client cannot upload something unexpected.

Mint the URL server-side

Have your server sign a PUT URL for the given key and content type, so the browser never sees credentials and the signature carries the constraints you want enforced.

The trade-off worth naming is that have your server sign a put url for the given key and content type, so the browser never sees credentials and the signature carries the constraints you want enforced adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

import { S3Client, getSignedUrl } from '@aws-sdk/client-s3';
import { PutObjectCommand } from '@aws-sdk/client-s3';
export async function sign(key, type) {
  const cmd = new PutObjectCommand({ Bucket: 'b', Key: key, ContentType: type });
  return getSignedUrl(new S3Client(), cmd, { expiresIn: 300 });
}

PUT from the browser

Send the file with a PUT to the signed URL and the exact content type, so the signature matches and S3 accepts the upload without your server touching the bytes.

The trade-off worth naming is that send the file with a put to the signed url and the exact content type, so the signature matches and s3 accepts the upload without your server touching the bytes adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

async function upload(url, file) {
  const r = await fetch(url, { method: 'PUT', body: file, headers: { 'Content-Type': file.type } });
  if (!r.ok) throw new Error('upload failed: ' + r.status);
}

Lock the constraints

Include content type and a max size in the signed conditions so the client cannot upload a different type or more bytes than you allowed, which is the security point of presigning.

The trade-off worth naming is that include content type and a max size in the signed conditions so the client cannot upload a different type or more bytes than you allowed, which is the security point of presigning adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

Confirm the upload

After the PUT, do a HEAD or a server-side check to confirm the object exists with the right size, so a client claiming success cannot lie to your database.

The trade-off worth naming is that after the put, do a head or a server-side check to confirm the object exists with the right size, so a client claiming success cannot lie to your database adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

Short expiry

Set the signature to a few minutes so a leaked URL is not reusable for long, and mint a fresh one on retry, which keeps the door narrow without breaking slow uploads.

The trade-off worth naming is that set the signature to a few minutes so a leaked url is not reusable for long, and mint a fresh one on retry, which keeps the door narrow without breaking slow uploads adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

Test the constraints

Assert a PUT with the wrong content type is rejected by S3 and a PUT over the size limit fails, so the signature actually enforces what you signed.

The trade-off worth naming is that assert a put with the wrong content type is rejected by s3 and a put over the size limit fails, so the signature actually enforces what you signed adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

Sources

Sources: S3 presigned PUT, AWS SDK v3.

FAQ

How long should a presigned URL live?
A few minutes; long enough for a slow upload to finish, short enough that a leaked URL is not reusable for long, and you can always mint a fresh one on retry.
Can I lock the content type in the signature?
Yes; include Content-Type in the signed headers so the PUT must send the same type, which stops a client from uploading an executable as an image.

Related Examples