Skip to content

Instantly share code, notes, and snippets.

@yaxhpal
Last active June 8, 2020 21:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yaxhpal/cd25c7278d56721c66f445c69d1f6eee to your computer and use it in GitHub Desktop.
Save yaxhpal/cd25c7278d56721c66f445c69d1f6eee to your computer and use it in GitHub Desktop.
Stream AWS S3 Files using Boto3
# 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