.NET Core code sample for AWS S3 service

By | May 27, 2019

Amazon S3 is one of most popular object storage service. As lot of enterprises moving toward cloud, one of preferred cloud provider is AWS cloud. One of such cloud service is AWS S3 which can be used to organize and manage enterprise data. Data is stored in “buckets”. AWS S3 stores objects in a flat, non-hierarchical structure and can be organized with shared names called prefixes. You can also associate object tags and meta data against S3 objects. So, combining S3 bucket names, prefix, object tags you can effectively create a great search on S3 objects.

.NET Core S3 SDK

AWS provides .NET SDK making it easier to build .NET applications that tap into reliable AWS S3 service into their business workflow. The SDK includes base libraries and documentation. However, lot of this information is scattered around and there is no single place where we can quickly find resources and build S3 solutions for .NET platform. Most of AWS documentation is sparse. In an effort to help developer community, we collected important AWS documentation links to expedite your code development around AWS services

Configuring AWS Credentials

To successfully access AWS services, you’ll need to have an AWS account and a set of valid AWS credentials. To use SDK in your applications, you’ll need at the minimum an access key and secret key. Please note, how to best manage AWS credentials/keys and IAM roles/users is all together a different topic. For sake of this article, we’ll assume you figured out credentials and access part on your own and rest of article builds on that premise.

Adding AWS client Config to .NET Core App

Considering, you have successfully obtained AWS credential, next step is to plug those credentials into application workflow.

As shown below, AWS SDK provides extensions AWSSDK.Extensions.NETCore.Setup which integrate tightly with .NET Core IConfiguration system and DI container to seamlessly validate AWS configuration and add Amazon S3 service. And also, they ensure credentials from read from designated sources like for example, %USERPROFILE%\.aws\credentials for windows unless you pass access key and secret key directly into IAmazonS3 constructor.

setting AWS client configuration in Startup.cs class
AWS client options in appsettings.json

C# wrapper for IAmazonS3 service

we wrote AwsS3HelperService helper class on top of IAmazonS3 . This helper class exposes commonly expected S3 based file operations like upload, delete, move and read files in a specific S3 bucket. Specifically, Upload function has overloads. It can accept FileStream, FilePath or inline file contents

IAwsS3HelperService.cs

Test Controller

Following is Test API controller which will consume Amazon S3 wrapper defined and becomes a kind of test harness to validate AwsS3HelperService helper class functionality

postman test

You can download full source code from this below link

Leave a Reply

Your email address will not be published. Required fields are marked *