Sub topic 9
Sharing private objects with presigned URLs
A presigned URL lets someone download or upload one S3 object without having AWS credentials of their own. This chapter explains how the URL is built, what limits its lifetime, and which mistakes cause the most trouble. It assumes you can run the AWS CLI and read basic Python. It also assumes you know that an IAM user or role holds credentials and permissions, and that Block Public Access and bucket policies exist (covered in the previous chapter).
The problem presigned URLs solve
Your bucket is private, as it should be. A customer needs to download one report, or a browser needs to upload one photo. You have three options:
- Make the object public. This exposes it to everyone and is usually the wrong choice.
- Create IAM users for every outside person. This does not scale.
- Proxy the bytes through your own server. This works, but you pay for the bandwidth and compute.
A presigned URL is a fourth option. Your code signs a request and hands the resulting URL to the other party. The recipient sends it over plain HTTPS, and S3 treats the request as if the signer had made it.
How a presigned URL works
A presigned URL is an ordinary S3 URL with the signature carried in query-string parameters instead of an Authorization header. The signing happens locally in your SDK. No call to S3 is made when you generate the URL.
The signing process uses Signature Version 4 (SigV4). It produces a URL like this (shortened):
https://my-example-bucket-123456789012.s3.eu-west-1.amazonaws.com/reports/2026/report.csv
?X-Amz-Algorithm=AWS4-HMAC-SHA256
&X-Amz-Credential=AKIA.../20261126/eu-west-1/s3/aws4_request
&X-Amz-Date=20261126T193323Z
&X-Amz-Expires=900
&X-Amz-SignedHeaders=host
&X-Amz-Signature=...Each parameter has a job:
| Parameter | Meaning |
|---|---|
| `X-Amz-Credential` | The access key of the signer, the date, the Region and the service |
| `X-Amz-Date` | When the URL was signed |
| `X-Amz-Expires` | How many seconds the URL stays valid after signing |
| `X-Amz-SignedHeaders` | The request headers covered by the signature |
| `X-Amz-Signature` | The HMAC computed from the request details and the signer's secret key |
When S3 receives the request, it recomputes the signature, checks that the URL has not expired, and then evaluates permissions as if the signer had sent the request.
Generating a download URL
The Python example uses generate_presigned_url with the get_object operation:
import boto3
s3 = boto3.client("s3")
url = s3.generate_presigned_url(
"get_object",
Params={"Bucket": "my-example-bucket-123456789012", "Key": "reports/2026/report.csv"},
ExpiresIn=900,
)
print(url)ExpiresIn is in seconds, so 900 gives the holder 15 minutes. Anyone who holds the URL can download the object until it expires. The URL is a bearer token, so treat it like a password: do not log it or post it in a public channel.
The AWS CLI has a shortcut for the same thing:
aws s3 presign s3://my-example-bucket-123456789012/reports/2026/report.csv \
--expires-in 900 \
--region eu-west-1Test the URL without credentials, for example with curl, to confirm it works for an outside caller:
curl -o report-copy.csv "$(aws s3 presign s3://my-example-bucket-123456789012/reports/2026/report.csv --expires-in 900 --region eu-west-1)"Generating an upload URL
You can also presign put_object. The recipient then uploads with an HTTP PUT, and the object lands at exactly the key you chose.
import boto3
import urllib.request
s3 = boto3.client("s3", region_name="eu-west-1")
url = s3.generate_presigned_url(
"put_object",
Params={
"Bucket": "my-example-bucket-123456789012",
"Key": "uploads/photo.jpg",
"ContentType": "image/jpeg",
},
ExpiresIn=300,
)
with open("photo.jpg", "rb") as f:
request = urllib.request.Request(
url, data=f.read(), method="PUT", headers={"Content-Type": "image/jpeg"}
)
with urllib.request.urlopen(request) as response:
print(response.status)Because ContentType was part of the signed parameters, the uploader must send the same Content-Type header. If the header differs, S3 returns 403 SignatureDoesNotMatch. This is useful: you can pin the content type. It is also the most common source of confusing failures.
A presigned PUT has limits. You cannot cap the file size with it. If you need a size limit or form-style uploads from a browser, look at presigned POST (generate_presigned_post), which supports policy conditions such as content-length-range. For very large files, each part of a multipart upload needs its own presigned URL.
What actually limits the lifetime
The ExpiresIn value is only one limit. The effective lifetime is the shortest of several:
| Credentials used to sign | Maximum lifetime |
|---|---|
| IAM user with long-term access keys | Up to 7 days (604800 seconds) with SigV4 |
| IAM role or temporary credentials (STS, instance profile, SSO) | Until the temporary credentials expire, even if `ExpiresIn` is longer |
| Console-generated presigned URL | A shorter window chosen in the console |
This catches many teams out. A Lambda function or EC2 instance signs with role credentials that may last only an hour or so. A URL generated with ExpiresIn=86400 will stop working when those credentials expire, and the failure looks like an ExpiredToken or AccessDenied error. If the URL must live for days, you need a signer with long-term credentials, or you need to regenerate the URL on demand instead.
Also, if the signing user's access key is deleted or deactivated, every URL signed with it stops working immediately.
Permissions: the signer's, not the recipient's
The URL carries the permissions of whoever signed it, evaluated at the time of use. Three consequences follow:
- The signer needs permission for the action. To presign a download, the signing identity needs
s3:GetObjecton that key. Generating the URL succeeds even without the permission, because nothing is checked locally. The request fails with403when someone uses it. - A policy change takes effect on existing URLs. If you remove the signer's access, previously issued URLs stop working.
- Block Public Access does not block presigned URLs, because the requests are authenticated. A bucket policy with an explicit
Denystill applies. For example, the HTTPS-only policy from the encryption chapter is satisfied because presigned URLs arehttps://URLs.
If an object is encrypted with SSE-KMS, the signer also needs kms:Decrypt on the key. Downloads through presigned URLs for KMS-encrypted objects require SigV4 signing, which current SDKs use by default.
You can restrict where URLs may be used from. A bucket policy can deny requests that do not come from your VPC endpoint or a known IP range, using aws:SourceVpce or aws:SourceIp conditions. A presigned URL used from outside that network will then be denied.
Common errors
| Error | Likely cause |
|---|---|
| `403 SignatureDoesNotMatch` | A signed header (such as `Content-Type`) differs from the request, or the key or query string was altered or re-encoded |
| `403 AccessDenied` with `Request has expired` | `X-Amz-Expires` has passed |
| `403 AccessDenied` or `ExpiredToken` before expiry | The signer's temporary credentials ended first |
| `403 AccessDenied` on a fresh URL | The signer lacks `s3:GetObject` or `kms:Decrypt`, or a bucket policy denies the request |
| `301` or `PermanentRedirect` | The URL was signed for the wrong Region or endpoint. Set `region_name` explicitly |
| Clock-related failure | The signing machine's clock is far off. Sync it with NTP |
Set the Region on the client. If a client signs for the wrong Region, the URL can break for buckets outside us-east-1 in ways that look like network faults.
Design guidance
- Keep URLs short-lived. Minutes are enough for a user clicking a download link. Generate the URL when the user asks, not in advance.
- Authenticate your own users first. Your application decides who gets a URL. S3 only checks that the URL is valid.
- Use a least-privilege signer. Give the signing role access only to the prefix it needs, such as
uploads/*, so a leaked URL cannot reach more than one object. - Use a server-chosen key for uploads, for example a generated ID, so users cannot overwrite each other's objects.
- Remember that you cannot revoke a single URL. You can only wait for expiry, or remove the signer's permission or credentials, which affects every URL it issued.
- Do not put URLs in logs or analytics. S3 access logs and CloudTrail record the request, and the URL itself is a working secret until it expires.
Summary
A presigned URL packages a signature, an expiry and the signer's identity into a link. It is generated locally, it works only for the signed method and object, and it carries the signer's permissions. The effective lifetime is the shorter of ExpiresIn and the signer's credential lifetime. Most failures come from mismatched signed headers, expired role credentials, a wrong Region, or a signer that lacks permissions.
Exercise
A Lambda function uses its execution role to generate a presigned download URL with ExpiresIn=86400 (24 hours). Users report that links stop working after about an hour, with an expired-token error. Why does this happen, and what are two ways to fix it?
Show answer
The URL is signed with temporary role credentials, and a presigned URL cannot outlive the credentials that signed it. When those credentials expire (often within about an hour), the URL stops working regardless of ExpiresIn. Fixes: (1) generate the URL on demand when the user clicks, so it is always signed with fresh credentials and a short lifetime, or (2) sign with an IAM user's long-term access keys, which allows up to 7 days, accepting the extra risk of managing long-term keys.
Exercise
You presign a put_object URL with ContentType="image/jpeg" in Params. A client uploads with the header Content-Type: image/png and gets 403 SignatureDoesNotMatch. Why, and how would you cap the upload size, which presigned PUT cannot do?
Show answer
The Content-Type value was included in the signature, so the request must send exactly the same value. A different value changes the computed signature and S3 rejects it. To cap the size, use a presigned POST with generatepresignedpost and a content-length-range condition in the policy.
Exercise
You generated a presigned download URL, but removed the signer's s3:GetObject permission before the recipient used it. What happens when the recipient opens the URL?
Show answer
S3 returns 403 AccessDenied. The URL carries the signer's permissions, evaluated when the request is made, not when the URL was created. Removing the permission invalidates all URLs that depend on it.