ThreeSharp - A C# Library for Amazon S3 and Amazon CloudFront

Sample Code & Libraries>CloudFront>ThreeSharp A C# Library for Amazon S3 and Amazon CloudFront
Community Contributed Software

  • Amazon Web Services provides links to these packages as a convenience for our customers, but software not authored by an "@AWS" account has not been reviewed or screened by AWS.
  • Please review this software to ensure it meets your needs before using it.

An open-source C# library and sample code for working with Amazon S3, providing full support for data streaming, data encryption, thread safety, live statistics, and more. Also supports Amazon CloudFront.

Details

Submitted By: affirma
AWS Products Used: Amazon S3
Language(s): C#
License: Apache License 2.0
Created On: October 9, 2007 3:41 PM GMT
Last Updated: November 18, 2008 3:54 PM GMT
Download

About This Sample

  • An open-source C# library and sample code for working with Amazon S3.
  • Features include:
    • Full support for data streaming. No need to load data into memory before sending to Amazon S3.
    • Data encryption.
    • Thread safety and live statistics. Perform multiple simultaneous uploads and downloads and show progress in real-time.
    • A powerful, unified object model that simplifies maintenance and extension.
  • Join the project or download the code from the project's web site on CodePlex: http://www.codeplex.com/ThreeSharp.

Prerequisites

  • You must have a valid Amazon Web Services developer account.
  • You must be signed up to use Amazon S3.

Using the Project

ThreeSharp interacts with Amazon S3 through REST requests. Both requests and responses are streamed. To model this, ThreeSharp provides an object for each type of request or response, and a query object which works with these request and response objects.

This is best illustrated with an example. The first thing we want to do is set up a config object
and pass it to our query object.

 ThreeSharpConfig config = new ThreeSharpConfig();
 config.AwsAccessKeyID = awsAccessKeyId;
 config.AwsSecretAccessKey = awsSecretAccessKey;
 IThreeSharp service = new ThreeSharpQuery(config);

Notice that the ThreeSharpQuery class is fulfilling the IThreeSharp interface contract. If you
were developing an application that used the ThreeSharp library, but didn't want to actually
talk to Amazon S3 during development, you could build a mock object that also implemented IThreeSharp.

Now, suppose we wanted to perform a streamed retrieve of a file, "Example.zip", in a bucket,
"TestBucket", from Amazon S3. First, we would want to instantiate an ObjectGetRequest. (This
object will be interpreted into a request to get an object. Pretty simple!) So we would write:

 ObjectGetRequest request = new ObjectGetRequest("TestBucket", "Example.zip");

We then pass our request to the query object, which will return a response.

 ObjectGetResponse response = service.ObjectGet(objectGetRequest);

This response contains a data stream, which we can stream to disk.

 response.StreamResponseToFile("c:\\Example.zip");

One thing to note is that all responses in this model have data streams, and you will have problems
if these streams aren't closed. The StreamResponseToFile method closes the stream when finished, but
if you do not use the stream from a response, you will need to close it manually. This can be done by:

 response.DataStream.Close();

All the operations supported by ThreeSharp proceed in the manner outlined here. Examples of each call
can be found in ConsoleSample app, with explanatory comments. We have tried to make the ThreeSharp code
readable and self-explanatory, as well as powerful. However, the best way to polish a framework is
through actual use, so comments and feedback are always welcome.

©2014, Amazon Web Services, Inc. or its affiliates. All rights reserved.