The safest way to accept user files is an S3 presigned URL upload. Your server checks the user and signs a short-lived URL. The client then sends the file straight to S3. Your API never handles the bytes, and the URL is useless after a few minutes.

This tutorial covers the full flow for web and mobile. It also covers the two mistakes that cause most production failures and how to test for them.

The problem and the pattern

Proxying uploads through your API wastes bandwidth and ties up workers for the length of every slow mobile connection. Making the bucket writable by clients is worse, because anyone with the bucket name can fill it.

A presigned URL sits between the two. It grants one action, like PUT on one key, for a fixed time, using your server's credentials. AWS documents the mechanics in its presigned URL guide.

If you'd rather have a team that has shipped this flow wire it into your app, we build this for clients — ask us about yours.

Prerequisites

  • An S3 bucket with Block Public Access turned on, which is the default for new buckets.
  • A backend in Node 18 or later, using AWS SDK for JavaScript v3.
  • An IAM role for that backend with s3:PutObject on the upload prefix only.
  • An existing auth system, so you know which user is asking.

How to build an S3 presigned URL upload in five steps

Step 1: Scope the bucket and the role

Grant the server role s3:PutObject on arn:aws:s3:::my-app-uploads/uploads/* and nothing broader. A presigned URL carries the permissions of whoever signed it. A broad role means a broad URL.

New objects are encrypted with SSE-S3 by default. Add a lifecycle rule to delete anything under a pending/ prefix after one day, so abandoned uploads don't pile up.

Step 2: Sign the URL on your server

Install @aws-sdk/client-s3 and @aws-sdk/s3-request-presigner. Then add an authenticated endpoint like this:

const allowed = ['image/jpeg', 'image/png', 'application/pdf']; if (!allowed.includes(contentType)) return res.status(400).end(); const key = `pending/${user.id}/${crypto.randomUUID()}`; const cmd = new PutObjectCommand({ Bucket: 'my-app-uploads', Key: key, ContentType: contentType }); const url = await getSignedUrl(s3, cmd, { expiresIn: 300 }); res.json({ url, key });

The server picks the key, never the client. That stops path tricks and overwrites. The 300-second expiry is enough for a slow phone. Signature Version 4 caps expiry at seven days. URLs signed with temporary role credentials also die when those credentials expire.

Step 3: Add a CORS rule for browsers

Browsers send a preflight OPTIONS request before a cross-origin PUT. Without a CORS rule on the bucket, it fails. Native mobile apps don't run CORS checks, so this step is only for web.

[{ "AllowedOrigins": ["https://app.example.com"], "AllowedMethods": ["PUT"], "AllowedHeaders": ["content-type"], "ExposeHeaders": ["ETag"], "MaxAgeSeconds": 3000 }]

List your real origins. Avoid the * wildcard in production. See the S3 CORS documentation for the full schema.

Step 4: Upload from web and mobile

On the web, send the File object directly with the same Content-Type you asked the server to sign:

await fetch(url, { method: 'PUT', headers: { 'Content-Type': file.type }, body: file });

In React Native, read the local file into a Blob first: const blob = await (await fetch(uri)).blob(); then PUT it the same way. This works for typical photos. For files over roughly 50 MB, or uploads that must survive the app going to the background, use a background upload library instead.

In Flutter, with the http package: await http.put(Uri.parse(url), headers: {'Content-Type': 'image/jpeg'}, body: bytes); Check for a 200 response. Treat anything else as a failure to retry or report.

Step 5: Confirm and verify the upload

Have the client call POST /uploads/complete with the key. On the server, run HeadObject to confirm the object exists and check ContentLength against your limit. Then read the first few bytes to confirm the file type.

Only after those checks pass, copy the object out of pending/ and save the key to your database. The OWASP File Upload Cheat Sheet lists further checks, such as malware scanning and stripping image metadata.

Presigned PUT vs presigned POST vs multipart

Presigned PUT

This is the simplest option and works from every client. It cannot enforce a size range, only an exact ContentLength if you sign one. It handles single objects up to 5 GB.

Presigned POST

Use @aws-sdk/s3-presigned-post with a content-length-range condition. S3 then rejects files outside your limits before storing them. The trade-off is a multipart form body, which is clumsier from mobile clients.

Multipart upload

You presign each part and the client uploads parts in parallel. AWS recommends it above about 100 MB. It needs more code, including a final CompleteMultipartUpload call and cleanup of incomplete uploads.

The two mistakes that break production

1. Content-Type mismatch. When you set ContentType while signing, the client must send exactly that header. If you sign image/jpeg and the browser sends image/jpg, or sends none, S3 returns 403 SignatureDoesNotMatch. Pass the type from the client to the server, sign it, and send the same string back.

2. A CORS rule that doesn't match the request. The upload works in Postman and on mobile, then fails in the browser with an opaque network error. The usual cause is a missing origin, PUT not being allowed, or content-type missing from AllowedHeaders. Check the preflight response in the browser's network tab first.

How to test it

  • Happy path: curl -X PUT -H 'Content-Type: image/jpeg' --upload-file test.jpg "$URL" should return 200.
  • Wrong type: send image/png against a URL signed for image/jpeg and expect 403.
  • Expiry: sign a URL with expiresIn: 5, wait ten seconds, and expect 403.
  • Preflight: upload from a real browser on your production origin, and also from an unlisted origin to confirm it fails.
  • Verification: upload a renamed .exe as photo.jpg and confirm your complete endpoint rejects it.

For automated tests, LocalStack or MinIO can stand in for S3 in CI. Still run the CORS check against a real bucket, because emulators don't always match S3's CORS behaviour.

FAQ

Is a presigned URL secure if someone intercepts it?

Anyone holding the URL can perform that one action until it expires. That's why expiry should be short and the key should be one the server generated. Always serve the URL over HTTPS.

Can I limit file size with a presigned PUT URL?

Not as a range. You can sign an exact ContentLength, or switch to presigned POST with a content-length-range condition. Either way, check the size again with HeadObject after the upload.

How long should a presigned URL last?

Five to fifteen minutes covers most uploads, including slow mobile networks. The hard maximum is seven days. In practice, the limit is often the lifetime of the temporary credentials that signed it.