CoderJony

Configuring the AWS SDK for .NET with .NET Core

In this post, we will step by step understand how to configure and set up a .NET application to use AWS SDK.

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

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.

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

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 the overall Program.cs will look like:

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"
    }
}

It will look like the below:

Configuring the AWS SDK for .NET with .NET Core

This is because, in the below code snippet, builder.Configuration.GetAWSOptions() populates the AWSOptions.Profile property from the 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.Credentials, 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<IActionResult> TestMethod()
        {
            var s3Response = await _s3Client.ListObjectsAsync("yourBucketName");

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

            return Ok();
        }
    }
}

This is what the above code will look like:

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

References

Buy Me A Coffee