【问题标题】:Localstack - AWS API Gateway enabling binary support using TerraformLocalstack - AWS API Gateway 使用 Terraform 启用二进制支持
【发布时间】: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

以下是基本步骤...

  1. 创建 ReST API:
resource "aws_api_gateway_rest_api" "api" {
  name = var.api_name
  
  # enable support for 'image/png'
  binary_media_types = [
    "image/png",
  ]
}
  1. 创建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
  }
}
  1. 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"
}
  1. 部署 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


【解决方案1】:

我通过使用“*/*”作为 binary_media_types 来让它工作,仅此而已。我在某处读到,如果您指定某些内容,客户端需要发送相同的接受内容标头。

我也没有 aws_api_gateway_integration_response 资源。

最后我以 ByteArray 的形式返回原始图像字节。我正在使用 Kotlin 来实现我的 lambda,但下面应该让您知道如何去做。

@Get("/{id}/avatar")
fun getImage(id: Long): HttpResponse<ByteArray> {
    logger.info { "AvatarController.getImage($id)" }
    val image = avatarService.getImage(id)
    val bytes: ByteArray = IOUtils.toByteArray(image)
    return HttpResponse.ok(bytes).contentType(MediaType.APPLICATION_OCTET_STREAM_TYPE)

不要 Base64 编码,不要使用 APIGatewayProxyResponse。

【讨论】:

    猜你喜欢
    • 2020-10-25
    • 2019-06-19
    • 2019-01-06
    • 1970-01-01
    • 2016-12-23
    • 2017-05-24
    • 1970-01-01
    • 1970-01-01
    • 2015-10-05
    相关资源
    最近更新 更多