C# Upload File to AWS S3 Bucket

C# Upload File to AWS S3 Bucket

In this post I’m going to show you how to upload files to an AWS S3 Bucket, if you don’t know what that is you can check out the AWS support pages for S3Buckets, but in the long and short of things its a file storage system hosted in the AWS Cloud Services portfolio.

Before we start you will need an AWS account, which is free but will require a credit card. Once you have signed up for an AWS account the first thing we are going to do is create an S3 bucket.

Create an S3 Bucket

Within the AWS portal, select the Services link, then select one of the S3 links, or key in ‘S3’ into the search bar. then select the S3 link.

AWS S3 Service

Within the S3 portal, select the Create Bucket button.

S3 Create Bucket

Within the Create Bucket page, key-in a new bucket name and then select a region to host the bucket.

S3 General Configuration

Then scroll down to the Default Encryption section and enable the Server-Side Encryption, and use the default Encryption Key Type – ‘Amazon S3 key (SSE-S3).

S3 Bucket Encryption

Select the ‘Create Bucket’ button at the bottom of the page, to complete the creation of the bucket.

Create S3 Bucket

AWS Access keys

Next we have to create some security credentials to allow the C# application to access the bucket. To do this, within the AWS portal in the upper right-hand corner select the dropdown next to your username, and then select ‘My Security Credentials‘ from the dropdown menu.

AWS Security Credentials

Expand the Access Keys section and then select ‘Create new Access Key’.

AWS Create new Access Key

Either take note of the new Access keys or download the file, then select close.

AWS Access Key Generation

Building the C# Application

Within Visual Studio or your IDE of choice, create a new solution and add a Dot.Net Core ‘Console Application’, then create a new ‘Project Folder’ as shown below:

Solution Struture

Within the Project Folder create two new classes; AmazonUploader, and SecretConsumer.

New Classes

Next, well add the NuGet packages required, ensure that the six packages shown below are added:

NuGet Packages

This is not the recommended, way in which to store secrets, but was simpler than the inbuilt secrets manager within visual studio core (but this was really complex, and still not secure) later on I will show you how to use the AWS Secrets manager, which is the preferred method when working with the AWS infrastructure.

For now add a .JSON file to the Project folder called ‘appSecrets.json’. and construct the JSON file shown below, filling in the AWS Secret Access keys generated earlier.

appSecrets.json

To recover the two secrets well create the code for the SecretsConsumer, which we will use to recover the keys from the json file.

C#

Next well create the code for the Uploader;

C#

Testing the Application

Finally we can add the code to the console application to test the application, we have to provide the:

  • The local file path to the file being copied to the S3 bucket.
  • The name of the S3 bucket.
  • If there are sub folders within the bucket, then we have to define the directory path, in this case we don’t.
  • and the name of the file, once it’s copied to the S3 bucket.
C#

If we execute the console application we can see that the file has been copied from my local machine to the S3 bucket.

Uploaded File in S3 Bucket