Upload a file in AWS S3 bucket using C#

Introduction

Amazon Simple Storage Service (S3) is a widely used cloud storage service provided by Amazon Web Services (AWS). It enables users to store and retrieve data from anywhere on the web. S3 is an object storage service that stores data as objects, unlike traditional file systems that store data as files. This article will explore how to upload files to an AWS S3 bucket using C#.

Prerequisites

Before we start, you need to have the following,

  • An AWS account with appropriate permissions to create an S3 bucket
  • A C# project with the AWS SDK for .NET installed

How to Create an S3 Bucket?

To upload files to an S3 bucket, we need to create a bucket first. Here are the steps to create an S3 bucket:

  1. Open the AWS Management Console and navigate to S3.
  2. Click on the "Create bucket" button.
  3. Enter a unique name for your bucket and select the region where you want to create it.
  4. Leave the default options for the remaining settings and click the "Create bucket" button.

Once the bucket is created, note down the bucket name and region, as we will need it later.

Uploading a File to S3 Bucket

To upload a file to an S3 bucket, we need to use the AWS SDK for .NET. Here are the steps to upload a file to an S3 bucket using C#.

  1. Open Visual Studio and create a new C# console application project.

  2. Install the AWS SDK for .NET using the NuGet package manager.

  3. Add the following namespaces at the top of your program.cs file.

    using Amazon.S3; 
    using Amazon.S3.Transfer;
  4. Add the following code to your Main method.

    var fileTransferUtility = new TransferUtility(new AmazonS3Client());
    
    var filePath = @"path\to\your\file"; var bucketName = "your-bucket-name"; var keyName = "your-file-name";
    
    fileTransferUtility.Upload(filePath, bucketName, keyName);
    
    Console.WriteLine("File uploaded successfully");
  5. Replace "path\to\your\file" with the actual path of the file you want to upload, "your-bucket-name" with the name of your S3 bucket, and "your-file-name" with the name you want to give to your file in the S3 bucket.

  6. Run your program. If everything goes well, you should see "File uploaded successfully" printed in the console.

 Sample Program

using Amazon.S3;
using Amazon.S3.Transfer;
using Amazon.Runtime;

class S3Uploader
{
    static void Main(string[] args)
    {
        string bucketName = "your-bucket-name";
        string keyName = "your-file-name";
        string filePath = @"C:\path\to\your\file.txt";

        // Set up your AWS credentials
        BasicAWSCredentials credentials = new BasicAWSCredentials("ACCESS_KEY", "SECRET_KEY");

        // Create a new Amazon S3 client
        AmazonS3Client s3Client = new AmazonS3Client(credentials, Amazon.RegionEndpoint.USWest2);

        try
        {
            // Upload the file to Amazon S3
            TransferUtility fileTransferUtility = new TransferUtility(s3Client);
            fileTransferUtility.Upload(filePath, bucketName, keyName);
            Console.WriteLine("Upload completed!");
        }
        catch (AmazonS3Exception e)
        {
            Console.WriteLine("Error encountered on server. Message:'{0}' when writing an object", e.Message);
        }
        catch (Exception e)
        {
            Console.WriteLine("Unknown encountered on server. Message:'{0}' when writing an object", e.Message);
        }
    }
}

In this example, you need to replace "ACCESS_KEY" and "SECRET_KEY" with your own AWS access key and secret key, respectively.

The BasicAWSCredentials object is used to authenticate your AWS credentials and is passed to the AmazonS3Client constructor. You can also specify the region you want to use by passing a RegionEndpoint object to the AmazonS3Client constructor (in this example, we're using the US West 2 region).

After setting up the credentials and creating the S3 client, the code uses the TransferUtility class to upload the file to Amazon S3. If there are any errors during the upload, the code will catch the exception and display an error message.

Conclusion

In this article, we have learned how to upload files to an AWS S3 bucket using C#. We created an S3 bucket and used the AWS SDK for .NET to upload a file to the bucket. AWS S3 is a powerful and flexible cloud storage service that can be used for various purposes, such as backup and archiving, content distribution, and data analytics. With the help of the AWS SDK for .NET, it is easy to integrate S3 with your C# applications and take advantage of its features.


Similar Articles