问题描述
我有一个用.NET Core(C#)编写的简单Lambda函数,该函数使用APIGatewayProxyRequest
对象来遍历所有请求属性.
如果我测试了该lambda函数(来自AWS Lambda),并向其传递了包含基本信息的示例事件配置:
我可以这样获得此信息:
public string FunctionHandler(APIGatewayProxyRequest request, ILambdaContext context)
logger.Logger.Log($"Body: {request.Body} \n");
logger.Logger.Log($"Path: {request.Path} \n");
logger.Logger.Log($"Resource: {request.Resource} \n");
如何从相同数据访问自定义上下文或授权者值:
我尝试过:
logger.Logger.Log($"RequestContext Authorizor: {request.RequestContext.Authorizer} \n");
包括它的不同属性(StringKey
,PrincipleId
等)
从Node.js看来,只需使用以下命令即可实现:
event.requestContext.authorizer.customKey
C#中没有这样的东西吗?
因此,在花了3天的时间对其进行故障排除并在AWS工程师的帮助下,这是我所发现的;
- 通过Lambda函数访问
$context
,$authorizer
或任何其他custom variables
是有限制的,该函数是通过C#
中的.Net Core编写的.
为此正在为AWS团队创建新的服务请求.
说明:
当前,在node.js中,您可以访问传递给Lambda函数的数据的整个有效负载(在event
参数中),该函数包括所有自定义变量(您可以直接访问它-对于问题示例,例如:event.requestContext.authorizer.customKey
.
这与C#等效项不同-使用 APIGatewayProxyRequest 在Lambda函数中的请求对象.因此,尽管您可以在C#中访问节点内的整个有效负载(包括所有自定义变量),但您只能访问 APIGatewayProxyRequest 对象.可以找到其属性这里:
或者简而言之:
public string Body { get; set; }
public IDictionary<string, string> Headers { get; set; }
public string HttpMethod { get; set; }
public bool IsBase64Encoded { get; set; }
public string Path { get; set; }
public IDictionary<string, string> PathParameters { get; set; }
public IDictionary<string, string> QueryStringParameters { get; set; }
public ProxyRequestContext RequestContext { get; set; }
public string Resource { get; set; }
public IDictionary<string, string> StageVariables { get; set; }
基于对象,这将不允许访问自定义或未知"属性,即使它们是有效负载的一部分.
长话短说,截至目前:如果您希望使用任何类型的自定义变量,则可能需要通过node(event)/python对其进行编码,或者可能会覆盖对象./p>
更新:
有一种方法可以访问进入的数据的整个有效负载:
I have a simple Lambda function written in .NET Core (C#) that uses the APIGatewayProxyRequest
object to go through all the request properties.
If I test this lambda function (from AWS Lambda), and pass it a sample event config that contains basic information:
I can get this information like so:
public string FunctionHandler(APIGatewayProxyRequest request, ILambdaContext context)
logger.Logger.Log($"Body: {request.Body} \n");
logger.Logger.Log($"Path: {request.Path} \n");
logger.Logger.Log($"Resource: {request.Resource} \n");
How is it that I can access custom context or authorizer values from the same data:
I have tried:
logger.Logger.Log($"RequestContext Authorizor: {request.RequestContext.Authorizer} \n");
Including it's different properties (StringKey
, PrincipleId
etc.)
It seems from Node.js, this would be simply achieved by using this:
event.requestContext.authorizer.customKey
There is no such thing in C#?
So, after spending 3 days troubleshooting this and with the help of the AWS engineers, this is what I've found;
- There is a limitation with accessing the
$context
,$authorizer
or any othercustom variables
from a Lambda function, written through .Net Core inC#
A new service request is being created for the AWS team for this.
To explain:
Currently, in node.js you have access to the entire payload of data being passed through to the Lambda function (within the event
parameter), which includes all custom variables (you could access it directly - for the question example, like this: event.requestContext.authorizer.customKey
.
This is not the same for the C# equivalent - which uses the APIGatewayProxyRequest request object within a Lambda function. So although you have access to the entire payload (including all the custom variables) within node, within C#, you only have access to the APIGatewayProxyRequest object. Properties of which can be found here:
Or in short:
public string Body { get; set; }
public IDictionary<string, string> Headers { get; set; }
public string HttpMethod { get; set; }
public bool IsBase64Encoded { get; set; }
public string Path { get; set; }
public IDictionary<string, string> PathParameters { get; set; }
public IDictionary<string, string> QueryStringParameters { get; set; }
public ProxyRequestContext RequestContext { get; set; }
public string Resource { get; set; }
public IDictionary<string, string> StageVariables { get; set; }
Being based off an object, this will not allow access to custom or "unknown" properties, even though they are part of the payload.
Long story short, as of right now: if you wish you work with custom variables of any sort, you would either need to code it through node(event) / python, or possibly overwrite an existing property within the APIGatewayProxyRequest object.
UPDATE:
There is a work around to accessing the entire payload of the data coming in:
这篇关于AWS Lambda C#-访问自定义上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!