使用aws toolkit for .net,不会在dynamodb中添加项目

时间:2017-01-09 12:38:57

标签: c# amazon-web-services amazon-dynamodb aws-lambda

我在.net的aws工具包中创建了一个配置文件,我通过上传aws函数创建了一个lambda函数。 在aws控制台中测试时的函数不会抛出任何错误。但是,数据未添加到dynamodb表中。

以下是代码段:

 public void FunctionHandler(DynamoDBEvent dynamoEvent, ILambdaContext context1)
    {
        AmazonDynamoDBClient client = new AmazonDynamoDBClient();
        var context = new DynamoDBContext(client);

        Table awsnet = Table.LoadTable(client, "bookmaster");
        context1.Logger.LogLine("In method : Function Handler : start");
        CreateBookItem(bookmaster);
    }

 private static void CreateBookItem(Table tblName)
    {
        var client = new AmazonDynamoDBClient();           
        Console.WriteLine("\n*** Executing CreateBookItem() ***");
        string sampleBookId = "3";
        var doc = new Document();
        doc["strid"] = sampleBookId;
        tblName.PutItemAsync(doc);
   }

此外,所有示例都使用" tblName.PutItem(doc)" ,但它不可用。所以我使用了" tblName.PutItemAsync(doc)"。日志行打印在aws控制台中,但数据未添加到表中。

1 个答案:

答案 0 :(得分:1)

我能够使用以下代码解决上述问题:

public void FunctionHandler(DynamoDBEvent dynamoEvent, ILambdaContext context1)
{
    AmazonDynamoDBClient client = new AmazonDynamoDBClient();
    var context = new DynamoDBContext(client);

    Table bookmaster = Table.LoadTable(client, "bookmaster");
    context1.Logger.LogLine("In method : Function Handler : start");            

    string result = PutDataAsync(bookmaster, context1).Result;
    context1.Logger.LogLine("Result = " + result);            
}

private static async Task<string> PutDataAsync(Table table , ILambdaContext context1)
{
    try
    {
        string sampleBookId = "3";
        var doc = new Document();
        doc["strid"] = sampleBookId;

        Document x = await table.PutItemAsync(doc);
        context1.Logger.LogLine("In method after put item async");
        return "success";
    }
    catch(Exception ex)
    {
        context1.Logger.LogLine("In method after put item async catch block");
        return "failed";
    }                     
}