参数传递给AWS lambda函数
问题描述:
我有一个lambda函数,它假设取3个参数参数传递给AWS lambda函数
public async Task<string> FunctionHandler(string pName, string dictName, ILambdaContext context)
{
//code...
}
答
就我个人而言,我没有在Lambda入口点尝试使用异步任务,因此无法对此进行评论。
然而,另一种方式去了解它是lambda函数入口点更改为:
public async Task<string> FunctionHandler(JObject input, ILambdaContext context)
然后拉两个变量出来,像这样:
string dictName = input["dictName"].ToString();
string pName = input["pName"].ToString();
然后在您输入的AWS Web控制台:
{
"dictName":"hello",
"pName":"kitty"
}
或者,您也可以采用JObject值并使用i t,如以下示例代码所示:
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
namespace SimpleJsonTest
{
[TestClass]
public class JsonObjectTests
{
[TestMethod]
public void ForgiveThisRunOnJsonTestJustShakeYourHeadSayUgghhhAndMoveOn()
{
//Need better names than dictName and pName. Kept it as it is a good approximation of software potty talk.
string json = "{\"dictName\":\"hello\",\"pName\":\"kitty\"}";
JObject jsonObject = JObject.Parse(json);
//Example Zero
string dictName = jsonObject["dictName"].ToString();
string pName = jsonObject["pName"].ToString();
Assert.AreEqual("hello", dictName);
Assert.AreEqual("kitty", pName);
//Example One
MeaningfulName exampleOne = jsonObject.ToObject<MeaningfulName>();
Assert.AreEqual("hello", exampleOne.DictName);
Assert.AreEqual("kitty", exampleOne.PName);
//Example Two (or could just pass in json from above)
MeaningfulName exampleTwo = JsonConvert.DeserializeObject<MeaningfulName>(jsonObject.ToString());
Assert.AreEqual("hello", exampleTwo.DictName);
Assert.AreEqual("kitty", exampleTwo.PName);
}
}
public class MeaningfulName
{
public string PName { get; set; }
[JsonProperty("dictName")] //Change this to suit your needs, or leave it off
public string DictName { get; set; }
}
}
问题是我不知道在AWS Lambda中是否可以有两个输入变量。赔率是你不能。除此之外,如果您坚持使用json字符串或对象来传递所需的多个变量,那么这可能是最好的选择。