Wednesday, June 15, 2022

Configuring the AWS SDK for .NET with .NET Core

 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.Configuring the AWS SDK for .NET with .NET Core

Provide some additional information such as your project name, project location, etc.Configuring the AWS SDK for .NET with .NET CoreHere, 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.Configuring the AWS SDK for .NET with .NET Core

Step 2: Install NuGet package

Now, go and install Amazon.Extensions.NETCore.Setup NuGet package.Configuring the AWS SDK for .NET with .NET Core

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:

Install-Package AWSSDK.S3
Install-Package AWSSDK.SQS
PowerShell

So, by now we have installed below NuGet packages.Configuring the AWS SDK for .NET with .NET Core

Step 4: ASP.NET Core dependency injection

Now, go to Program.cs file and copy the below code there:

// Get the AWS profile information from configuration providers
AWSOptions awsOptions = builder.Configuration.GetAWSOptions();

// Configure AWS service clients to use these credentials
builder.Services.AddDefaultAWSOptions(awsOptions);

// These AWS service clients will be singleton by default
builder.Services.AddAWSService<IAmazonS3>();
builder.Services.AddAWSService<IAmazonSQS>();

This is how over-all Program.cs will look like this:Configuring the AWS SDK for .NET with .NET Core

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.

{
  "AWS": {
    "Profile": "local-test-profile",
    "Region": "us-west-2"
  }
}
JSON

It will look like below:Configuring the AWS SDK for .NET with .NET CoreThis is because in below code snippet, builder.Configuration.GetAWSOptions() populates the AWSOptions.Profile property from an above configuration file.

AWSOptions awsOptions = builder.Configuration.GetAWSOptions();
builder.Services.AddDefaultAWSOptions(awsOptions);

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.

AWSOptions awsOptions = new AWSOptions 
{
   Credentials = new BasicAWSCredentials("yourAccessKey", "yourAccessSecret")
};
builder.Services.AddDefaultAWSOptions(awsOptions);

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:

using Amazon.S3;
using Amazon.SQS;
using Microsoft.AspNetCore.Mvc;

namespace AWSSDK.Setup.Demo.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        private readonly IAmazonS3 _s3Client;
        private readonly IAmazonSQS _sqsClient;

        public UserController(IAmazonS3 s3Client, IAmazonSQS sqsClient)
        {
            _s3Client = s3Client;
            _sqsClient = sqsClient;
        }

        [Route("[action]")]
        public async Task TestMethod()
        {
            var s3Response = await _s3Client.ListObjectsAsync("yourBucketName");

            var sqsResponse = await _sqsClient.CreateQueueAsync("yourQuueName");

            return Ok();
        }
    }
}

This is how above code will look like:Configuring the AWS SDK for .NET with .NET CoreThat'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