How do I create a Presigned URL to download a file from an S3 Bucket using Boto3?

大城市里の小女人 提交于 2020-12-11 08:49:12

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/60163289/how-do-i-create-a-presigned-url-to-download-a-file-from-an-s3-bucket-using-boto3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!