Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#如何配置AWS API网关参数以映射到基本AWS Lambda函数?_C#_Amazon Web Services_Aws Lambda_Aws Api Gateway - Fatal编程技术网

C#如何配置AWS API网关参数以映射到基本AWS Lambda函数?

C#如何配置AWS API网关参数以映射到基本AWS Lambda函数?,c#,amazon-web-services,aws-lambda,aws-api-gateway,C#,Amazon Web Services,Aws Lambda,Aws Api Gateway,一般来说,我不熟悉使用AWS和云服务,但我正在尝试创建一个后端,用于处理从DynamodDB获取的项目,如果它们不存在,则创建它们。我试图通过使用API网关调用lambda并使用lambda处理数据库上的工作来实现这一点。我创建了一种示例lambda,它扫描数据库并返回一个字符串。我是在visualstudios中使用awslambda项目来实现这一点的。当在visual Studio和lambda designer上的测试事件中为其提供字符串的示例输入时,它工作正常,并返回一个包含预期结果的字

一般来说,我不熟悉使用AWS和云服务,但我正在尝试创建一个后端,用于处理从DynamodDB获取的项目,如果它们不存在,则创建它们。我试图通过使用API网关调用lambda并使用lambda处理数据库上的工作来实现这一点。我创建了一种示例lambda,它扫描数据库并返回一个字符串。我是在visualstudios中使用awslambda项目来实现这一点的。当在visual Studio和lambda designer上的测试事件中为其提供字符串的示例输入时,它工作正常,并返回一个包含预期结果的字符串。因此,我尝试添加一个新的API作为触发器,但我不知道如何配置它以发送正确的输入。我已经为此工作了几个小时,我找不到任何关于将数据作为参数发送到lambda的信息。当我尝试在浏览器中运行api时,我得到{“消息”:“内部服务器错误”}。当我在api网关上测试它时,我得到了这个结果

Sat Mar 17 18:50:38 UTC 2018 : Endpoint response body before transformations: {
"errorType": "JsonReaderException",
"errorMessage": "Unexpected character encountered while parsing value: {. 
Path '', line 1, position 1.",
"stackTrace": [
"at Newtonsoft.Json.JsonTextReader.ReadStringValue(ReadType readType)",
"at Newtonsoft.Json.JsonTextReader.ReadAsString()",
"at 
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.ReadForType
(JsonReader reader, JsonContract contract, Boolean hasConverter)",
"at 
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize
(JsonReader reader, Type objectType, Boolean checkAdditionalContent)",
"at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, 
Type objectType)",
"at Newtonsoft.Json.JsonSerializer.Deserialize[T](JsonReader reader)",
"at Amazon.Lambda.Serialization.Json.JsonSerializer.Deserialize[T](Stream 
requestStream)",
"at lambda_method(Closure , Stream , Stream , LambdaContextInternal )"
]
}

Sat Mar 17 18:50:38 UTC 2018 : Endpoint response headers: {X-Amz-Executed- 
Version=$LATEST, x-amzn-Remapped-Content-Length=0, Connection=keep-alive, x- 
amzn-RequestId=15d89cfe-2a14-11e8-982c-db6e675f8b1d, Content-Length=939, X- 
Amz-Function-Error=Unhandled, Date=Sat, 17 Mar 2018 18:50:38 GMT, X-Amzn- 
Trace-Id=root=1-5aad637e-629010f757ae9b77679f6f40;sampled=0, Content- 
Type=application/json}
Sat Mar 17 18:50:38 UTC 2018 : Execution failed due to configuration error: 
Malformed Lambda proxy response
Sat Mar 17 18:50:38 UTC 2018 : Method completed with status: 502
下面是我的lambda的副本,我通过将其添加为触发器并将其设置为open security来配置API。我只是不明白如何将参数设置为目标输入

