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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With