Picturesocial - How to use DynamoDB on a containerized API
Image recognition sounds like some high tech computer science topic, and it is. Fortunately, there are tools that abstract the complexity of creating your own algorithms into a REST API. In this post, you are going to learn how to add image recognition to your Picturesocial app with an API.
Jose Yapur
Amazon Employee
Published Feb 27, 2023
Last Modified Apr 25, 2024
This is an 8-part series about Picturesocial:
So far you have been learning about containers, Kubernetes, Infrastructure as a Code and even intelligent services like Amazon Rekognition. Now it’s time to add a database to store the needed data models that will be used on our social media platform.
I choose Amazon DynamoDB because the Picturesocial application needs a modern database that will support high throughput and simplify the overall data management by providing an API for all the Data Management and Operations. Also because I'm using document data structures that will be better working on a non-relational and document DB as DynamoDB. But first I need to figure out the relevant field for the data structure in the APIs that I have done so far.
In a previous post about how to analyze images with Machine Learning, I created an API that returned the Labels from a photo using Amazon Rekognition. The most relevant attributes for Picturesocial are: 1/ An ID to correlate the image with the Labels, 2/ Image Name, 3/ Label List (top 5), and 4/ User who uploaded the image. Now I'm going to convert those attributes into a Data Model, using JSON, that I will follow for the whole API.
That Data Model with real data will look like this in our API. This is what we call a Data Object, in this case using JSON.
Now that we have our data model, let’s surf some of the CRUD API specs to make our Pictures API to follow the best practices. CRUD stands for Create, Read, Update and Delete, which are the basic operations that you usually do with a Data Object, and we will align those operations into HTTP Methods like this:
- Create → POST
- Read → GET
- Update → PUT
- Delete → DELETE
This way we don’t have to know much about the method names, we just know what it does just following the HTTP Method and understanding the Data Model. You can learn about the different HTTP Methods and purposes by visiting the Restful documentation here.
Now we are going to put all this together and start coding!
- An AWS Account.
- If you are using Linux or macOS, you can continue to the next bullet point. If you are using Microsoft Windows, I suggest you to use WSL2.
- Install Git.
- Install AWS CLI 2.
- Install .NET 6.
- Or
If this is your first time working with AWS CLI or you need a refresher on how to set up your credentials, I suggest you follow this step-by-step guide of how to configure your local AWS environment. In this same guide, you can also follow steps to configure AWS Cloud9, as that will be very helpful if you don’t want to install everything from scratch.
- First, we are going to create the DynamoDB Table needed for this walkthrough, this table will be named Pictures. We will use the Terminal of our preference with AWS CLI 2 installed and logged in.
- When we execute the command, we are going to get a JSON response with the table structure. We just need to type
:q
and press Enter to finish. The command:q
is what we use to exit the current view of the terminal when we are in text mode. This also apply to console text editors like VIM.
- Now the table is ready for the code! Start by cloning the sample repository and choosing the "Ep7" branch. This repo contains all the code needed for replicate this walkthrough.
- Before we can code, we need to add the AWS Packages to our project by going to Pictures folder and running the following commands, which were explored on our previous posts and the one new AWSSDK.DynamoDBv2
- We are going to open the cloned repository in VS Code and explore the file PictureTable.cs, which contains the classes PictureTable and PictureTableRequest. The first is the one that defines the Table Data Model and the second is the Payload Data Model for creating new items.
- We start by adding the definition of DynamoDBTable and the name of the table on top of the PictureTable Class by using
[DynamoDBTable("**pictures**")]
. Also, we define which attribute of the class is the Key of the Schema by using[DynamoDBHashKey]
on top of the declaration and for the other attributes we just set the name as will appear on the JSON document like this[DynamoDBProperty("**nameOfTheAttribute**")]
- The second class will be much simpler as it only is the Payload Data Model, where we define the photo, bucket and user name that will be send to create a new element.
- Open the Controller Picture by going to Controllers/PictureController.cs, there we have 3 Methods: Create, Read and Delete using the corresponding HTTP Method.
- On top of the class, we declare the DynamoDB variables and the Data Model variable. And using the constructor method we are going to initialize them, which will use the default AWS profile from your shell's environment.
- We are not adding an Authorize header to the controller because this will be handled by Amazon API Gateway and will be explored with more detail in the next episode.
- We declared, on top of the class, the DynamoDB variables and the Data Model variable. And using the constructor method we are going to initialize them. They are going to read the AWS profile from the environment to authenticate.
- We explored Amazon Rekognition in our previous post, so for now I will focus only on the added parts of the API to store the information on DynamoDB. First we declare the Create method as HttpPost and parse the request from the PictureTableRequest class that we explored earlier. The next step is to create a PictureTable object and fill it with the results from the Amazon Rekognition detection, and then use the global variable context to save asynchronously the object into the database. As you can see this is quite straightforward and doesn't add much custom logic to our methods; it's one line of code.
- We are going to follow similar steps for the Read method using HTTP Get, where we use the Table Key to retrieve values. In this case we are reading the ID from the query string in the URL and returning the corresponding Data Object from DynamoDB by using the context and the method LoadAsync.
- And finally the Delete method using HTTP Delete, where we also use the LoadAsync method to get the Data Object and then the DeleteAsync method will take the model object as a parameter to describe the object that needs to be deleted.
- The final API will look like this:
- We are now ready to test it locally by running on our Terminal the following command:
- We are going to test it using the built-in Swagger server. Go to your browser and type the following URL. The Swagger endpoint will only work when you test it locally.
- There we can see the auto documented Data Model and Methods and also test them.
- I will create a new Picture by going to the POST option and creating a payload using the PictureTableRequest Model, in my case I will use the name of a picture and bucket that exists to test it.
- The result will be a view similar to the following:
- The most important part of the response is the id. We are going to copy that and use it to test the Read method.
- As we can see, we get exactly the same information that we had using the Create method, with the difference that this time we are retrieving the object directly from the Database and not evaluating the image through Amazon Rekognition. This also improved significantly the response time from the Read vs the Create.
We are making a good progress in our journey to create Picturesocial. So far we have learned how to create an API, containerized it, deployed a Kubernetes cluster, used infrastructure as a code and deployed our API to Kubernetes. Finally we learned how to add Database support!
The next post will be about exposing our APIs to the Internet using API Gateway and VPC Link!
The next post will be about exposing our APIs to the Internet using API Gateway and VPC Link!
Any opinions in this post are those of the individual author and may not reflect the opinions of AWS.