Downloading a file from an s3 Bucket to the USERS computer

后端 未结 2 1207
暖寄归人
暖寄归人 2020-12-06 11:27

Goal

Download file from s3 Bucket to users computer.

Context

I am working on a Python/Flask API for a React app. When the user clicks the Downloa

2条回答
  •  天涯浪人
    2020-12-06 12:19

    A better way to solve this problem is to create presigned url. This gives you a temporary URL that's valid up to a certain amount of time. It also removes your flask server as a proxy between the AWS s3 bucket which reduces download time for the user.

    def get_attachment_url():
       bucket = 'BUCKET_NAME'
       key = 'FILE_KEY'
    
       client: boto3.s3 = boto3.client(
         's3',
         aws_access_key_id=YOUR_AWS_ACCESS_KEY,
         aws_secret_access_key=YOUR_AWS_SECRET_KEY
       )
    
       return client.generate_presigned_url('get_object',
                                         Params={'Bucket': bucket, 'Key': key},
                                         ExpiresIn=60) `
    

提交回复
热议问题