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.
- 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.
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:
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.