Last active
June 8, 2020 21:54
-
-
Save yaxhpal/cd25c7278d56721c66f445c69d1f6eee to your computer and use it in GitHub Desktop.
Stream AWS S3 Files using Boto3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Demonstration only | |
from flask import current_app as app, Response | |
import boto3 # boto3==1.13.25 | |
import botocore # botocore==1.16.25 | |
CHUNK_SIZE = 256*1024 # 256kb | |
def get(a_bucket, a_file) | |
s3 = boto3.client('s3') | |
try: | |
response = s3.get_object(Bucket=a_bucket, Key=a_file) | |
data_stream = response['Body'] | |
content_type = response['ContentType'] | |
headers = {'Content-Disposition': 'attachment;filename=' + a_file} | |
return Response(data_stream.iter_chunks(CHUNK_SIZE), mimetype=content_type, headers=headers) | |
except botocore.exceptions.ClientError as e: | |
app.logger.exception('No such file exists: {}'.format(e)) | |
return 'Not Found', 404 | |
# Feel free to give suggestions, we can improve this. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment