本文介绍了AWS Chalice从S3返回图像文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用Chalice构建一个简单的服务器应用程序,该应用程序从S3返回一个图像文件.
I am using Chalice to build a simple severless application which returns an image file from S3.
经过64位编码后,我可以返回文件.但是我想知道如何返回二进制文件,以便用户将其作为文件下载吗?我的以下代码无法正常工作.
I am able to return file after it is 64 bit encoded. But I am wondering how I can return the binary file so that user can take it as a file download? My following code is not working.
@app.route('/binary_object/{bucket}/{key}', methods=['GET', 'PUT'])
def binary_object(bucket, key):
request = app.current_request
if request.method == 'GET':
try:
file_path = '/tmp/{}_{}'.format(uuid.uuid4(), key)
s3_client.download_file(bucket, key, file_path)
file_size = os.path.getsize(file_path)
headers = {'Content-Disposition': 'attachment; filename=\"' + key + '\"',
'Content-Type': 'application/octet-stream',
# 'Content-Type': 'image/*',
'Content-Length': str(file_size)}
fsk = open(file_path, 'rb')
return Response(body=fsk, headers=headers, status_code=200)
except Exception as e:
print e
raise e
推荐答案
圣杯中有一个错误已在2019年5月14日修复:
There was a bug in Chalice that was fixed on 14-May-2019:
https://github.com/aws/chalice/issues/1095
我遇到了与您类似的问题,并且通过获取最新版本的Chalice已解决了该问题.
I had a similar problem to yours and it's been fixed by grabbing the latest version of Chalice.
这篇关于AWS Chalice从S3返回图像文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!