summaryrefslogtreecommitdiffstats
path: root/src/ceph/doc/radosgw/s3/csharp.rst
blob: af1c6e4b57c30bee2f822462f0c478c1149ef317 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
.. _csharp:

C# S3 Examples
==============

Creating a Connection
---------------------

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

.. code-block:: csharp

	using System;
	using Amazon;
	using Amazon.S3;
	using Amazon.S3.Model;

	string accessKey = "put your access key here!";
	string secretKey = "put your secret key here!";

	AmazonS3Config config = new AmazonS3Config();
	config.ServiceURL = "objects.dreamhost.com";

	AmazonS3Client s3Client = new AmazonS3Client(
		accessKey,
		secretKey,
		config
		);


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.

.. code-block:: csharp

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

The output will look something like this::

   mahbuckat1	2011-04-21T18:05:39.000Z
   mahbuckat2	2011-04-21T18:05:48.000Z
   mahbuckat3	2011-04-21T18:07:18.000Z


Creating a Bucket
-----------------
This creates a new bucket called ``my-new-bucket``

.. code-block:: csharp

	PutBucketRequest request = new PutBucketRequest();
	request.BucketName = "my-new-bucket";
	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.

.. code-block:: csharp

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

The output will look something like this::

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


Deleting a Bucket
-----------------

.. note::

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

.. code-block:: csharp

	DeleteBucketRequest request = new DeleteBucketRequest();
	request.BucketName = "my-new-bucket";
	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!"``

.. code-block:: csharp

	PutObjectRequest request = new PutObjectRequest();
	request.BucketName  = "my-new-bucket";
	request.Key         = "hello.txt";
	request.ContentType = "text/plain";
	request.ContentBody = "Hello World!";
	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.

.. code-block:: csharp

	PutACLRequest request = new PutACLRequest();
	request.BucketName = "my-new-bucket";
	request.Key        = "hello.txt";
	request.CannedACL  = S3CannedACL.PublicRead;
	client.PutACL(request);

	PutACLRequest request2 = new PutACLRequest();
	request2.BucketName = "my-new-bucket";
	request2.Key        = "secret_plans.txt";
	request2.CannedACL  = S3CannedACL.Private;
	client.PutACL(request2);


Download an Object (to a file)
------------------------------

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

.. code-block:: csharp

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


Delete an Object
----------------

This deletes the object ``goodbye.txt``

.. code-block:: csharp

	DeleteObjectRequest request = new DeleteObjectRequest();
	request.BucketName = "my-new-bucket";
	request.Key        = "goodbye.txt";
	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.

.. code-block:: csharp

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

The output of this will look something like::

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