Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning a PDF from S3 in Flask

I'm trying to return a PDF in the browser in a Flask application. I'm using AWS S3 to store the files and boto3 as the SDK to interact with S3. My code so far is:

s3 = boto3.resource('s3', aws_access_key_id=settings.AWS_ACCESS_KEY,
                aws_secret_access_key=settings.AWS_SECRET_KEY)
file = s3.Object(settings.S3_BUCKET_NAME, 'test.pdf')).get()
response = make_response(file)
response.headers['Content-Type'] = 'application/pdf'
return response

I'm able to successfully retrieve the object from S3 in file using boto3, however that is a dictionary of which I'm not sure how to return the PDF from that

like image 936
rohit Avatar asked Jul 30 '16 17:07

rohit


1 Answers

It turns out that boto3 doesn't have a readLines() method but they do have a read() method, so it worked by doing:

response = make_response(file['Body'].read())
response.headers['Content-Type'] = 'application/pdf'
return response
like image 153
rohit Avatar answered Sep 20 '22 13:09

rohit