我正在使用 Chalice 构建一个简单的无服务器应用程序,该应用程序从 S3 返回图像文件。
我能够在 64 位编码后返回文件。但我想知道如何返回二进制文件以便用户可以将其作为文件下载?我的以下代码不起作用。
@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
最佳答案
Chalice 中存在一个错误,已于 2019 年 5 月 14 日修复:
https://github.com/aws/chalice/issues/1095
我遇到了与您类似的问题,通过获取最新版本的 Chalice 已解决该问题。
关于python - AWS Chalice 从 S3 返回图像文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44149971/