The shortest ever S3 C#/SOAP/WCF client

Just the code

Download the sample project from:

AmazonS3SoapWcfSample.zip, 30KB, 8/4/2009

The sample is self-contained. You don’t need to follow any of the text in this post to build and use it. If trying to replicate the sample’s behavior in a different program, take a look at step 2 below regarding adding the Amazon S3 service reference

The shortest ever S3 C#/SOAP/WCF client

I put this together as an answer to a question on stackoverflow. What’s the simplest, most straight-forward way to access the Amazon S3 service from a C# program? To me, the obstacle has always been with authenticating the requests. You have to dig it out of Amazon’s sample code set up in the days before WCF. So here’s the shortest I could get it to:

  1. Step 1: Start Visual Studio 2008, create a new C# Windows console application.

  2. Step 2: Add the S3 WSDL as a service reference. In Solution Explorer, right click References, select Add Service Reference. Type in the S3 WSDL address in the Address box: http://s3.amazonaws.com/doc/2006-03-01/AmazonS3.wsdl. Click Go. “AmazonS3” should show in the Services box. Enter a namespace. I entered Amazon.S3. Click OK.

  3. Step 3: Modify Program.cs to look something like the following:

using System;
using System.Globalization;
using System.Text;
using System.Security.Cryptography;
using AmazonS3SoapWcfSample.Amazon.S3;

namespace AmazonS3SoapWcfSample {
	class Program {
		private const string accessKeyId     = "YOURACCESSKEYIDHERE0";
		private const string secretAccessKey = "YOURSECRETACCESSKEYHEREANDYESITSTHATLONG";

		public static DateTime LocalNow() {
			DateTime now = DateTime.Now;
			return new DateTime(
				now.Year, now.Month, now.Day,
				now.Hour, now.Minute, now.Second,
				now.Millisecond, DateTimeKind.Local);
		}

		public static string SignRequest(string secret, string operation, DateTime timestamp) {
			HMACSHA1 hmac         = new HMACSHA1(Encoding.UTF8.GetBytes(secret));
			string   isoTimeStamp = timestamp.ToUniversalTime().ToString(
				"yyyy-MM-ddTHH:mm:ss.fffZ",
				CultureInfo.InvariantCulture);
			string   signMe       = "AmazonS3" + operation + isoTimeStamp;
			string   signature    = Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(signMe)));
			return signature;
		}

		static void Main(string[] args) {
			DateTime       now    = LocalNow();
			AmazonS3Client client = new AmazonS3Client();

			var result = client.ListAllMyBuckets(
				accessKeyId,
				now,
				SignRequest(secretAccessKey, "ListAllMyBuckets", now));

			foreach (var bucket in result.Buckets) {
				Console.WriteLine(bucket.Name);
			}
		}
	}
}

If you now insert your access key ID and secret access key in the appropriate spots and run the program, you should get a listing of your S3 buckets. The AmazonS3Client class has all the SOAP operations available as instance methods on it.

Works for you? Have an idea how to make it even shorter? –leave a comment.

Advertisements

5 Responses to The shortest ever S3 C#/SOAP/WCF client

  1. jonny says:

    I can get the list of bucket,but I can’t get the content of bucket and execute the method of putObject,I got a error “malformed request” when I run it.

    • Nick says:

      @jonny, if you want to list items in a bucket you must write additional code. You need to add a using Amazon.S3.Model. Then make a new instance of the ListObjectsRequest class. Like so:
      DateTime now = LocalNow();
      AmazonS3Client client = new AmazonS3Client(accessKeyId,secretAccessKey);
      ListObjectsRequest request = new ListObjectsRequest();
      request.BucketName=”bucketname”;
      var result= client.ListObjects(request);
      foreach (var bucket in result.S3Objects)
      {
      spanHey.InnerHtml += bucket.Key +””;
      }

  2. Liping says:

    How at use PutObject() to upload a large file with attachment? Thank you!

  3. Anonymous says:

    nice maannn….!!!!

  4. Anonymous says:

    Very useful – much appreciated.

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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: