~ubuntu-branches/ubuntu/quantal/ceph/quantal

« back to all changes in this revision

Viewing changes to doc/api/s3/csharp.rst

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2012-07-16 09:56:24 UTC
  • mfrom: (0.3.11)
  • mto: This revision was merged to the branch mainline in revision 17.
  • Revision ID: package-import@ubuntu.com-20120716095624-azr2w4hbhei1rxmx
Tags: upstream-0.48
ImportĀ upstreamĀ versionĀ 0.48

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
.. _csharp:
 
2
 
 
3
C# S3 Examples
 
4
==============
 
5
 
 
6
Creating a Connection
 
7
---------------------
 
8
 
 
9
This creates a connection so that you can interact with the server.
 
10
 
 
11
.. code-block:: csharp
 
12
 
 
13
        using System;
 
14
        using Amazon;
 
15
        using Amazon.S3;
 
16
        using Amazon.S3.Model;
 
17
 
 
18
        string accessKey = "put your access key here!";
 
19
        string secretKey = "put your secret key here!";
 
20
 
 
21
        AmazonS3Config config = new AmazonS3Config();
 
22
        config.ServiceURL = "objects.dreamhost.com";
 
23
 
 
24
        AmazonS3 client = Amazon.AWSClientFactory.CreateAmazonS3Client(
 
25
                accessKey,
 
26
                secretKey,
 
27
                config
 
28
                );
 
29
 
 
30
 
 
31
Listing Owned Buckets
 
32
---------------------
 
33
 
 
34
This gets a list of Buckets that you own.
 
35
This also prints out the bucket name and creation date of each bucket.
 
36
 
 
37
.. code-block:: csharp
 
38
 
 
39
        ListBucketResponse response = client.ListBuckets();
 
40
        foreach (S3Bucket b in response.Buckets)
 
41
        {
 
42
                Console.WriteLine("{0}\t{1}", b.BucketName, b.CreationDate);
 
43
        }
 
44
 
 
45
The output will look something like this::
 
46
 
 
47
   mahbuckat1   2011-04-21T18:05:39.000Z
 
48
   mahbuckat2   2011-04-21T18:05:48.000Z
 
49
   mahbuckat3   2011-04-21T18:07:18.000Z
 
50
 
 
51
 
 
52
Creating a Bucket
 
53
-----------------
 
54
This creates a new bucket called ``my-new-bucket``
 
55
 
 
56
.. code-block:: csharp
 
57
 
 
58
        PutBucketRequest request = new PutBucketRequest();
 
59
        request.BucketName = "my-new-bucket";
 
60
        client.PutBucket(request);
 
61
 
 
62
Listing a Bucket's Content
 
63
--------------------------
 
64
 
 
65
This gets a list of objects in the bucket.
 
66
This also prints out each object's name, the file size, and last
 
67
modified date.
 
68
 
 
69
.. code-block:: csharp
 
70
 
 
71
        ListObjectsRequest request = new ListObjectsRequest();
 
72
        request.BucketName = "my-new-bucket";
 
73
        ListObjectsResponse response = client.ListObjects(request);
 
74
        foreach (S3Object o in response.S3Objects)
 
75
        {
 
76
                Console.WriteLine("{0}\t{1}\t{2}", o.Key, o.Size, o.LastModified);
 
77
        }
 
78
 
 
79
The output will look something like this::
 
80
 
 
81
   myphoto1.jpg 251262  2011-08-08T21:35:48.000Z
 
82
   myphoto2.jpg 262518  2011-08-08T21:38:01.000Z
 
83
 
 
84
 
 
85
Deleting a Bucket
 
86
-----------------
 
87
 
 
88
.. note::
 
89
 
 
90
   The Bucket must be empty! Otherwise it won't work!
 
91
 
 
92
.. code-block:: csharp
 
93
 
 
94
        DeleteBucketRequest request = new DeleteBucketRequest();
 
95
        request.BucketName = "my-new-bucket";
 
96
        client.DeleteBucket(request);
 
97
 
 
98
 
 
99
Forced Delete for Non-empty Buckets
 
100
-----------------------------------
 
101
 
 
102
.. attention::
 
103
 
 
104
   not available
 
105
 
 
106
 
 
107
Creating an Object
 
108
------------------
 
109
 
 
110
This creates a file ``hello.txt`` with the string ``"Hello World!"``
 
111
 
 
112
.. code-block:: csharp
 
113
 
 
114
        PutObjectRequest request = new PutObjectRequest();
 
115
        request.Bucket      = "my-new-bucket";
 
116
        request.Key         = "hello.txt";
 
117
        request.ContentType = "text/plain";
 
118
        request.ContentBody = "Hello World!";
 
119
        client.PutObject(request);
 
120
 
 
121
 
 
122
Change an Object's ACL
 
123
----------------------
 
124
 
 
125
This makes the object ``hello.txt`` to be publicly readable, and
 
126
``secret_plans.txt`` to be private.
 
127
 
 
128
.. code-block:: csharp
 
129
 
 
130
        SetACLRequest request = new SetACLRequest();
 
131
        request.BucketName = "my-new-bucket";
 
132
        request.Key        = "hello.txt";
 
133
        request.CannedACL  = S3CannedACL.PublicRead;
 
134
        client.SetACL(request);
 
135
 
 
136
        SetACLRequest request2 = new SetACLRequest();
 
137
        request2.BucketName = "my-new-bucket";
 
138
        request2.Key        = "secret_plans.txt";
 
139
        request2.CannedACL  = S3CannedACL.Private;
 
140
        client.SetACL(request2);
 
141
 
 
142
 
 
143
Download an Object (to a file)
 
144
------------------------------
 
145
 
 
146
This downloads the object ``perl_poetry.pdf`` and saves it in
 
147
``C:\Users\larry\Documents``
 
148
 
 
149
.. code-block:: csharp
 
150
 
 
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");
 
156
 
 
157
 
 
158
Delete an Object
 
159
----------------
 
160
 
 
161
This deletes the object ``goodbye.txt``
 
162
 
 
163
.. code-block:: csharp
 
164
 
 
165
        DeleteObjectRequest request = new DeleteObjectRequest();
 
166
        request.BucketName = "my-new-bucket";
 
167
        request.Key        = "goodbye.txt";
 
168
        client.DeleteObject(request);
 
169
 
 
170
 
 
171
Generate Object Download URLs (signed and unsigned)
 
172
---------------------------------------------------
 
173
 
 
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).
 
180
 
 
181
.. note::
 
182
 
 
183
   The C# S3 Library does not have a method for generating unsigned
 
184
   URLs, so the following example only shows generating signed URLs.
 
185
 
 
186
.. code-block:: csharp
 
187
 
 
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);
 
195
 
 
196
The output of this will look something like::
 
197
 
 
198
   http://objects.dreamhost.com/my-bucket-name/secret_plans.txt?Signature=XXXXXXXXXXXXXXXXXXXXXXXXXXX&Expires=1316027075&AWSAccessKeyId=XXXXXXXXXXXXXXXXXXX
 
199