Skip to content
Advertisement

How do I get a download link for an object I upload to an AWS bucket?

I’m using AWS S3 boto3 to upload files to my AWS bucket called uploadtesting. Here is an example implementation:

import boto3

...

s3 = boto3.resource('s3')
s3.meta.client.upload_file('files/report.pdf', 'uploadtesting', 'report.pdf')

Accessing the object from the AWS S3 console allows you to see the object URL, however it is not a downloadable link. What I wanted to know is how can I use python to print out a downloadable link to the file I just uploaded?

Advertisement

Answer

It appears you are asking how to generate a URL that allows a private object to be downloaded.

This can be done by generating an Amazon S3 pre-signed URL, which provides access to a private S3 object for a limited time.

Basically, using credentials that have access to the object, you can create a URL that is ‘signed’. When Amazon S3 receives this URL, it verifies the signature and provides access to the object if the expiry period has not ended.

From Presigned URLs — Boto3 documentation:

        response = s3_client.generate_presigned_url('get_object',
                                                    Params={'Bucket': bucket_name,
                                                            'Key': object_name},
                                                    ExpiresIn=expiration)

The ExpiresIn parameter is expressed in seconds.

Advertisement