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年前630ビュー
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年前

ログインしていません。 ログイン 回答を投稿する。

優れた回答とは、質問に明確に答え、建設的なフィードバックを提供し、質問者の専門分野におけるスキルの向上を促すものです。

質問に答えるためのガイドライン

関連するコンテンツ