如何访问AWS Lambda中的标头? [英] How to Access header in AWS Lambda?

查看:144
本文介绍了如何访问AWS Lambda中的标头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用AWS Api Gateway. 我已经使用API​​网关创建了资源和方法.

I am using AWS Api Gateway. I have created resource and method using API Gateway.

我创建了Lambda函数,用于生成json格式的签名URL,以通过CloudFront访问s3存储桶.

I have created Lambda function for generating signed URL in json format to access s3 bucket via CloudFront.

当我使用GET方法调用lambda函数时.我将"channekID"作为查询字符串传递.我也想发送X-API-Key自定义标头进行授权.

When I call lambda function using GET method. I pass "channekID" as a querystring. I want to send X-API-Key custom header as well for authorization.

我尝试了很多事情,但没有找到任何解决方案. 如何在Lambda函数中发送自定义标头? 在访问Lambda中的标头值之后 如何使用x-api-key授权?

I have tried lot things but did not find any solution. How to send custom header in Lambda function?? and after accessing header value in Lambda How to authorize using x-api-key ?

推荐答案

您无法使用Lambda访问标头.但是您可以做的是在Api Gateway中创建一个映射模板,该模板将标头值放在事件对象中.

You cannot access the header using Lambda. But what you can do is in the Api Gateway create a mapping template that puts a header value in the event object.

标头应位于可在映射模板中使用的$input.params(x)变量中.请参阅完整文档,以了解如何完全集成此文档

The header should be in the $input.params(x) variable that can be used in the mapping template. See the full documentation of how to exactly integrate this.

更新:在您的映射模板中(在api网关->您的端点->集成请求下),添加以下内容:

update: in your mapping template (under api gateway -> your endpoint -> integration request), add something like this:

#set($inputRoot = $input.path('$'))
{
  "apikey" : "$input.params('X-Api-Key')"
}

现在,您可以在event.apikey下的lambda函数中访问api键(我没有对此进行测试,但是我们在生产中使用了类似的东西).请注意,您可以对所有标头变量以及正文中的变量执行此操作.

Now you can access the api key in the lambda function under event.apikey (I did not test this, but we use something similar in production). Note that you can do this for all header variables and also variables in the body.

这篇关于如何访问AWS Lambda中的标头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