Search code examples
c#.netamazon-web-servicesamazon-s3

How to upload a file to amazon S3 super easy using c#


I am tired of all these "upload to S3" examples and tutorials that don't work , can someone just show me an example that simply works and is super easy?


Solution

  • well here are the instruction that you have to follow to get a fully working demo program ...

    1-Download and install the Amazon web services SDK for .NET which you can find in (http://aws.amazon.com/sdk-for-net/). because I have visual studio 2010 I choose to install the 3.5 .NET SDK.

    2- open visual studio and make a new project , I have visual studio 2010 and I am using a console application project.

    3- add reference to AWSSDK.dll , it is installed with the Amazon web service SDK mentioned above , in my system the dll is located in "C:\Program Files (x86)\AWS SDK for .NET\bin\Net35\AWSSDK.dll".

    4- make a new class file ,call it "AmazonUploader" here the complete code of the class:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Amazon;
    using Amazon.S3;
    using Amazon.S3.Transfer;
    
    namespace UploadToS3Demo
    {
        public class AmazonUploader
        {
            public bool sendMyFileToS3(string localFilePath, string bucketName, string subDirectoryInBucket, string fileNameInS3)
            {
            // input explained :
            // localFilePath = the full local file path e.g. "c:\mydir\mysubdir\myfilename.zip"
            // bucketName : the name of the bucket in S3 ,the bucket should be alreadt created
            // subDirectoryInBucket : if this string is not empty the file will be uploaded to
                // a subdirectory with this name
            // fileNameInS3 = the file name in the S3
    
            // create an instance of IAmazonS3 class ,in my case i choose RegionEndpoint.EUWest1
            // you can change that to APNortheast1 , APSoutheast1 , APSoutheast2 , CNNorth1
            // SAEast1 , USEast1 , USGovCloudWest1 , USWest1 , USWest2 . this choice will not
            // store your file in a different cloud storage but (i think) it differ in performance
            // depending on your location
            IAmazonS3 client = Amazon.AWSClientFactory.CreateAmazonS3Client(RegionEndpoint.EUWest1);
    
            // create a TransferUtility instance passing it the IAmazonS3 created in the first step
            TransferUtility utility = new TransferUtility(client);
            // making a TransferUtilityUploadRequest instance
            TransferUtilityUploadRequest request = new TransferUtilityUploadRequest();
    
            if (string.IsNullOrEmpty(subDirectoryInBucket))
            {
                request.BucketName = bucketName; //no subdirectory just bucket name
            }
            else
            {   // subdirectory and bucket name
                request.BucketName = bucketName + @"/" + subDirectoryInBucket;
            }
            request.Key = fileNameInS3 ; //file name up in S3
            request.FilePath = localFilePath; //local file name
            utility.Upload(request); //commensing the transfer
    
            return true; //indicate that the file was sent
        }
      }
    }
    

    5- add a configuration file : right click on your project in the solution explorer and choose "add" -> "new item" then from the list choose the type "Application configuration file" and click the "add" button. a file called "App.config" is added to the solution.

    6- edit the app.config file : double click the "app.config" file in the solution explorer the edit menu will appear . replace all the text with the following text :

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <appSettings>
        <add key="AWSProfileName" value="profile1"/>
        <add key="AWSAccessKey" value="your Access Key goes here"/>
        <add key="AWSSecretKey" value="your Secret Key goes here"/>
    
      </appSettings>
    </configuration>
    

    you have to modify the above text to reflect your Amazon Access Key Id and Secret Access Key.

    7- now in the program.cs file (remember this is a console application) write the following code :

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    namespace UploadToS3Demo
    {
        class Program
        {
            static void Main(string[] args)
            {
                // preparing our file and directory names
                string fileToBackup = @"d:\mybackupFile.zip" ; // test file
                string myBucketName = "mys3bucketname"; //your s3 bucket name goes here
                string s3DirectoryName = "justdemodirectory";
                string s3FileName = @"mybackupFile uploaded in 12-9-2014.zip";
    
                AmazonUploader myUploader = new AmazonUploader();
                myUploader.sendMyFileToS3(fileToBackup, myBucketName, s3DirectoryName, s3FileName);
            }
        }
    }
    

    8- replace the strings in the code above with your own data

    9- add error correction and your program is ready