Stop 06 · Storage: S3, EBS & EFSTopics · 6/10

S3 Presigned URLs

Part of the AWS Roadmap

In short

Your app needs users to download their invoice or upload a profile photo, but the bucket must stay private. Presigned URLs solve this neatly: a link that works for one file, for a short time, and then stops.

A presigned URL grants time-limited access to upload or download a specific object using the permissions of whoever created it. It is the common way to let users upload files directly to S3 from a browser or app without making the bucket public.

The jaadu signs a small paper gate pass with a big flourish and hands it to a carpenter carrying a toolbox at a society gate.

Think of it like this

A signed gate pass

A society secretary writes a gate pass: "Let the carpenter into flat 402 today until 6 pm" and signs it. The guard checks the signature and time, not the carpenter's ID. A presigned URL is that pass, signed with your credentials and valid until it expires.

Watch it happen

Uploading a photo with a presigned URL

User's app
Your backend
Private S3 bucket

I want to upload

1/5The logged-in user asks your API for permission to upload avatar.jpg.

The big ideas

What you need to know

Signer's permissions

The URL can do only what its creator is allowed to do. If the signer loses access, the URL stops working.

Short expiry

Up to 7 days with IAM user keys, but credentials from a role expire sooner and take the URL with them. Use minutes, not days.

GET and PUT

The CLI's aws s3 presign makes download links. Use an SDK to generate upload (PUT) URLs or presigned POST forms.

CORS for browsers

Browser uploads directly to S3 need a CORS rule on the bucket allowing your site's origin.

See it in code

Walk through the code

python
1import boto32 3s3 = boto3.client("s3", region_name="ap-south-1")4 5# Upload link for one exact key, valid for 5 minutes6upload_url = s3.generate_presigned_url(7    "put_object",8    Params={"Bucket": "acme-user-files", "Key": "users/42/avatar.jpg",9            "ContentType": "image/jpeg"},10    ExpiresIn=300,11)12 13# Download link for an invoice, valid for 10 minutes14download_url = s3.generate_presigned_url(15    "get_object",16    Params={"Bucket": "acme-user-files", "Key": "users/42/invoice-1001.pdf"},17    ExpiresIn=600,18)19print(upload_url, download_url, sep="\n")

1/3The client uses the backend's role credentials. No keys in code.

Backend code that signs one upload and one download URL. The bucket stays private with Block Public Access on.

Save yourself the pain

Common mistakes

Oops

Letting the client choose any key, like ../../admin/config.json.

Do this instead

The backend builds the key itself from the logged-in user's ID and a safe filename.

Oops

Setting a 7-day expiry 'to be safe'.

Do this instead

Anyone with the link can use it until expiry. Keep it as short as the use case allows.

Your mission

Share without going public

~20 min

Quick check

A presigned URL was generated by a role whose temporary credentials expire in 1 hour, with ExpiresIn set to 12 hours. How long will it work?

Go deeper

  1. 1Generate a download URL with aws s3 presign and open it in a browser.
  2. 2Generate an upload URL with boto3 and upload a file with curl.
  3. 3Set a short expiry and confirm the URL stops working afterwards.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge