Sub topic 9
Sharing private objects with presigned URLs
This chapter assumes you can create a bucket, upload an object with the CLI or boto3, and read a basic IAM policy. It also assumes Block Public Access is on for your bucket, as set up in earlier chapters.
The problem
Your bucket is private. A customer needs to download one report. You could make the object public, but that exposes it to everyone. You could create an IAM user for the customer, but that is heavy for a single file.
A presigned URL solves this. It is a normal S3 URL with a signature and an expiry time added to the query string. Anyone who holds the URL can make that one request until it expires. The bucket stays private.
How it works
The signature is computed locally by the SDK from the credentials you use. No call to S3 is made to create the URL. When S3 receives the request, it checks the signature and the expiry time. It then evaluates the request as if the signing identity had made it.
This has two consequences:
- The URL can do only what the signer is allowed to do. If the signer loses access to the object, the URL stops working.
- If the signing credentials are temporary (for example an IAM role), the URL stops working when those credentials expire, even if the requested lifetime is longer.
Creating a download URL
This code creates a URL that allows a download for 15 minutes (900 seconds).
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)Anyone can then fetch the object with a plain HTTP client, with no AWS credentials:
curl -o report.csv "PASTE_THE_PRESIGNED_URL_HERE"Keep the quotes. The URL contains & characters that the shell would otherwise interpret.
You can also create a URL from the CLI. The default lifetime is 3600 seconds.
aws s3 presign s3://my-example-bucket-123456789012/reports/2026/report.csv --expires-in 900Uploads
Change the operation name to put_object and the URL allows an upload of that one key. The client sends an HTTP PUT with the file as the body. This lets browsers or mobile apps upload straight to S3 without your server handling the bytes.
Expiry limits
The maximum lifetime depends on the credentials that sign the URL.
| Signing credentials | Maximum lifetime |
|---|---|
| IAM user with long-term access keys | 7 days |
| IAM role or other temporary credentials | Until the temporary credentials expire |
Prefer short lifetimes. A URL cannot be revoked individually. You can only wait for it to expire, change the signer's permissions, or deny access with a bucket policy.
Common errors
AccessDeniedorSignatureDoesNotMatch: The signer lacks permission, the URL was altered, or the Region or signature version is wrong. Create the client with the bucket's Region if you see this.Request has expired: The lifetime has passed, or the temporary credentials expired first.- URL works for the signer's identity only in theory: Block Public Access does not block presigned URLs, because the request is authenticated. Do not assume a URL is harmless because the bucket is private.
Exercise
You generate a presigned GET URL with ExpiresIn=604800 (7 days) while running on an EC2 instance that uses an IAM role. A user reports the URL stopped working after a few hours. What is the likely cause, and how would you avoid it?
Show answer
The URL is signed with the role's temporary credentials, and it stops working when those credentials expire, whatever ExpiresIn says. To avoid this, request a lifetime shorter than the credential lifetime and generate the URL close to when it is needed. If you really need a long-lived URL, sign with long-term IAM user credentials, which allow up to 7 days. Weigh that against the risk of long-lived access keys.
Exercise
A presigned URL for a private object was posted in a public chat. Name two ways to stop it from working before it expires.
Show answer
Add a bucket policy statement that denies access to the object, or remove the signing identity's permission to read it. Deleting or replacing the object also stops it from returning the data.