Get an S3 Presigned Url with AWS CLI

avatar
Borislav Hadzhiev

2 min

banner

# How to get an S3 Presigned Url with AWS CLI

To get an S3 pre-signed URL using the AWS CLI, invoke the aws s3 presign s3://my-bucket/file.txt command, where you specify the entire path of the object in the S3 URI.

shell
aws s3 presign s3://YOUR_BUCKET/your-file.txt

get presigned url

By default, the pre-igned URL is valid for 3600 seconds, which is 60 minutes.

If you click on the URL, you will see the contents of the file in your browser.

Note that the full path to the object has to be specified, including the extension, if the file has one, e.g. s3://YOUR_BUCKET/your-folder/your-file.txt

To configure when the pre-signed URL should expire, use the --expires-in parameter. It allows us to specify the number of seconds the pre-signed URL should be valid for.

aws s3 presign s3://YOUR_BUCKET/your-file.txt --expires-in 10

get presigned url with expiration

In the example above, the pre-signed URL will only be valid for 10 seconds. If the URL is accessed after 10 seconds, it returns an AccessDenied error.

presigned url expired error

Note that the S3 presign command only generates GET pre-signed URLs, if you want to upload files to an S3 bucket using POST or PUT pre-signed URLs, you have to use the AWS SDK.

# Further Reading

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.