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?
6 Answers
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

- 171
- 1
- 9

- 3,831
- 3
- 35
- 33
-
2thanks for answer, but returning true to indicate operation success is terrible and unnecessary in this example. – Igor Yalovoy Sep 22 '15 at 19:30
-
so you suggest raising exception if any error ,right ? – EKanadily Oct 15 '15 at 10:25
-
10Steps 1 and 3 can be replaced by getting AWSSDK with NuGet. – lahjaton_j Dec 22 '15 at 07:06
-
3Download link is broken. – lahjaton_j Dec 22 '15 at 09:00
-
9This answer is a little outdated, the AWSSDK was the big assembly that held all Aws logic. In Aws 3.0, there are many smaller assemblies for each feature of Aws you want to use. Awssdk.s3 is probably what you want to use instead of awssdk. I think awssdk still exists though, although parts of the API have changed. – Roger Hill Jul 09 '16 at 00:20
-
Thank you for the detailed answer. Worked perfectly! – Najeeb Apr 20 '18 at 17:41
-
1How do you get the response data, i.e. the URL of the object – chumberjosh May 20 '20 at 19:19
-
This does not cover the configuration part, S3 permissions, policy, IAM role which are important for the production environment to work – Naga Feb 19 '21 at 07:45
-
Could we use it as singleton and share the client (AmazonS3Client) over more threads? – aKiRa Apr 02 '21 at 10:36
The solution of @docesam is for an old version of AWSSDK. Here is an example with the latest documentation of AmazonS3:
First open Visual Studio (I'm using VS2015) and create a New Project -> ASP.NET Web Application -> MVC.
Browse in Manage Nuget Package , the package AWSSDK.S3 and install it.
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); } } } } }
Edit your Web.config file adding the next lines inside of
<appSettings></appSettings>
: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(); } ....
Find your file in your Amazon S3 bucket and that's all.

- 5,348
- 3
- 19
- 25

- 6,807
- 1
- 32
- 34
-
with your solution how can compress image and then after upload image on server FilePath = filePath here any idea? – coderwill Aug 01 '17 at 08:09
-
i have try your code and it's work fine but i need to before upload image compress and then after upload image but how can do that i have not idea so can you please help me – coderwill Aug 01 '17 at 09:29
-
Coderwill, try the API of https://tinypng.com/ , maybe that's what are you looking for. – mejiamanuel57 Aug 03 '17 at 15:27
-
1Thanks for the give a replay and your solution is awsome i have try and that is done very well. – coderwill Aug 04 '17 at 07:06
-
Hello i need your help. i am try with your solution in my local to cdn iamge upload work very well but i have deploy my code on server and try on the server to image upload on cdn image is not upload on cdn server and i am not getting any error but image is not uplaoded can you pleae help me – coderwill Aug 14 '17 at 08:48
-
Hi @coderwill, talk to me directly through email, mejiamanuel57@gmail.com, Explain me the details of your problem and if you can add some images. – mejiamanuel57 Aug 15 '17 at 00:28
-
4
-
1against which version is this code tested ? i have this in the csproj file (
) and it's giving the error: error CS0122: 'AmazonS3Client.PutObject(PutObjectRequest)' is inaccessible due to its protection level – jmiguel77 Apr 10 '19 at 23:57 -
@Nick @jmiguel77: You need to call `await AmazonS3Client.PutObjectAsync(PutObjectRequest)` with the latest version. – FranzHuber23 May 31 '19 at 07:46
-
@mejiamanuel57 Your solution works perfectly for me, thank you. I have a question though, I'm trying replicate this in a DLL. I've taken your AmazonS3Uploader file, added it to my project and called it from an exe. The result of this is the exe completes, presents like it was successful, but nothing actually gets uploaded to the S3 bucket. Is there something about your solution that is specific to a web service? – Barry Piccinni Oct 26 '19 at 22:19
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
}
}

