How to download a file from S3/Elastic Beanstalk instance to local machine using Python?

0

I'm trying to transfer a file from S3 to my local machine. I thought running

s3.download_file('bucket', 'key', 'filename')    
send_file('filename', as_attachment=True)

in my Elastic Beanstalk code will download the file to my local machine but it seems instead to save it on the EB instance instead. No error is being logged either so it's able to download the file, just not to my local machine.

Is there a way to transfer the file from the EB instance to the local computer after EB downloads the file from S3? If there's Boto3 Python code that can do that, that would be really good.

已提问 2 年前633 查看次数
1 回答
2
已接受的回答

Based on the send_file() function, am I right that you're using Flask? If the files are small, it might be easier to just keep the file in memory (using get_object()) rather than downloading it to the server's filesystem. For example, something like:

s3_result = s3.get_object(Bucket='bucket', Key='key')
response = make_response(s3_result['Body'].read())
response.headers['Content-Disposition'] = 'attachment; filename=myfile.txt'
response.mimetype = 'text/plain' # or whatever the MIME type is of your file
return response

If the files are large, you could instead redirect the user to a presigned url (which you can generate using boto3).

已回答 2 年前
profile pictureAWS
专家
Chris_G
已审核 2 年前

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则