C# S3 Examples

Creating a Connection

This creates a connection so that you can interact with the server.

  1. using System;
  2. using Amazon;
  3. using Amazon.S3;
  4. using Amazon.S3.Model;
  5. string accessKey = "put your access key here!";
  6. string secretKey = "put your secret key here!";
  7. AmazonS3Config config = new AmazonS3Config();
  8. config.ServiceURL = "objects.dreamhost.com";
  9. AmazonS3Client s3Client = new AmazonS3Client(
  10. accessKey,
  11. secretKey,
  12. config
  13. );

Listing Owned Buckets

This gets a list of Buckets that you own. This also prints out the bucket name and creation date of each bucket.

  1. ListBucketsResponse response = client.ListBuckets();
  2. foreach (S3Bucket b in response.Buckets)
  3. {
  4. Console.WriteLine("{0}\t{1}", b.BucketName, b.CreationDate);
  5. }

The output will look something like this:

  1. mahbuckat1 2011-04-21T18:05:39.000Z
  2. mahbuckat2 2011-04-21T18:05:48.000Z
  3. mahbuckat3 2011-04-21T18:07:18.000Z

Creating a Bucket

This creates a new bucket called my-new-bucket

  1. PutBucketRequest request = new PutBucketRequest();
  2. request.BucketName = "my-new-bucket";
  3. client.PutBucket(request);

Listing a Bucket’s Content

This gets a list of objects in the bucket. This also prints out each object’s name, the file size, and last modified date.

  1. ListObjectsRequest request = new ListObjectsRequest();
  2. request.BucketName = "my-new-bucket";
  3. ListObjectsResponse response = client.ListObjects(request);
  4. foreach (S3Object o in response.S3Objects)
  5. {
  6. Console.WriteLine("{0}\t{1}\t{2}", o.Key, o.Size, o.LastModified);
  7. }

The output will look something like this:

  1. myphoto1.jpg 251262 2011-08-08T21:35:48.000Z
  2. myphoto2.jpg 262518 2011-08-08T21:38:01.000Z

Deleting a Bucket

Note

The Bucket must be empty! Otherwise it won’t work!

  1. DeleteBucketRequest request = new DeleteBucketRequest();
  2. request.BucketName = "my-new-bucket";
  3. client.DeleteBucket(request);

Forced Delete for Non-empty Buckets

Attention

not available

Creating an Object

This creates a file hello.txt with the string "Hello World!"

  1. PutObjectRequest request = new PutObjectRequest();
  2. request.BucketName = "my-new-bucket";
  3. request.Key = "hello.txt";
  4. request.ContentType = "text/plain";
  5. request.ContentBody = "Hello World!";
  6. client.PutObject(request);

Change an Object’s ACL

This makes the object hello.txt to be publicly readable, and secret_plans.txt to be private.

  1. PutACLRequest request = new PutACLRequest();
  2. request.BucketName = "my-new-bucket";
  3. request.Key = "hello.txt";
  4. request.CannedACL = S3CannedACL.PublicRead;
  5. client.PutACL(request);
  6. PutACLRequest request2 = new PutACLRequest();
  7. request2.BucketName = "my-new-bucket";
  8. request2.Key = "secret_plans.txt";
  9. request2.CannedACL = S3CannedACL.Private;
  10. client.PutACL(request2);

Download an Object (to a file)

This downloads the object perl_poetry.pdf and saves it in C:\Users\larry\Documents

  1. GetObjectRequest request = new GetObjectRequest();
  2. request.BucketName = "my-new-bucket";
  3. request.Key = "perl_poetry.pdf";
  4. GetObjectResponse response = client.GetObject(request);
  5. response.WriteResponseStreamToFile("C:\\Users\\larry\\Documents\\perl_poetry.pdf");

Delete an Object

This deletes the object goodbye.txt

  1. DeleteObjectRequest request = new DeleteObjectRequest();
  2. request.BucketName = "my-new-bucket";
  3. request.Key = "goodbye.txt";
  4. client.DeleteObject(request);

Generate Object Download URLs (signed and unsigned)

This generates an unsigned download URL for hello.txt. This works because we made hello.txt public by setting the ACL above. This then generates a signed download URL for secret_plans.txt that will work for 1 hour. Signed download URLs will work for the time period even if the object is private (when the time period is up, the URL will stop working).

Note

The C# S3 Library does not have a method for generating unsigned URLs, so the following example only shows generating signed URLs.

  1. GetPreSignedUrlRequest request = new GetPreSignedUrlRequest();
  2. request.BucketName = "my-bucket-name";
  3. request.Key = "secret_plans.txt";
  4. request.Expires = DateTime.Now.AddHours(1);
  5. request.Protocol = Protocol.HTTP;
  6. string url = client.GetPreSignedURL(request);
  7. Console.WriteLine(url);

The output of this will look something like:

  1. http://objects.dreamhost.com/my-bucket-name/secret_plans.txt?Signature=XXXXXXXXXXXXXXXXXXXXXXXXXXX&Expires=1316027075&AWSAccessKeyId=XXXXXXXXXXXXXXXXXXX