In this post, we will only set up SDKs for S3 and SQS services, but the procedure will be the same for any other AWS service you want to use in a .NET application.
So, let's get started.
Tools & Frameworks
We will be using the below tools and frameworks in this post going forward.
- Visual Studio 2022 with
- .NET 6
Step 1: Create a new ASP.NET Core Web API project
Create a new ASP.NET Core Web API project from the available templates.
Provide some additional information such as your project name, project location, etc.Here, I am using .NET 6 LTS version with controllers option, I have not opted for minimal API. Once you have provided all the information, just press the create button.
Step 2: Install NuGet package
Now, go and install Amazon.Extensions.NETCore.Setup NuGet package.
Step 3: Install AWS Clients for S3 and SQS
In this post, we will configure SDKs for only these 2 services, but this will give you an idea of how to configure them for the rest of the services.
For S3 & SQS, install the below NuGet packages respectively:
So, by now we have installed below NuGet packages.
Step 4: ASP.NET Core dependency injection
Now, go to Program.cs
file and copy the below code there:
This is how over-all Program.cs
will look like this:
Step 5: Load AWS credentials in SDK
Go to your appsettings.Development.json
file, and provide your local AWS profile information there in the below format.
It will look like below:This is because in below code snippet, builder.Configuration.GetAWSOptions()
populates the AWSOptions.Profile
property from an above configuration file.
Alternatively, if you don't want to load the credentials from the profile (as that will work only on your local machine), you can also update AWSOptions.Credentials
property like below to have the AWS credentials.
However, even if you don't use both - AWSOptions.Profile
and AWSOptions.Crdentials
, then also SDK is smart enough to search at certain locations to find the AWS credentials. You can check out this blog to understand how AWS SDK credential loading works.
Step 6: Use S3 & SQS clients in the Controller
To use AWS S3 and SQS clients in the controller, you can write code like the below:
This is how above code will look like:That's all.
Conclusion
In this post, we learned how quickly we can set up AWS SDK in a .NET application for any AWS service that we want to use. Please let me know your thoughts and feedback in the comment section below.
Thank you !!! Happy Learning !!!
No comments:
Post a Comment