Amazon web services AWS Chalice-将文件作为响应发送
我目前有一个API设置,如下所示Amazon web services AWS Chalice-将文件作为响应发送,amazon-web-services,aws-lambda,aws-api-gateway,chalice,Amazon Web Services,Aws Lambda,Aws Api Gateway,Chalice,我目前有一个API设置,如下所示 @app.route('/', cors=cors_config, methods=['GET']) def index(): request = app.current_request file_name = "orders.csv" file_path = '/tmp/{}_{}'.format("Life", file_name) with open(file_path, 'w') as csvFile:
@app.route('/', cors=cors_config, methods=['GET'])
def index():
request = app.current_request
file_name = "orders.csv"
file_path = '/tmp/{}_{}'.format("Life", file_name)
with open(file_path, 'w') as csvFile:
field_names = ["OrderID", "OrderedBy", "Agent"]
writer = csv.DictWriter(csvFile, fieldnames=field_names)
writer.writeheader()
for item in range(10):
writer.writerow(
rowdict={
"OrderID": str(1),
"OrderedBy": "noob",
"Agent": "pro"
}
)
csvFile.close()
with open(file_path, 'rb') as f:
contents = f.read()
f.close()
file_size = os.path.getsize(file_path)
headers = {'Content-Type': 'application/octet-stream',
'Content-Disposition': 'attachment; filename={}'.format(file_name),
'Content-Length': str(os.path.getsize(file_path))
}
return Response(body=contents, headers=headers)
现在,当使用运行此代码时,我可以下载在这里创建的文件,而不会出现任何问题。但是,在将其部署到我的AWS帐户后,API端点允许我下载一个文件,但它包含我想要的文件的二进制字符串。我阅读了一些其他帖子,其中指出要更新contentHandling路径,将_转换为_BINARY(),然后从API网关内部重新部署API(而不是通过chalice部署)。它仍然表现出同样的行为
有什么办法解决这个问题吗