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 年前檢視次數 635 次
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 年前

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南