Using C# to upload a file to AWS S3 Part 2: Creating the C# Console App

This post follows on from part 1.  With the AWS S3 objects in place it is now time to create a simple C# console application that will upload a text file stored locally to the AWS S3 bucket.

The first step is to create a test file that you want to upload. In my example, I have created a text file in the Downloads folder called TheFile.txt which contains some text. After creating the text file, note the name of the file and its location.

Start Visual Studio and create a new console application

Use NuGet to add the AWSSDK.S3 package. At the time of writing this was at version

Add the following to App.config

   <add key="AWSProfileName" value="dotnet">
   <add key="AWSAccessKey" value="YOUR ACCESS KEY">
   <add key="AWSSecretKey" value="YOUR SECRET KEY">

You will find the values for the access key and secret key in the accessKeys.csv which you downloaded in part one of the tutorial.

Create a new class called S3Uploader and paste the following code ensuring you change the variables for bucketName, keyName and filePath as appropriate. As you can see from the comments, this code is based on this answer from Stack Overflow.

For the sake of brevity the code deliberately does not have any exception handling nor unit tests as I wanted this example to focus purely on the AWS API without any other distractions.

using Amazon.S3;
using Amazon.S3.Model;

namespace S3FileUploaderGeekOut

  /// <summary>
  /// Based upon
  /// </summary>
  public class S3Uploader
    private string bucketName = "myimportantfiles";
    private string keyName = "TheFile.txt";
    private string filePath = @"C:\Users\Ian\Downloads\TheFile.txt";

    public void UploadFile()
      var client = new AmazonS3Client(Amazon.RegionEndpoint.EUWest2);
      PutObjectRequest putRequest = new PutObjectRequest
        BucketName = bucketName,
        Key = keyName,
        FilePath = filePath,
        ContentType = "text/plain"

      PutObjectResponse response = client.PutObject(putRequest);

In the Program.cs class add the following:

namespace S3FileUploaderGeekOut
  class Program
    static void Main(string[] args)
      S3Uploader s3 = new S3Uploader();


Run the program and once it completes, navigate to your S3  Bucket via the AWS console and you will be able to see that your file has been successfully uploaded.


In this and the previous post I have demonstrated the steps required to upload a text file from a simple C# console application to a AWS bucket.