node.js - AWS Lambda - 在 lambda 函数中获取 API 网关路径

标签 node.js amazon-web-services aws-lambda aws-api-gateway

我有一个与 API Gateway 绑定(bind)的 Lambda 函数,并且我正在尝试从传递给 Lambda 函数的事件或上下文对象中获取路径和阶段。

AWS控制台生成的映射模板如下:

##  See http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html
##  This template will pass through all parameters including path, querystring, header, stage variables, and context through to the integration endpoint via the body/payload
#set($allParams = $input.params())
{
"body-json" : $input.json('$'),
"params" : {
#foreach($type in $allParams.keySet())
    #set($params = $allParams.get($type))
"$type" : {
    #foreach($paramName in $params.keySet())
    "$paramName" : "$util.escapeJavaScript($params.get($paramName))"
        #if($foreach.hasNext),#end
    #end
}
    #if($foreach.hasNext),#end
#end
},
"stage-variables" : {
#foreach($key in $stageVariables.keySet())
"$key" : "$util.escapeJavaScript($stageVariables.get($key))"
    #if($foreach.hasNext),#end
#end
},
"context" : {
"stage" : "$context.stage",
    "source-ip" : "$context.identity.sourceIp",
    "user" : "$context.identity.user",
    "user-agent" : "$context.identity.userAgent",
    "user-arn" : "$context.identity.userArn",
    "request-id" : "$context.requestId",
    "resource-id" : "$context.resourceId",
    "resource-path" : "$context.resourcePath",
    "account-id" : "$context.identity.accountId",
    "api-id" : "$context.apiId",
    "api-key" : "$context.identity.apiKey",
    "authorizer-principal-id" : "$context.authorizer.principalId",
    "caller" : "$context.identity.caller",
    "cognito-authentication-provider" : "$context.identity.cognitoAuthenticationProvider",
    "cognito-authentication-type" : "$context.identity.cognitoAuthenticationType",
    "cognito-identity-id" : "$context.identity.cognitoIdentityId",
    "cognito-identity-pool-id" : "$context.identity.cognitoIdentityPoolId",
    "http-method" : "$context.httpMethod"
    }
}

我试图从上下文对象中获取舞台,如下所示:

exports.handler = function(event, context) {
 console.log("Stage: " + context.stage);
 ...
}

但 logcat 显示为 Stage : undefined

我还有其他查询参数,我可以从事件对象的参数中提取这些参数,如下所示

var id = event.params.querystring.id;
var publisher_id = event.params.querystring.publisher_id;

如何使用上述映射模板从上下文中提取路径和阶段值?

最佳答案

stage 值在 event.context.stage 中可用。因此,将您的代码更改为:

exports.handler = function(event, context) {
 console.log("Stage: " + event.context.stage);
 ...
}

我们正在使用此处列出的基本映射模板:

##  See http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html
##  This template will pass through all parameters including path, querystring, header, stage variables, and context through to the integration endpoint via the body/payload
#set($allParams = $input.params())
{
"body-json" : $input.json('$'),
"params" : {
#foreach($type in $allParams.keySet())
    #set($params = $allParams.get($type))
"$type" : {
    #foreach($paramName in $params.keySet())
    "$paramName" : "$util.escapeJavaScript($params.get($paramName))"
        #if($foreach.hasNext),#end
    #end
}
    #if($foreach.hasNext),#end
#end
},
"stage-variables" : {
#foreach($key in $stageVariables.keySet())
"$key" : "$util.escapeJavaScript($stageVariables.get($key))"
    #if($foreach.hasNext),#end
#end
},
"context" : {
    "account-id" : "$context.identity.accountId",
    "api-id" : "$context.apiId",
    "api-key" : "$context.identity.apiKey",
    "authorizer-principal-id" : "$context.authorizer.principalId",
    "caller" : "$context.identity.caller",
    "cognito-authentication-provider" : "$context.identity.cognitoAuthenticationProvider",
    "cognito-authentication-type" : "$context.identity.cognitoAuthenticationType",
    "cognito-identity-id" : "$context.identity.cognitoIdentityId",
    "cognito-identity-pool-id" : "$context.identity.cognitoIdentityPoolId",
    "http-method" : "$context.httpMethod",
    "stage" : "$context.stage",
    "source-ip" : "$context.identity.sourceIp",
    "user" : "$context.identity.user",
    "user-agent" : "$context.identity.userAgent",
    "user-arn" : "$context.identity.userArn",
    "request-id" : "$context.requestId",
    "resource-id" : "$context.resourceId",
    "resource-path" : "$context.resourcePath"
    }
}

关于node.js - AWS Lambda - 在 lambda 函数中获取 API 网关路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60189031/

相关文章:

amazon-web-services - AWS S3 CLI 中的选择性文件下载

node.js - AWS Lambda 外部 URI 请求错误 : connect ETIMEDOUT

node.js - 使用 Node js Mocha 运行单个测试文件

node.js - 如何在nodejs中获取服务器变量?

javascript - 除非全局声明,否则无法从事件处理程序内的函数更新变量

node.js - 如何使用无服务器框架从另一个 lambda 异步调用 lambda

amazon-web-services - 如何使用 AWS CLI 向 AWS Lambda 函数添​​加触发器?

javascript - 绝对初学者的 Node.js/Express.j 或 Ruby on Rails

amazon-web-services - AWS ECS 跨多个实例处理 DNS 子域

c# - .Net Core (2.1) - Lambda 函数可以工作,而在 2.2 中却不行?