【发布时间】:2021-02-08 19:31:36
【问题描述】:
我无法在 Localstack 上使用带有 Terraform 的 API 网关启用二进制支持。
例子很简单:一个 Lambda 生成类似于 Github 的头像,并将图像返回为 PNG。
Lambda (代理)与 API 网关集成(想法是:GET /avatars/{username} -> image/png)
当我调用已发布的 URL(我在 Localstack 上执行此操作)时,API 始终返回 Base64 编码图像而不应用 CONVERT_TO_BINARY。
以下是基本步骤...
- 创建 ReST API:
resource "aws_api_gateway_rest_api" "api" {
name = var.api_name
# enable support for 'image/png'
binary_media_types = [
"image/png",
]
}
- 创建
GET /avatars/{username}端点:
# ReST API endpoint 'avatars'
resource "aws_api_gateway_resource" "avatars" {
rest_api_id = aws_api_gateway_rest_api.api.id
parent_id = aws_api_gateway_rest_api.api.root_resource_id
path_part = "avatars"
}
# 'avatars' endpoint resource path parameter.
resource "aws_api_gateway_resource" "resource" {
rest_api_id = aws_api_gateway_rest_api.api.id
parent_id = aws_api_gateway_resource.avatars.id
path_part = "{username}"
}
# Defines the resource HTTP method (verb or action).
resource "aws_api_gateway_method" "get-avatar" {
rest_api_id = aws_api_gateway_rest_api.api.id
resource_id = aws_api_gateway_resource.resource.id
http_method = "GET"
authorization = "NONE"
request_parameters = {
"method.request.path.username" = true
}
}
- 将
GET /avatars/{username}端点集成到 ReST API:
resource "aws_api_gateway_integration" "resource_integration" {
rest_api_id = aws_api_gateway_rest_api.api.id
resource_id = aws_api_gateway_resource.resource.id
http_method = aws_api_gateway_method.get-avatar.http_method
type = "AWS_PROXY"
integration_http_method = "POST"
uri = aws_lambda_function.lambda.invoke_arn
passthrough_behavior = "WHEN_NO_MATCH"
request_parameters = {
"integration.request.path.id" = "method.request.path.username"
}
}
resource "aws_api_gateway_integration_response" "get-avatar-response" {
rest_api_id = aws_api_gateway_rest_api.api.id
resource_id = aws_api_gateway_resource.resource.id
http_method = aws_api_gateway_method.get-avatar.http_method
status_code = "200"
content_handling = "CONVERT_TO_BINARY"
}
- 部署 ReST API:
resource "aws_api_gateway_deployment" "deployment" {
rest_api_id = aws_api_gateway_rest_api.api.id
stage_name = "stage"
depends_on = [aws_api_gateway_method.get-avatar, aws_api_gateway_integration.resource_integration]
}
API Gateway 假定文本数据是 base64 编码的字符串,并将二进制数据输出为 base64 解码的 blob。
这里是简单的 Lambda 处理程序(当然是在很棒的 Go 中!:-))
func handler(ctx context.Context, evt events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
log.Printf("Processing request data for request %s.\n", evt.RequestContext.RequestID)
username := evt.PathParameters["username"]
if len(username) == 0 {
code := http.StatusBadRequest
msg := http.StatusText(code)
return events.APIGatewayProxyResponse{Body: msg, StatusCode: code}, nil
}
key := []byte{0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF}
icon := identicon.New7x7(key)
log.Printf("creating identicon for '%s'\n", username)
pngdata := icon.Render([]byte(username))
body := base64.StdEncoding.EncodeToString(pngdata) // <= Base64 Encoded Response
return events.APIGatewayProxyResponse{
Body: body,
Headers: map[string]string{
"Content-Type": "image/png",
},
IsBase64Encoded: true, // <= Is Base64 Encoded? Yes!
StatusCode: 200,
}, nil
}
使用 curl 调用端点(例如):
curl 'http://localhost:4566/restapis/8nilx7bu49/stage/_user_request_/avatars/type-a-username-here'
...它以 Base64 编码图像响应。事实上,如果我将输出传送到base64 -d 保存内容,则图像是正确的:
curl 'http://localhost:4566/restapis/8nilx7bu49/stage/_user_request_/avatars/type-a-username-here' -s | base64 -d > test.png
有人可以指出我遗漏或困惑的地方吗?
一切顺利, 卢卡
【问题讨论】:
-
我遇到了同样的问题,到目前为止我所做的所有研究都表明 API Gateway 实现不支持二进制媒体类型。
标签: terraform aws-api-gateway localstack