PutItemAsync() public method

Initiates the asynchronous execution of the PutItem operation.
public PutItemAsync ( Document doc, AmazonDynamoDBCallback callback, AsyncOptions asyncOptions = null ) : void
doc Document Document to save.
callback AmazonDynamoDBCallback The callback that will be invoked when the asynchronous operation completes.
asyncOptions Amazon.Runtime.AsyncOptions An instance of AsyncOptions that specifies how the async method should be executed.
return void
示例#1
0
 private static async Task InsertData(Table table, string id, string body)
 {
     await table.PutItemAsync(new Document(new Dictionary<string, DynamoDBEntry>
     {
         ["Id"] = new Primitive(id),
         ["Body"] = new Primitive(body)
     }));
 }
        public async Task AddNewItem(string tableName, JArray item)
        {
            var      tableRepo = new TableRepo(new AmazonDynamoDBClient());
            Document tableAttr = await tableRepo.GetTableAttr(tableName);

            List <string> attribute = new List <string> {
            };
            List <string> type      = new List <string> {
            };
            List <string> key       = new List <string> {
            };

            if (tableAttr != null)
            {
                var c = tableAttr["attr"].AsDocument().GetAttributeNames();
                foreach (var c1 in c)
                {
                    attribute.Add(c1);
                    var t = tableAttr["attr"].AsDocument()[c1].AsDocument()["type"];
                    var k = tableAttr["attr"].AsDocument()[c1].AsDocument()["key"];
                    type.Add(t);
                    key.Add(k);
                }
            }
            List <Models.Attribute> attr = new List <Models.Attribute> {
            };

            for (int i = 0; i < attribute.Count; i++)
            {
                attr.Add(new Models.Attribute {
                    attrName = attribute[i], type = type[i], key = key[i]
                });
            }
            Amazon.DynamoDBv2.DocumentModel.Table table = Amazon.DynamoDBv2.DocumentModel.Table.LoadTable(dynamoDB, tableName);
            var  itm  = new Document();
            bool flag = false;

            foreach (var value in item)
            {
                if (attribute.Contains(value["key"].ToString()) is true)
                {
                    switch (type[attribute.IndexOf(value["key"].ToString())])
                    {
                    case "N":
                        itm[value["key"].ToString()] = (float)value["value"];
                        break;

                    case "B":
                        byte[]       byteArray = Encoding.ASCII.GetBytes(value["value"].ToString());
                        MemoryStream stream    = new MemoryStream(byteArray);
                        itm[value["key"].ToString()] = stream;
                        break;

                    default:
                        itm[value["key"].ToString()] = value["value"].ToString();
                        break;
                    }
                }
                else
                {
                    flag = true;
                    attribute.Add(value["key"].ToString());
                    if (IsNumeric(value["value"].ToString()) is true)
                    {
                        itm[value["key"].ToString()] = (float)value["value"];
                        type.Add("N");
                        attr.Add(new Models.Attribute {
                            attrName = value["key"].ToString(), type = "N", key = "n"
                        });
                    }
                    else
                    {
                        itm[value["key"].ToString()] = value["value"].ToString();
                        type.Add("S");
                        attr.Add(new Models.Attribute {
                            attrName = value["key"].ToString(), type = "S", key = "n"
                        });
                    }
                }
            }
            await table.PutItemAsync(itm);

            if (flag is true)
            {
                var ar  = JArray.FromObject(attr);
                var obj = new JObject {
                    new JProperty("attr", ar)
                };
                await tableRepo.UpdateAttributeAndType(tableName, obj);
            }
        }