.NET on AWS – Persisting Application Data to S3 (Part 2)
In part two of this two-part series, we will build a REST API using .NET to perform CRUD operations for application data storage on S3.
Join the DZone community and get the full member experience.Join For Free
In the previous post, we discussed AWS S3 service and its various use cases. We then set up an AWS S3 bucket with configurations and access for our web application data storage requirements. We created a .NET6 WebAPI project and some basic wiring/configuration to allow our application to access S3. However, we still have to write application code that will allow our user to store notes data (files) in an S3 bucket and read the information from these files as well for processing (CRUD operations).
In this post, we will look into how to perform these operations from our .NET application code, along with the use of AWS SDK for S3.
If you haven’t already, I will suggest you read the previous post, as we will be building on that foundation.
AWSSDK.S3 nuget Packages
AWS SDK, available as nuget package, can simplify the code that is needed to interact with Amazon S3 service. We can add it to our solution using the package manager, as shown below:
You can notice that SDK is very modular, and this structure helps us to import only the package we need for our requirements instead of importing a lot of non-related code.
We have a very simple domain model for our application. The main entity is Note which represents an individual note from a user. We also have another entity NoteSummary which, as the name implies, stores summary information about notes.
Here are the model class for these entities:
Next step, to store and retrieve domain models to and from the S3 bucket, our application needs some code, and that service is defined as the following interface:
Following is the
S3NoteStorageService implementation for this interface:
As you can see, this implementation uses the
IAmazonS3 object, which is the abstraction provided by AWSSDK.S3 nuget package we added earlier.
We’ll not go into the details of these methods' code. The code is self-explanatory, and you can check it from this GitHub repository.
Also, I added the following line in the Program.cs file to enable Dependency injection of this service in our application:
Next, let's focus on the Controller side to wire all this up.
The following screenshot shows the
As you can see that
INotesStorageService is injected via controller injection, and we can now use this service in the various action methods as needed to interact with the S3 service.
My plan is to later update this application with users' logins, but for the sake of simplicity, here I’ve hard-coded the user, so all the notes will be saved under this user for now.
Let's see the code for getting a list of notes for a user:
Again, the code here is self-explanatory and very typical controller code, and you can check the code of all the methods in detail from the GitHub repository.
Testing the REST API
Ok, with all these in place, I started the application and used the Postman client tool to test the application.
This is the request payload to add a note. Once executed, we shall have the following files created in the S3 bucket.
So, here we have one file for the note itself and the other file for the summary.
Try adding more notes using Postman and see more files will be created in your bucket:
Next, we can test the API for List notes action as shown below:
As you can see, we are getting data from all the notes.
In a similar way, we can test the delete operation by providing NoteID, as shown below:
Here is the API call to get single Note details by providing a NoteId:
You can check the source code from this git repository.
In this post, we covered the .NET application code for our notes application and how it uses AWSSDK to interact with S3. AWSDK simplifies our code by providing us abstractions for easy use in our application code.
We build a very simple REST API that allows us to perform CRUD operations on our domain model.
Our application can now benefit from all the great features of the S3 storage service, and we can easily integrate it with other AWS services for more advanced use cases.
Let me know if you have some comments or questions. Till next time, Happy Coding.
Published at DZone with permission of Jawad Hasan Shani. See the original article here.
Opinions expressed by DZone contributors are their own.
Apache Kafka vs. Message Queue: Trade-Offs, Integration, Migration
A Complete Guide to AWS File Handling and How It Is Revolutionizing Cloud Storage
Build a Simple Chat Server With gRPC in .Net Core