从Flask中的S3返回PDF

roh*_*hit 4 python amazon-s3 amazon-web-services flask boto3

我正在尝试在Flask应用程序的浏览器中返回PDF。我正在使用AWS S3存储文件,并使用boto3作为与S3进行交互的SDK。到目前为止,我的代码是:

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
Run Code Online (Sandbox Code Playgroud)

我可以file使用boto3 从S3成功检索对象,但是那是一个字典,我不确定如何从中返回PDF

roh*_*hit 5

事实证明,boto3没有readLines()方法,但是他们确实有read()方法,因此它可以通过以下方式工作:

response = make_response(file['Body'].read())
response.headers['Content-Type'] = 'application/pdf'
return response
Run Code Online (Sandbox Code Playgroud)