koc*_*fan 12 aws-lambda aws-api-gateway serverless-framework
众所周知,API Gateway和lambda支持二进制请求/响应,但我对节点JavaScript中的后端编程有一个疑问.
环境:
在上述环境中,在我的代码中,我将响应内容作为二进制(缓冲对象数组).
但是,如果我直接将Buffer对象数组作为响应,
callback(null,{
headers: {'Content-Type': 'image/jpeg'},
body: body
});
Run Code Online (Sandbox Code Playgroud)
收到回复是这样的:
Content-type: image/jpeg
{type=Buffer, data=[255,216,255,224,0,16,74,70,73,70,0...
Run Code Online (Sandbox Code Playgroud)
如果我通过base64编码将Buffer对象数组作为响应,
callback(null,{
headers: {'Content-Type': 'image/jpeg'},
body: body.toString('base64')
});
Run Code Online (Sandbox Code Playgroud)
收到回复是这样的:
Content-type: image/jpeg
/9j/4AAQSkZJRgABAQEASABIAAD/2wBDA...
Run Code Online (Sandbox Code Playgroud)
如何使用无服务器框架从节点JS后端向API网关提供二进制响应?
== PostScript ==
根据此文档:
没有Accept标头的AWS API Gateway二进制输出
我们必须将Integration响应的"Content Handling"更改为"CONVERT TO BINARY",以响应二进制响应.
但是我该怎么设置呢?
我不知道serverless.yml和AWS控制台GUI.
如果我成功设置了Content Handling => CONVERT TO BINARY,我可以解决响应二进制响应吗?
== 1月17日编辑==
嗨@ ka-hou-ieong
你写了rest-api-id和resource-id,它们在下面的图片中,对吗?
但是使用这些id,命令结果说:
$aws apigateway put-integration-response --rest-api-id XXXXXXXX --resource-id XXXXXX --http-method GET --status-code 200 --content-handling CONVERT_TO_BINARY
An error occurred (NotFoundException) when calling the PutIntegrationResponse operation: Invalid Resource identifier specified
Run Code Online (Sandbox Code Playgroud)
这有什么问题?我使用最新的aws-cli(aws-cli/1.11.37 Python/2.7.9 Darwin/16.3.0 botocore/1.5.0)
如果您想要强制响应为二进制响应,您可以通过 AWS CLI 或 API 将“CONVERT_TO_BINARY”设置为集成响应上的 contentHandling。目前,我们在控制台上缺少此选项。
PATCH /restapis/<restapi_id>/resources/<resource_id>/methods/<http_method>/integration/responses/<status_code>
{
"patchOperations" : [ {
"op" : "replace",
"path" : "/contentEncoding",
"value" : "CONVERT_TO_BINARY"
}]
}
Run Code Online (Sandbox Code Playgroud)
aws apigateway put-integration-response --rest-api-id xxxxxxx --resource-id xxxxx --http-method GET --status-code 200 --content-handling CONVERT_TO_BINARY
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2079 次 |
| 最近记录: |