Introduction
This article describes Amazon S3 from the C# developer point of view. It shows how to access Amazon S3 service from C#, what operations can be used, and how they can be programmed.
- ListBucketsResponse response = client.ListBuckets();
- bool found = false;
- foreach(S3Bucket bucket in response.Buckets) {
- if (bucket.BucketName == BUCKET_NAME) {
- found = true;
- break;
- }
- }
- if (found == false) {
- client.PutBucket(new PutBucketRequest().WithBucketName(BUCKET_NAME));
- }

- PutObjectRequest request = new PutObjectRequest();
- request.WithBucketName(BUCKET_NAME);
- request.WithKey(S3_KEY);
- request.WithContentBody("This is body of S3 object.");
- client.PutObject(request);
- PutObjectRequest request = new PutObjectRequest();
- request.WithBucketName(BUCKET_NAME);
- request.WithKey(S3_KEY);
- request.WithFilePath(pathToFile);
- client.PutObject(request);

- GetObjectRequest request = new GetObjectRequest();
- request.WithBucketName(BUCKET_NAME);
- request.WithKey(S3_KEY);
- GetObjectResponse response = client.GetObject(request);
- StreamReader reader = new StreamReader(response.ResponseStream);
- string content = reader.ReadToEnd();
- CopyObjectRequest request = new CopyObjectRequest();
- request.DestinationBucket = BUCKET_NAME;
- request.DestinationKey = S3_KEY;
- request.Directive = S3MetadataDirective.REPLACE;
- NameValueCollection metadata = new NameValueCollection();
- // Each user defined metadata must start from "x-amz-meta-"
- metadata.Add("x-amz-meta-test", "Test data");
- request.AddHeaders(metadata);
- request.SourceBucket = BUCKET_NAME;
- request.SourceKey = S3_KEY;
- client.CopyObject(request);
Amazon S3 does not have a special API call to associate metadata with an S3 object. Instead of it the copy method should be called.
But S3 API has a special method for reading metadata:
Let's see through EC2Studio add-in that the metadata have been assigned:
HTTP access.
And the good news is that S3 allows accessing S3 objects not only by API calls but directly by HTTP (so each S3 object has URL that can be used to access it by any web browser). You can use S3 as a simple static HTTP server, where you can host your static web content.
- GetObjectMetadataRequest request = new GetObjectMetadataRequest();
- request.WithBucketName(BUCKET_NAME).WithKey(S3_KEY);
- GetObjectMetadataResponse response = client.GetObjectMetadata(request);
- foreach(string key in response.Metadata.AllKeys) {
- Console.Out.WriteLine(" key: " + key + ", value: " + response.Metadata[key]);
- }

- GetPreSignedUrlRequest request = new GetPreSignedUrlRequest().WithBucketName(BUCKET_NAME).WithKey(S3_KEY);
- request.WithExpires(DateTime.Now.Add(new TimeSpan(7, 0, 0, 0)));
- string url = client.GetPreSignedURL(request));



As I mentioned earlier Amazon S3 has a feature to define access. There are 2 types of access: by user id/email or by URL (it means predefined groups or users):
So you can define a read or write access and define who is permitted to read and write ACL for any S3 object or bucket.
There is a special API call to set of reading ACLs, for example, an owner of S3Object can be found:
See more details at http://docs.amazonwebservices.com/AmazonS3/latest/dev/UsingAuthAccess.html.
Versions
Another cool feature of Amazon S3 is versioning. You can turn versioning on for a bucket and when you put any object into it, it will not be simply replaced, but the new version of the object will be created and stored under the same key. So you will be able to access and manage all versions (modifications) of the object.

- GetACLResponse response = client.GetACL(new GetACLRequest().WithBucketName(BUCKET_NAME).WithKey(S3_KEY));
- Console.Out.WriteLine("Object owner is " + response.AccessControlList.Owner.DisplayName);
- ListVersionsResponse response = client.ListVersions(new ListVersionsRequest().WithBucketName(BUCKET_NAME).WithPrefix(S3_KEY));
- Console.Out.WriteLine("Found the following versions for prefix " + S3_KEY);
- foreach(S3ObjectVersion version in response.Versions) {
- Console.Out.WriteLine(" version id: " + version.VersionId + ", last modified time: " + version.LastModified);
- }

- ListObjectsRequest req = new ListObjectsRequest();
- req.WithBucketName(BUCKET_NAME);
- req.WithPrefix(DIR_NAME);
- ListObjectsResponse res = client.ListObjects(req);
- Console.Out.WriteLine("Enumerating all objects in directory: " + DIR_NAME);
- foreach(S3Object obj in res.S3Objects) {
- Console.Out.WriteLine(" S3 object key: " + obj.Key);
- }

RG GPosted Jul 23, 2018, 5:29 AM
I want to download whole bucket data to local. pls help me thanks
sivakumar murugesanPosted Apr 11, 2018, 8:40 AM
I need folder delete using s3 in c#
naresh bahekarPosted Jul 12, 2011, 7:12 AM
during execution of your program i get an error that" Your account is not signed up for the S3 service. You must sign up before you can use S3".i already sign up for the s3 service of amazone and i have secret access key and acess key both.then what is the issue after execution of program.
Craig LebowitzPosted Mar 16, 2011, 4:03 PM
I like how you highlight the hierarchical abilities of S3. I recently signed up and have been thinking about how my current employer use Amazon services. We should be able to spin up ~10 new micro instances for automated load testing of a high-cpu instance. As a fairly typical asp.net dev, your level of explanation was perfect for me. Nice post!