.NET on AWS – Persisting Application Data to S3 (Part-2)

Introduction

In previous post, we discussed AWS S3 service and its various use cases.

We then setup 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 which will allow our user to store notes data (files) in 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 operation from our .NET application code along with use of AWS SDK for S3.

If you haven’t already, I will suggest you to read the previous post, as we will be building on that foundation.

AWSSDK.S3 nuget Packages

AWS SDK available as nuget package can simplify code which is needed to interact with Amazon S3 service. We can add it to our solution using package manager as shown below

You can notice that SDK is very modular and this structure help us to import only that package we need for our requirements instead of importing a lot of non-related code.

Domain Model

We have a very simple domain model for our application. The main entity is Note which represent an individual note from a user.

We also have another entity NoteSummary which as name implies stores summary information about notes.

Here are the model class for these entities

Storage Service

Next step, to store and retrieve domain models to and from S3 bucket, our application needs some code and that service is defined as following interface

Following is the S3NoteStorageService implementation for this interface

as you can see that this is implementation is using 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. Code is self explanatory and you can check it from this GitHub repository.

Also, I added following line in Program.cs file to enable Dependency injection of this service in our application:

Next, lets focus on Controller side to wire all this up.

API Controller

Following screenshot shows the NotesController code.

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 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.

Lets see code for Getting List of notes for a user

Again, the code here is self explanatory and very typical controller code and you can check code of all the methods it in details from GitHub repository.

Testing the REST API

Ok, with all these in place, I started the application and used postman client tool to test the application.

Adding Note

This is the request payload to add a note. Once executed, we shall have following files created in S3 bucket

So here we have one file for the note itself and the other file for summary.

Try add more notes using postman and see more files will be created in your bucket:

List Notes

Next, we can test the API for List notes action as shown below

as you can see that we are getting data of all the notes.

Delete Note

Similar way we can test the delete operation by providing NoteID as shown below

Get Note

and here is the API call to get single Note details by providing a NoteId

You can check the source-code from this git repository.

Summary

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 which allows us to perform CRUD operations on our domain model.

Our application can now benefit from all the great features of 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.


Discover more from Hex Quote

Subscribe to get the latest posts sent to your email.

Discover more from Hex Quote

Subscribe now to keep reading and get access to the full archive.

Continue reading