Coding Geek

A blog by Nadeem Iqbal

Upload file Directly to Amazon S3 using C# via POST

There are scenarios when you want to directly upload the file from the client browser/application to S3.

Amazon provides simple POST method to upload files to amazon File Upload. That method is not acceptable when you want to upload the file(s) programatically using any programming language such as C# or by using the Silverlight.

C# doesn't provide the support of  "multipart/form-data" form posting which is required to upload directly to the S3. So, HttpWebRequest class can be configured so that it will send the request in the required format.

 So, I've written a custom class which will generate the request contents in "multipart/form-data" format.

Below is the code how to use that custom class and upload to the amazon S3 directly using C#/SilverLight

UploadFile uf = new UploadFile("file.txt", "file", "txt/html"); // file should be in bin directory
            string []a = new string[5];
            UploadFile[] files = new UploadFile[1];
            files[0] = uf;

            NameValueCollection form = new NameValueCollection();
            form["key"] = "file.txt";
            form["acl"] = "public-read";
            form["success_action_redirect"] = "http://www.yahoo.com";
           
            form["x-amz-meta-uuid"] = "14365123651274";
            form["x-amz-meta-tag"] = "";
            form["AWSAccessKeyId"] = "zzzzzzzz";
            form["Policy"] = "zzzzzzzzzzzzzzzzzzzzzzzz";
            form["Signature"] = "zzzzzzzzzzzzzzzzzzzzzzzzz";
           
            
            string url = "http://MyBucket.s3.amazonaws.com/";

            string resultQuery = HttpUploadHelper.Upload(url, files, form);
            Console.WriteLine(resultQuery);
 

CHEERS :)

Comments

niqbal said:

All code is in attachment

# June 9, 2009 1:08 AM

niqbal said:

For SilverLight, Asynchronous methods should be use BeginGetResponse, EndGetResponse etc..

# June 10, 2009 6:08 AM

James Auld said:

This code doesn't work with Silverlight 2.0 it seems. I've replaced the NameValueCollection with Dictionary<string,string> instead, which will probably be ok, but now there are problems with the HTTPWebRequest not working. It doesn't contain a contentlength, and GetRequestStream() and GetResponse() are not in Silverlight 2.0. Do you have an updated version of this - I'm needing to upload large files to S3 from Silverlight. Anyone have another idea?

Thanks!!

# June 21, 2009 9:58 PM

niqbal said:

There isn't methods like GetRequestStream but there are methods in Silverlight BeginGetRequestStream, EndGetRequestStream.. So you need to use the asynchronous methos.

Content length silverlight automatically sets to the request header

# July 28, 2009 4:53 AM

krystalware.com said:

# August 18, 2009 5:10 AM

romb said:

> There are scenarios when you want to directly upload the file from the client browser/application to S3.

The code attached doesn't upload directly :) it uploads to a server (where this code runs) first and to s3 after that.

# September 27, 2009 4:17 PM

niqbal said:

This code is for Desktop application..

Secondly if you want to upload through web application then you can use the JavaScript / HTML POST

# October 22, 2009 11:15 AM

Pol37 said:

Look towards players from Madrid. ,

# October 22, 2009 3:15 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)