AWS ApiGateway请求路径映射+ lambda

Ste*_*ton 1 amazon-s3 amazon-web-services aws-lambda aws-api-gateway

我知道有很多关于映射请求数据的问题,但是没有人帮助过我.所以,我想要实现的是一个映射到lambda的API端点.当存储桶触发404时,将转发对该端点的请求,并通过请求路径将参数传递给lambda,例如:/ {image_name}/{width}/{height}.我的lambda代码只是调用context.succeed(event, context);

在方法请求配置中,自动创建请求路径的参数, 证明.

在集成请求中,我创建了三个映射模板:plain/text,plain/html,application/json,定义与bellow相同:

#set($inputRoot = $input.path('$'))
{
    "name": $input.params('name'),
    "width" : $input.params('width'),
    "height" : $input.params('height'),
    "params": $input.params(),
    "resourcePath": $context.resourcePath,
}
Run Code Online (Sandbox Code Playgroud)

当我打电话给一个chrome rest客户端时,我得到:

从控制台调用测试时,我得到以下响应:{"Type":"User","message":"Could not parse request body into 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
Run Code Online (Sandbox Code Playgroud)

正如我在某些时候看到的那样,URL路径被正确解析,但我不知道出了什么问题.另外,我不知道为什么X-Amz-Source-Arn中的路径中存在空值.

谢谢.

Ste*_*ton 9

问题是集成请求映射模板.您应该双引号字符串类型的字段,以便以后可以将它们转换为JSON.所以在这个例子中你应该写:

#set($inputRoot = $input.path('$'))
{
    "name": "$input.params('name')",
    "width" : $input.params('width'),
    "height" : $input.params('height'),
    "params": "$input.params()",
    "resourcePath": "$context.resourcePath",
}
Run Code Online (Sandbox Code Playgroud)

这对我来说似乎很奇怪,但这是解决方案.

此外,您不需要为此案例编写三个映射模板,您应该只留下 application/json