5

I have to download a file from my S3 bucket onto my server for some processing. The bucket does not support direct connections and has to use a Pre-Signed URL.


The Boto3 Docs talk about using a presigned URL to upload but do not mention the same for download.

Alok
  • 7,392
  • 8
  • 44
  • 82
Varun Shridhar
  • 133
  • 1
  • 1
  • 8
  • Could you please let me know what could be improved? I went through the link but was unable to figure out what part of my question was not according to the standard. – Varun Shridhar Feb 11 '20 at 08:16

1 Answers1

13
import boto3

s3_client = boto3.client('s3')

BUCKET = 'my-bucket'
OBJECT = 'foo.jpg'

url = s3_client.generate_presigned_url(
    'get_object',
    Params={'Bucket': BUCKET, 'Key': OBJECT},
    ExpiresIn=300)

print(url)

For another example, see: Presigned URLs — Boto 3 documentation

You can also generate a pre-signed URL using the AWS CLI:

aws s3 presign s3://my-bucket/foo.jpg --expires-in 300

See: presign — AWS CLI Command Reference

John Rotenstein
  • 203,710
  • 21
  • 304
  • 382
  • 13
    This answer shows how to generate a pre-signed url but not how to download the file. – David Medinets Aug 21 '20 at 17:58
  • 1
    @DavidMedinets The pre-signed url can be downloaded as any regular file. – Ariel M. Jan 08 '21 at 16:58
  • @ArielM. - doesn't work for me.. I get an access denied when trying to open the presigned url on my browser – Siddhant Sadangi Jan 18 '22 at 08:24
  • @SiddhantSadangi The pre-signed URL uses permissions from the credentials that were used to create the pre-signed URL. So, receiving `Access Denied` suggests that the credentials used do _not_ have permission to access the object. You can test this by trying to access the object by using the AWS CLI with the same credentials (eg `aws s3 cp s3://bucketname/object.name .`). – John Rotenstein Jan 18 '22 at 11:44