- 14,480
- 11
- 70
- 137
-
This is what I needed, thanks! Also, could you explain when to use the low-level API vs the high-level API? Thanks. – chris Oct 23 '22 at 02:29
-
2@chris: please see the last paragraph in this [tutorial](https://www.c-sharpcorner.com/blogs/working-with-files-and-folders-in-s3-using-aws-sdk-for-net). Low-level and high-level APIs are essentially different syntax doing the same thing, pick whichever you prefer. Personally I prefer the high-level syntax. – Hooman Bahreini Oct 23 '22 at 03:37
@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.

- 2,140
- 1
- 26
- 26
-
Nice, long time without using my own approach but I will give a try to your solution. Thanks! – mejiamanuel57 Oct 27 '19 at 22:07
The example on the AWS site worked for me: https://docs.aws.amazon.com/AmazonS3/latest/dev/HLuploadFileDotNet.html
Although it was set to a different region which returned an error:
//private static readonly RegionEndpoint bucketRegion = RegionEndpoint.USWest2; private static readonly RegionEndpoint bucketRegion = RegionEndpoint.USWest1;
I set up my bucket with Northern California, which is USWest1.

- 695
- 6
- 6
@mejiamanuel57 and @HoomanBahreini covers it very well but i would still like to show the official sample code from AWS SDK Code Examples:
https://docs.aws.amazon.com/sdk-for-net/v3/developer-guide/welcome.html
https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/dotnetv3/S3
https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/dotnetv3/S3/S3_Basics/S3Bucket.cs#L45
/// <summary>
/// Shows how to upload a file from the local computer to an Amazon S3
/// bucket.
/// </summary>
/// <param name="client">An initialized Amazon S3 client object.</param>
/// <param name="bucketName">The Amazon S3 bucket to which the object
/// will be uploaded.</param>
/// <param name="objectName">The object to upload.</param>
/// <param name="filePath">The path, including file name, of the object
/// on the local computer to upload.</param>
/// <returns>A boolean value indicating the success or failure of the
/// upload procedure.</returns>
public static async Task<bool> UploadFileAsync(
IAmazonS3 client,
string bucketName,
string objectName,
string filePath)
{
var request = new PutObjectRequest
{
BucketName = bucketName,
Key = objectName,
FilePath = filePath,
};
var response = await client.PutObjectAsync(request);
if (response.HttpStatusCode == System.Net.HttpStatusCode.OK)
{
Console.WriteLine($"Successfully uploaded {objectName} to {bucketName}.");
return true;
}
else
{
Console.WriteLine($"Could not upload {objectName} to {bucketName}.");
return false;
}
}
This is a modified service I wrote to upload json strings as json files both with sync and async methods:
public class AwsS3Service
{
private readonly IAmazonS3 _client;
private readonly string _bucketName;
private readonly string _keyPrefix;
/// <param name="bucketName">The Amazon S3 bucket to which the object
/// will be uploaded.</param>
public AwsS3Service(string accessKey, string secretKey, string bucketName, string keyPrefix)
{
BasicAWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
_client = new AmazonS3Client(credentials);
_bucketName=bucketName;
_keyPrefix=keyPrefix;
}
public bool UploadJson(string objectName, string json, int migrationId, long orgId)
{
return Task.Run(() => UploadJsonAsync(objectName, json, migrationId, orgId)).GetAwaiter().GetResult();
}
public async Task<bool> UploadJsonAsync(string objectName, string json, int migrationId, long orgId)
{
var request = new PutObjectRequest
{
BucketName = $"{_bucketName}",
Key = $"{_keyPrefix}{migrationId}/{orgId}/{objectName}",
InputStream = new MemoryStream(Encoding.UTF8.GetBytes(json)),
};
var response = await _client.PutObjectAsync(request);
if (response.HttpStatusCode == System.Net.HttpStatusCode.OK)
{
return true;
}
else
{
return false;
}
}
}

- 62,132
- 37
- 328
- 418