使用 aws toolkit for .net 不在 dynamodb 中添加项目
Items are not added in dynamodb using aws toolkit for .net
我已经在 aws 工具包中为 .net 创建了一个配置文件,并且我正在通过上传 aws 函数来创建一个 lambda 函数。
在 aws 控制台中测试时该函数不会抛出任何错误。但是,数据未添加到 dynamodb table。
这是代码片段:
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 控制台中,但未在 table.
中添加数据
我能够使用以下代码解决上述问题:
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";
}
}
我已经在 aws 工具包中为 .net 创建了一个配置文件,并且我正在通过上传 aws 函数来创建一个 lambda 函数。 在 aws 控制台中测试时该函数不会抛出任何错误。但是,数据未添加到 dynamodb table。
这是代码片段:
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 控制台中,但未在 table.
中添加数据我能够使用以下代码解决上述问题:
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";
}
}