TESTLAMBDA
[程序集:LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]
名称空间FindItem
{
公共类函数
{
/// 
///一个简单的函数,它接受一个字符串并执行一个ToUpper
/// 
/// 
/// 
/// 
公共字符串FunctionHandler(字符串输入,ILambdaContext上下文)
{
AmazonDynamoDBClient=GetClient();
Table=GetTableObject(客户机,“存储”);
如果(表==null)
{
PauseFordBugWindow();
返回“失败”;
}
ScanFilter过滤器=新的ScanFilter();
AddCondition(“Item_name”,ScanOperator.Contains,new DynamoDBEntry[]{input});
ScanOperationConfig配置=新建ScanOperationConfig
{
AttributesToGet=新列表{“商店、商品名称、过道、价格”},
过滤器=过滤器
};
搜索=表扫描(过滤器);
List docList=新列表();
任务obj=traversedoc(文档列表,搜索);
返回目标结果;
}
/// /////////////////////////////////////////////////////////////////////////
公共异步任务遍历(ListdocList,搜索)
{
字符串astring=“”;
做
{
尝试
{
docList=await search.GetNextSetAsync();
}
捕获(例外情况除外)
{
Console.WriteLine(“\n错误:Search.GetNextStep失败,因为:”+ex.Message);
打破
}
foreach(单据列表中的var单据)
{
astring=astring+doc[“商店”]+doc[“商品名称”]+doc[“过道”]+doc[“价格”];
}
}而(!search.IsDone);
回程收缩;
}
公共静态AmazonDynamoDBClient GetClient()
{
//首先,为DynamoDB本地服务器设置DynamoDB客户端
AmazonDynamoDBConfig ddbConfig=新的AmazonDynamoDBConfig();
AmazonDynamoDBClient;
尝试
{
客户端=新的AmazondynamodClient(ddbConfig);
}
捕获(例外情况除外)
{
Console.WriteLine(“\n错误:无法创建DynamoDB客户端;”+ex.Message);
返回(空);
}
退货(客户);
}
公共静态表GetTableObject(AmazondynamodClient客户端,字符串表名)
{
Table=null;
尝试
{
table=table.LoadTable(客户机,tableName);
}
捕获(例外情况除外)
{
Console.WriteLine(“\n错误:加载'Movies'表失败;“+ex.Message”);
返回(空);
}
返回(表);
}
公共静态无效PauseFordBugWindow()
{
//如果处于调试模式,请保持控制台打开。。。
控制台。写入(“\n\n…按任意键继续”);
Console.ReadKey();
Console.WriteLine();
}
}
}

结果是Lambda代理集成已被检查。这是在集成请求中我的资源方法下。我特别确定这是怎么回事,但一旦我取消选中,我只能用post方法发送请求体。所以现在,当我只发送请求体中的文本(“milk”)时,它将作为我的输入字符串参数接收并正常运行

[assembly:LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]

namespace FindItem
{
public class Function
{
    
    /// <summary>
    /// A simple function that takes a string and does a ToUpper
    /// </summary>
    /// <param name="input"></param>
    /// <param name="context"></param>
    /// <returns></returns>
    public string FunctionHandler(string input, ILambdaContext context)
    {

        AmazonDynamoDBClient client = GetClient();

        Table table = GetTableObject(client, "Stores");
        if (table == null)
        {
            PauseForDebugWindow();
            return "Failure";
        }

        ScanFilter filter = new ScanFilter();
        filter.AddCondition("Item_name", ScanOperator.Contains, new DynamoDBEntry[] { input });
        ScanOperationConfig config = new ScanOperationConfig
        {
            AttributesToGet = new List<string> { "Store, Item_name, Aisle, Price" },
            Filter = filter
        };

        Search search = table.Scan(filter);
        List<Document> docList = new List<Document>();

        Task<String> obj = traversedoc(docList,search);

        return obj.Result;
    }


    /// /////////////////////////////////////////////////////////////////////////

    public async Task<String> traversedoc(List<Document>docList,Search search)
    {

        string astring = "";

        do
        {
            try
            {
                docList = await search.GetNextSetAsync();
            }
            catch (Exception ex)
            {
                Console.WriteLine("\n Error: Search.GetNextStep failed because: " + ex.Message);
                break;
            }
            foreach (var doc in docList)
            {

                 astring = astring + doc["Store"] + doc["Item_name"] + doc["Aisle"] + doc["Price"];

            }
        } while (!search.IsDone);

        return astring;

    }

    public static AmazonDynamoDBClient GetClient()
    {
        // First, set up a DynamoDB client for DynamoDB Local
        AmazonDynamoDBConfig ddbConfig = new AmazonDynamoDBConfig();
        AmazonDynamoDBClient client;
        try
        {
            client = new AmazonDynamoDBClient(ddbConfig);
        }
        catch (Exception ex)
        {
            Console.WriteLine("\n Error: failed to create a DynamoDB client; " + ex.Message);
            return (null);
        }
        return (client);



    }


    public static Table GetTableObject(AmazonDynamoDBClient client, string tableName)
    {
        Table table = null;
        try
        {
            table = Table.LoadTable(client, tableName);
        }
        catch (Exception ex)
        {
            Console.WriteLine("\n Error: failed to load the 'Movies' table; " + ex.Message);
            return (null);
        }
        return (table);
    }

    public static void PauseForDebugWindow()
    {
        // Keep the console open if in Debug mode...
        Console.Write("\n\n ...Press any key to continue");
        Console.ReadKey();
        Console.WriteLine();
    }


}
}