【发布时间】:2023-03-23 12:25:01
【问题描述】:
我正在试验 AWS API-Gateway 与 S3 后端的集成。我注意到集成响应中不同映射模板之间的切换似乎不起作用。
在集成响应中,我有以下映射模板:
应用程序/json:
{
type: "JSON",
body: "$input.body"
}
文本/纯文本:
PlainText:
$input.body
我似乎无法使用 text/plain 映射模板 - 它始终使用 application/json。
我希望text/plain 映射模板的使用基于以下条件之一:
- S3 返回带有
Content-Type: text/plain的内容。 - 对 API-Gateway 的初始请求传递一个
Accept: text/plain标头。
根据下面的示例,S3 返回一个Content-Type: text/plain AND 我用Accept: text/plain 请求。 API-Gateway 也正确响应 Content-Type: text/plain。
但是application/json 模板仍然用于转换body。
我什至从方法响应中完全删除了application/json 作为有效响应类型,但仍然没有。
任何想法为什么会发生这种情况?
仅供参考,我使用的是经典的 v1 ApiGateway (Rest)。
Execution log for request c24cfce3-2cf3-4693-ad72-4fdf44f4fdcd
Wed May 19 17:13:38 UTC 2021 : Starting execution for request: c24cfce3-2cf3-4693-ad72-4fdf44f4fdcd
Wed May 19 17:13:38 UTC 2021 : HTTP Method: GET, Resource Path: /feeds-poc/test.txt
Wed May 19 17:13:38 UTC 2021 : Method request path: {filename=test.txt}
Wed May 19 17:13:38 UTC 2021 : Method request query string: {}
Wed May 19 17:13:38 UTC 2021 : Method request headers: {Accept=text/plain}
Wed May 19 17:13:38 UTC 2021 : Method request body before transformations:
Wed May 19 17:13:38 UTC 2021 : Endpoint request URI: https://my-bucket-id.s3.eu-west-1.amazonaws.com/test.txt
Wed May 19 17:13:38 UTC 2021 : Endpoint request headers: {Authorization=****57173a, X-Amz-Date=20210519T171338Z, x-amzn-apigateway-api-id=123456789a, Accept=application/json, User-Agent=AmazonAPIGateway_123456789a, X-Amz-Security-Token=**** [TRUNCATED]
Wed May 19 17:13:38 UTC 2021 : Endpoint request body after transformations:
Wed May 19 17:13:38 UTC 2021 : Sending request to https://my-bucket-id.s3.eu-west-1.amazonaws.com/test.txt
Wed May 19 17:13:38 UTC 2021 : Received response. Status: 200, Integration latency: 43 ms
Wed May 19 17:13:38 UTC 2021 : Endpoint response headers: {x-amz-id-2=****, x-amz-request-id=****, Date=Wed, 19 May 2021 17:13:39 GMT, Last-Modified=Mon, 17 May 2021 16:45:13 GMT, ETag="420f804aa21220bf0db57bb4b9799c8a", Accept-Ranges=bytes, Content-Type=text/plain, Content-Length=13, Server=AmazonS3}
Wed May 19 17:13:38 UTC 2021 : Endpoint response body before transformations: It's working
Wed May 19 17:13:38 UTC 2021 : Method response body after transformations: {
type: "JSON",
body: "It's working
"
}
Wed May 19 17:13:38 UTC 2021 : Method response headers: {X-Amzn-Trace-Id=Root=****, Content-Type=text/plain}
Wed May 19 17:13:38 UTC 2021 : Successfully completed execution
Wed May 19 17:13:38 UTC 2021 : Method completed with status: 200
【问题讨论】:
标签: amazon-web-services amazon-s3 aws-api-gateway integration