Amazon web services AWS ApiGateway请求路径映射+;兰姆达
我知道有很多关于映射请求数据的问题,但是没有一个对我有帮助。 所以,我试图实现的是映射到lambda的API端点。当bucket触发404时,对该端点的请求被转发,参数通过请求路径传递给lambda,如:/{image_name}/{width}/{height}。 我的lambda代码只调用Amazon web services AWS ApiGateway请求路径映射+;兰姆达,amazon-web-services,amazon-s3,aws-api-gateway,aws-lambda,Amazon Web Services,Amazon S3,Aws Api Gateway,Aws Lambda,我知道有很多关于映射请求数据的问题,但是没有一个对我有帮助。 所以,我试图实现的是映射到lambda的API端点。当bucket触发404时,对该端点的请求被转发,参数通过请求路径传递给lambda,如:/{image_name}/{width}/{height}。 我的lambda代码只调用context.success(事件,上下文) 在方法请求配置中,请求路径的参数是自动创建的 在集成请求中,我创建了三个映射模板:plain/text、plain/html、application/json
context.success(事件,上下文)代码>
在方法请求配置中,请求路径的参数是自动创建的
在集成请求中,我创建了三个映射模板:plain/text、plain/html、application/json,其定义如下:
#set($inputRoot = $input.path('$'))
{
"name": $input.params('name'),
"width" : $input.params('width'),
"height" : $input.params('height'),
"params": $input.params(),
"resourcePath": $context.resourcePath,
}
从chrome rest客户端调用时,我得到:
从控制台调用测试时,我得到以下响应:
{“Type”:“User”,“message”:“无法将请求正文解析为json。”}
当我调用curl或者在浏览器中打开URL时,得到的响应与此相同
但在控制台测试调用的日志中,我看到:
Execution log for request test-request
Tue Sep 08 09:10:20 UTC 2015 : Starting execution for request: test-invoke-request
Tue Sep 08 09:10:20 UTC 2015 : API Key: test-invoke-api-key
Tue Sep 08 09:10:20 UTC 2015 : Method request path: {name=name, width=100, height=100}
Tue Sep 08 09:10:20 UTC 2015 : Method request query string: {}
Tue Sep 08 09:10:20 UTC 2015 : Method request headers: {}
Tue Sep 08 09:10:20 UTC 2015 : Method request body before transformations: null
Tue Sep 08 09:10:20 UTC 2015 : Endpoint request URI: <endpoint>:function:Magic/invocations
Tue Sep 08 09:10:20 UTC 2015 : Endpoint request headers: {
Authorization=<authorization>
Credential=<credential>,
SignedHeaders=accept;content-type;host;user-agent;x-amz-content-sha256;x-amz-date;x-amz-source-arn,
Signature=<signature>,
X-Amz-Date=20150908T091020Z,
X-Amz-Source-Arn=<ARN>/null/GET/image/{name}/{width}/{height},
Accept=application/json,
User-Agent=AmazonAPIGateway_ebkkwbbpo0,
Host=lambda.us-east-1.amazonaws.com,
X-Amz-Content-Sha256=<key>,
Content-Type=application/json
}
Tue Sep 08 09:10:20 UTC 2015 : Endpoint request body after transformations: {
"name": name,
"width" : 100,
"height" : 100,
"params": {path={name=name, width=100, height=100}, querystring={}, header={}},
"resourcePath": /image/{name}/{width}/{height},
}
Tue Sep 08 09:10:20 UTC 2015 : Endpoint response body before transformations: {"Type":"User","message":"Could not parse request body into json."}
Tue Sep 08 09:10:20 UTC 2015 : Endpoint response headers: {
x-amzn-ErrorType=InvalidRequestContentException:http://internal.amazon.com/coral/com.amazonaws.awsgirapi/,
x-amzn-RequestId=<RequestId>,
Connection=keep-alive,
Content-Length=68,
Date=Tue, 08 Sep 2015 09:10:20 GMT,
Content-Type=application/json}
Tue Sep 08 09:10:20 UTC 2015 : Method response body after transformations: {"Type":"User","message":"Could not parse request body into json."}
Tue Sep 08 09:10:20 UTC 2015 : Method response headers: {Content-Type=application/json}
Tue Sep 08 09:10:20 UTC 2015 : Successfully completed execution
请求测试请求的执行日志
2015年9月8日星期二09:10:20 UTC:开始执行请求:测试调用请求
2009年9月8日星期二09:10:20 UTC 2015:API密钥:测试调用API密钥
2015年9月8日星期二09:10:20 UTC:方法请求路径:{name=name,width=100,height=100}
2009年9月8日星期二09:10:20 UTC 2015:方法请求查询字符串:{}
2009年9月8日星期二09:10:20 UTC 2015:方法请求头:{}
2009年9月8日星期二09:10:20 UTC 2015:转换前的方法请求正文:null
2009年9月8日星期二09:10:20 UTC 2015:端点请求URI::函数:魔术/调用
2015年9月8日星期二09:10:20 UTC:端点请求标题:{
授权=
凭证=,
SignedHeaders=accept;内容类型;主机;用户代理;x-amz-content-sha256;x-amz-date;x-amz-source-arn,
签名=,
X-Amz-Date=20150908T091020Z,
X-Amz-Source-Arn=/null/GET/image/{name}/{width}/{height},
Accept=application/json,
用户代理=AmazonAPIGateway_ebkkbbpo0,
Host=lambda.us-east-1.amazonaws.com,
X-Amz-Content-Sha256=,
内容类型=应用程序/json
}
2015年9月8日星期二09:10:20 UTC:转换后的端点请求正文:{
“姓名”:姓名,
“宽度”:100,
“高度”:100,
“params:{path={name=name,width=100,height=100},querystring={},header={},
“resourcePath”:/image/{name}/{width}/{height},
}
周二9月8日09:10:20 UTC 2015:转换前的端点响应正文:{“类型”:“用户”,“消息”:“无法将请求正文解析为json。”}
2015年9月8日星期二09:10:20 UTC:端点响应标题:{
x-amzn-ErrorType=InvalidRequestContentException:http://internal.amazon.com/coral/com.amazonaws.awsgirapi/,
x-amzn-RequestId=,
连接=保持活动状态,
内容长度=68,
日期=2015年9月8日星期二09:10:20 GMT,
内容类型=应用程序/json}
2015年9月8日星期二09:10:20 UTC:转换后的方法响应正文:{“类型”:“用户”,“消息”:“无法将请求正文解析为json。”}
2015年9月8日星期二09:10:20 UTC:方法响应标题:{Content Type=application/json}
2015年9月8日星期二09:10:20 UTC:成功完成执行
正如我在某一点上看到的,URL路径被正确解析,但我不知道哪里出了问题。
另外,我不知道为什么X-Amz-Source-Arn中的路径中有空值
谢谢。问题在于集成请求映射模板。您应该对字符串类型的字段加双引号,以便以后可以将它们转换为JSON。 所以在这个例子中,你应该写:
#set($inputRoot = $input.path('$'))
{
"name": "$input.params('name')",
"width" : $input.params('width'),
"height" : $input.params('height'),
"params": "$input.params()",
"resourcePath": "$context.resourcePath",
}
我觉得很奇怪,但这就是解决办法
此外,在这种情况下,您不需要编写三个映射模板,您应该只保留
应用程序/json
问题在于集成请求映射模板。您应该对字符串类型的字段加双引号,以便以后可以将它们转换为JSON。
所以在这个例子中,你应该写:
#set($inputRoot = $input.path('$'))
{
"name": "$input.params('name')",
"width" : $input.params('width'),
"height" : $input.params('height'),
"params": "$input.params()",
"resourcePath": "$context.resourcePath",
}
我觉得很奇怪,但这就是解决办法
此外,对于这种情况,您不需要编写三个映射模板,您应该只保留
应用程序/json
如果使用路径参数进行lambda集成,则路径参数应在集成请求中映射,如下所示。转到集成响应->映射模板,将路径参数的以下映射添加到输入值:
{ "itemId": "$input.params('catalogitemid')"}
如果使用路径参数进行lambda集成,则应在集成请求中映射路径参数,如下所示。
转到集成响应->映射模板,将路径参数的以下映射添加到输入值:
{ "itemId": "$input.params('catalogitemid')"}