Home > Uncategorized > #AWS #S3 upload with C# and HttpPostedFile

#AWS #S3 upload with C# and HttpPostedFile


Uploading a file to AWS S3 via C# has been documented a million times, but I thought I’d put my own version here, a simple upload from a HTML page, a bit of ajax, and an ASPX file to upload the file to S3, and return a unique file name.

Firstly, you need to include the Nuget package with Install-Package AWSSDK.S3

Here’s the javascript:

$(document).ready(function(e) {
(function(e) {
url: “/api/imageUpload.aspx”, // Url to which the request is sent
type: “POST”, // Type of request to be send, called as method
data: new
this), // Data sent to server, a set of key/value pairs (i.e. form fields and values)
contentType: false, // The content type used when sending data to the server.
cache: false, // To unable request pages to be cached
processData: false, // To send DOMDocument or non processed data file it is set to false
success: function(data) // A function to be called if request succeeds

Then, here is the api/imageUpload.aspx file codebehind (with keys removed)

var MyFiles = Request.Files;
for (int l = 0; l < MyFiles.Count; l++)
if (MyFiles.GetKey(l) != “file”) continue;
var file = MyFiles.Get(“file”);
var strExt = Path.GetExtension(file.FileName);
var filename = Guid.NewGuid().ToString(“D”) + strExt;
var cfg = new AmazonS3Config {RegionEndpoint = Amazon.RegionEndpoint.EUWest1};
const string bucketName = “xxxxxx”;
var s3Client = new AmazonS3Client(“xxxxx”, “xxxxx”, cfg);
var fileTransferUtility = new TransferUtility(s3Client);
var fileTransferUtilityRequest = new TransferUtilityUploadRequest
BucketName = bucketName,
InputStream = file.InputStream,
StorageClass = S3StorageClass.ReducedRedundancy,
Key = filename,
CannedACL = S3CannedACL.PublicRead
Response.Write(“https://s3-eu-west-1.amazonaws.com/xxxxxx/&#8221; + filename);

Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: