AWS DynamoDB - Add and Get Items in .NET Core Using the AWS SDK for .NET

In this blog post we are going to look at the Put and Get DynamoDB operations. First we will look at how to Add 'Put' an item onto a DynamoDB table, then cover how we 'Get' either a single item or all items from a DynamoDB table.

If you haven't already read my previous blog post on Creating an AWS DynamoDB Table in .NET Core that is a good place to start. We covered creating a DynamoDB client and assigned Access and Secret keys to the client. We then used the client to create a DynamoDB table.

Let's start implementing the code inside the PutItem class. This will allow us to interact with DynamoDB by adding items to our table.

PutItem.cs

Initialize the DynamoDB client at the top. Remember we created our client in Creating an AWS DynamoDB Table in .NET Core so if you are unsure how this is done, head along to that post where I explain how to set one up.

We are going to create three methods inside the PutItem class. The first will be a public method, that will simply call off to the other two methods. It will contain a call to a RequestBuilder builder, the request builder will build the request ready to be passed into our third method, PutItemAsync, this method will send the request to DynamoDB and return us back our results. This will all be past back to our first method that will then be returned back to the user.

That's it, the following will add an item that contains “Id” and “ReplyDateTime” to our Dynamo table named TempDynamoDbTable.

Next let's have a look at how we can read items from our table.

GetItem – read an item

Let’s look at how we can get an item from our dynamoDB table.

There are a couple of ways we can get items back from our table. We can either get all of the items back from our table, or we can get items based on a filter.

Controller

Let’s first create another endpoint that will allow us to Get the items from our DynamoDB table.

We are going to create the endpoint to allow us to either enter an Id as a query parameter into the endpoint, that will return us back the specified entry that relates to the Id we are searching for. We will also make it so that the id is optional, so if no Id is entered, then all items will be returned.

Summary

In this blog we have covered how we can add items to our DynamoDB table, We then looked at how we can retrieve the items with either a filter or if no filter request is passed in we then return all items.

I have create Youtube tutorials that take you through everything discussed above.