向AWS Lambda函数传递参数

4
我有一个Lambda函数,应该接收3个参数。
public async Task<string> FunctionHandler(string pName, string dictName, ILambdaContext context)
{
//code...
}

我正在使用Visual Studio 2015,将其发布到AWS环境中,那么我需要在示例输入框中输入什么来调用此函数? enter image description here
1个回答

9

个人而言,我还没有在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 值,并按照以下示例代码使用:

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字符串或对象来传递需要的多个变量。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接