9 This creates a connection so that you can interact with the server.
11 .. code-block:: csharp
16 using Amazon.S3.Model;
18 string accessKey = "put your access key here!";
19 string secretKey = "put your secret key here!";
21 AmazonS3Config config = new AmazonS3Config();
22 config.ServiceURL = "objects.dreamhost.com";
24 AmazonS3Client s3Client = new AmazonS3Client(
34 This gets a list of Buckets that you own.
35 This also prints out the bucket name and creation date of each bucket.
37 .. code-block:: csharp
39 ListBucketsResponse response = client.ListBuckets();
40 foreach (S3Bucket b in response.Buckets)
42 Console.WriteLine("{0}\t{1}", b.BucketName, b.CreationDate);
45 The output will look something like this::
47 mahbuckat1 2011-04-21T18:05:39.000Z
48 mahbuckat2 2011-04-21T18:05:48.000Z
49 mahbuckat3 2011-04-21T18:07:18.000Z
54 This creates a new bucket called ``my-new-bucket``
56 .. code-block:: csharp
58 PutBucketRequest request = new PutBucketRequest();
59 request.BucketName = "my-new-bucket";
60 client.PutBucket(request);
62 Listing a Bucket's Content
63 --------------------------
65 This gets a list of objects in the bucket.
66 This also prints out each object's name, the file size, and last
69 .. code-block:: csharp
71 ListObjectsRequest request = new ListObjectsRequest();
72 request.BucketName = "my-new-bucket";
73 ListObjectsResponse response = client.ListObjects(request);
74 foreach (S3Object o in response.S3Objects)
76 Console.WriteLine("{0}\t{1}\t{2}", o.Key, o.Size, o.LastModified);
79 The output will look something like this::
81 myphoto1.jpg 251262 2011-08-08T21:35:48.000Z
82 myphoto2.jpg 262518 2011-08-08T21:38:01.000Z
90 The Bucket must be empty! Otherwise it won't work!
92 .. code-block:: csharp
94 DeleteBucketRequest request = new DeleteBucketRequest();
95 request.BucketName = "my-new-bucket";
96 client.DeleteBucket(request);
99 Forced Delete for Non-empty Buckets
100 -----------------------------------
110 This creates a file ``hello.txt`` with the string ``"Hello World!"``
112 .. code-block:: csharp
114 PutObjectRequest request = new PutObjectRequest();
115 request.BucketName = "my-new-bucket";
116 request.Key = "hello.txt";
117 request.ContentType = "text/plain";
118 request.ContentBody = "Hello World!";
119 client.PutObject(request);
122 Change an Object's ACL
123 ----------------------
125 This makes the object ``hello.txt`` to be publicly readable, and
126 ``secret_plans.txt`` to be private.
128 .. code-block:: csharp
130 PutACLRequest request = new PutACLRequest();
131 request.BucketName = "my-new-bucket";
132 request.Key = "hello.txt";
133 request.CannedACL = S3CannedACL.PublicRead;
134 client.PutACL(request);
136 PutACLRequest request2 = new PutACLRequest();
137 request2.BucketName = "my-new-bucket";
138 request2.Key = "secret_plans.txt";
139 request2.CannedACL = S3CannedACL.Private;
140 client.PutACL(request2);
143 Download an Object (to a file)
144 ------------------------------
146 This downloads the object ``perl_poetry.pdf`` and saves it in
147 ``C:\Users\larry\Documents``
149 .. code-block:: csharp
151 GetObjectRequest request = new GetObjectRequest();
152 request.BucketName = "my-new-bucket";
153 request.Key = "perl_poetry.pdf";
154 GetObjectResponse response = client.GetObject(request);
155 response.WriteResponseStreamToFile("C:\\Users\\larry\\Documents\\perl_poetry.pdf");
161 This deletes the object ``goodbye.txt``
163 .. code-block:: csharp
165 DeleteObjectRequest request = new DeleteObjectRequest();
166 request.BucketName = "my-new-bucket";
167 request.Key = "goodbye.txt";
168 client.DeleteObject(request);
171 Generate Object Download URLs (signed and unsigned)
172 ---------------------------------------------------
174 This generates an unsigned download URL for ``hello.txt``. This works
175 because we made ``hello.txt`` public by setting the ACL above.
176 This then generates a signed download URL for ``secret_plans.txt`` that
177 will work for 1 hour. Signed download URLs will work for the time
178 period even if the object is private (when the time period is up, the
179 URL will stop working).
183 The C# S3 Library does not have a method for generating unsigned
184 URLs, so the following example only shows generating signed URLs.
186 .. code-block:: csharp
188 GetPreSignedUrlRequest request = new GetPreSignedUrlRequest();
189 request.BucketName = "my-bucket-name";
190 request.Key = "secret_plans.txt";
191 request.Expires = DateTime.Now.AddHours(1);
192 request.Protocol = Protocol.HTTP;
193 string url = client.GetPreSignedURL(request);
194 Console.WriteLine(url);
196 The output of this will look something like::
198 http://objects.dreamhost.com/my-bucket-name/secret_plans.txt?Signature=XXXXXXXXXXXXXXXXXXXXXXXXXXX&Expires=1316027075&AWSAccessKeyId=XXXXXXXXXXXXXXXXXXX