【问题标题】:Items are not added in dynamodb using aws toolkit for .net使用 .net 的 aws 工具包未在 dynamodb 中添加项目
【发布时间】:2017-05-23 17:30:46
【问题描述】:

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

这里是代码sn-p:

 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 控制台中,但数据未添加到表中。

【问题讨论】:

  • 你能检查一下 tblName.PutItemAsync(doc) 函数的响应是什么

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


【解决方案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";
    }                     
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-04
    • 2021-07-10
    • 2018-12-02
    • 2019-04-04
    • 2019-01-06
    • 2014-12-22
    • 1970-01-01
    相关资源
    最近更新 更多