-
Notifications
You must be signed in to change notification settings - Fork 220
/
Copy pathPutItemConditional.cs
69 lines (63 loc) · 2.52 KB
/
PutItemConditional.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Amazon;
using Amazon.DynamoDBv2;
using Amazon.DynamoDBv2.Model;
namespace DotnetSamples.WorkingWithItems
{
public class PutItemConditional
{
private readonly IAmazonDynamoDB amazonDynamoDB;
public PutItemConditional()
{
amazonDynamoDB = new AmazonDynamoDBClient(RegionEndpoint.USWest2);
}
public PutItemConditional(IAmazonDynamoDB amazonDynamoDB)
{
this.amazonDynamoDB = amazonDynamoDB;
}
public async Task ServiceClientExampleAsync()
{
try
{
var request = new PutItemRequest
{
TableName = "RetailDatabase",
Item = new Dictionary<string, AttributeValue>
{
{ "pk", new AttributeValue("[email protected]") },
{ "sk", new AttributeValue("metadata") },
{ "name", new AttributeValue("Jim Bob") },
{ "first_name", new AttributeValue("Jim") },
{ "last_name", new AttributeValue("Bob") },
{
"address",
new AttributeValue
{
M = new Dictionary<string, AttributeValue>
{
{ "road", new AttributeValue("456 Nowhere Lane") },
{ "city", new AttributeValue("Langely") },
{ "state", new AttributeValue("WA") },
{ "pcode", new AttributeValue("98260") },
{ "country", new AttributeValue("USA") },
}
}
},
{ "username", new AttributeValue("jbob") },
},
// this condition expression will not allow updates, it will only succeed if the record does not already exist
ConditionExpression = "attribute_not_exists(sk)",
};
var response = await amazonDynamoDB.PutItemAsync(request);
Console.WriteLine($"PutItemConditional succeeded.");
}
catch (Exception e)
{
Console.Error.WriteLine(e.Message);
throw;
}
}
}
}