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

103,762

Solution 1

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 (subDirectoryInBucket == "" || subDirectoryInBucket == null)
        {
            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

Solution 2

The solution of @docesam is for an old version of AWSSDK. Here is an example with the latest documentation of AmazonS3:

  1. First open Visual Studio (I'm using VS2015) and create a New Project -> ASP.NET Web Application -> MVC.

  2. Browse in Manage Nuget Package , the package AWSSDK.S3 and install it.

  3. Now create a class named AmazonS3Uploader, then copy and paste this code:

    using System;
    using Amazon.S3;
    using Amazon.S3.Model;
    
    namespace AmazonS3Demo
    {
     public class AmazonS3Uploader
     {
         private string bucketName = "your-amazon-s3-bucket";
         private string keyName = "the-name-of-your-file";
         private string filePath = "C:\\Users\\yourUserName\\Desktop\\myImageToUpload.jpg";
    
    
    
         public async void UploadFile()
         {
             var client = new AmazonS3Client(Amazon.RegionEndpoint.USEast1);
    
             try
             {
                 PutObjectRequest putRequest = new PutObjectRequest
                 {
                     BucketName = bucketName,
                     Key = keyName,
                     FilePath = filePath,
                     ContentType = "text/plain"
                 };
    
                 PutObjectResponse response = await client.PutObjectAsync(putRequest);
             }
             catch (AmazonS3Exception amazonS3Exception)
             {
                 if (amazonS3Exception.ErrorCode != null &&
                     (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId")
                     ||
                     amazonS3Exception.ErrorCode.Equals("InvalidSecurity")))
                 {
                     throw new Exception("Check the provided AWS Credentials.");
                 }
                 else
                 {
                     throw new Exception("Error occurred: " + amazonS3Exception.Message);
                 }
             }
         }
     }
    }
    
  4. Edit your Web.config file adding the next lines inside of <appSettings></appSettings> :

  5. Now call your method UploadFile from HomeController.cs to test it:

     public class HomeController : Controller
     {
         public ActionResult Index()
         {
             AmazonS3Uploader amazonS3 = new AmazonS3Uploader();
    
             amazonS3.UploadFile();
             return View();
         }
     ....
    
  6. Find your file in your Amazon S3 bucket and that's all.

Download my Demo Project

Solution 3

I have written a tutorial about this.

Uploading a file to S3 bucket using low-level API:

IAmazonS3 client = new AmazonS3Client("AKI...access-key...", "+8Bo...secrey-key...", RegionEndpoint.APSoutheast2);  

FileInfo file = new FileInfo(@"c:\test.txt");  
string destPath = "folder/sub-folder/test.txt"; // <-- low-level s3 path uses /
PutObjectRequest request = new PutObjectRequest()  
{  
    InputStream = file.OpenRead(),  
    BucketName = "my-bucket-name",  
    Key = destPath // <-- in S3 key represents a path  
};  
  
PutObjectResponse response = client.PutObject(request); 

Uploading a file to S3 bucket using high-level API:

IAmazonS3 client = new AmazonS3Client("AKI...access-key...", "+8Bo...secrey-key...", RegionEndpoint.APSoutheast2);  

FileInfo localFile = new FileInfo(@"c:\test.txt");  
string destPath = @"folder\sub-folder\test.txt"; // <-- high-level s3 path uses \
 
S3FileInfo s3File = new S3FileInfo(client, "my-bucket-name", destPath);  
if (!s3File.Exists)  
{  
    using (var s3Stream = s3File.Create()) // <-- create file in S3  
    {  
        localFile.OpenRead().CopyTo(s3Stream); // <-- copy the content to S3  
    }  
}  

Solution 4

@mejiamanuel57's solution works fine for small files under 15MB. For larger files, I was getting System.Net.Sockets.SocketException: The I/O operation has been aborted because of either a thread exit or an application request. Following improved solution works for larger files (tested with 50MB file):

...
public void UploadFile()
{
    var client = new AmazonS3Client(Amazon.RegionEndpoint.USEast1);
    var transferUtility = new TransferUtility(client);

    try
    {
        TransferUtilityUploadRequest transferUtilityUploadRequest = new TransferUtilityUploadRequest
        {
            BucketName = bucketName,
            Key = keyName,
            FilePath = filePath,
            ContentType = "text/plain"
        };

        transferUtility.Upload(transferUtilityUploadRequest); // use UploadAsync if possible
    }
...

More info here.

Share:
103,762
EKanadily
Author by

EKanadily

Updated on July 08, 2022

Comments

  • EKanadily
    EKanadily almost 2 years

    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?