从 Flask 中的 S3 返回 PDF
Returning a PDF from S3 in Flask
我正在尝试在 Flask 应用程序的浏览器中 return PDF。我正在使用 AWS S3 存储文件,并使用 boto3 作为 SDK 与 S3 交互。到目前为止我的代码是:
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
我能够使用 boto3 在 file
中从 S3 中成功检索对象,但是我不确定如何从 return 中获取 PDF
事实证明,boto3 没有 readLines()
方法,但它们确实有 read()
方法,所以它可以通过以下方式工作:
response = make_response(file['Body'].read())
response.headers['Content-Type'] = 'application/pdf'
return response
我正在尝试在 Flask 应用程序的浏览器中 return PDF。我正在使用 AWS S3 存储文件,并使用 boto3 作为 SDK 与 S3 交互。到目前为止我的代码是:
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
我能够使用 boto3 在 file
中从 S3 中成功检索对象,但是我不确定如何从 return 中获取 PDF
事实证明,boto3 没有 readLines()
方法,但它们确实有 read()
方法,所以它可以通过以下方式工作:
response = make_response(file['Body'].read())
response.headers['Content-Type'] = 'application/pdf'
return response