summaryrefslogtreecommitdiffstats
path: root/src/ceph/doc/radosgw/s3/ruby.rst
blob: 435b3c63083da3ab122c97dbabe2df98f2219c4d (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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
.. _ruby:

Ruby `AWS::SDK`_ Examples (aws-sdk gem ~>2)
===========================================

Settings
---------------------

You can setup the connection on global way:

.. code-block:: ruby

	Aws.config.update(
		endpoint: 'https://objects.dreamhost.com.',
		access_key_id: 'my-access-key',
		secret_access_key: 'my-secret-key',
		force_path_style: true, 
		region: 'us-east-1'
	)


and instantiate a client object:

.. code-block:: ruby

    	s3_client = Aws::S3::Client.new

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:: ruby

	s3_client.list_buckets.buckets.each do |bucket|
		puts "#{bucket.name}\t#{bucket.creation_date}"
	end

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:: ruby

	s3_client.create_bucket(bucket: 'my-new-bucket')

If you want a private bucket: 

`acl` option accepts: # private, public-read, public-read-write, authenticated-read

.. code-block:: ruby

	s3_client.create_bucket(bucket: 'my-new-bucket', acl: 'private')


Listing a Bucket's Content
--------------------------

This gets a list of hashes with the contents of each object
This also prints out each object's name, the file size, and last
modified date.

.. code-block:: ruby

	s3_client.get_objects(bucket: 'my-new-bucket').contents.each do |object|
		puts "#{object.key}\t#{object.size}\t#{object.last-modified}"
	end

The output will look something like this if the bucket has some files::

   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:: ruby

	s3_client.delete_bucket(bucket: 'my-new-bucket')


Forced Delete for Non-empty Buckets
-----------------------------------
First, you need to clear the bucket:

.. code-block:: ruby

	Aws::S3::Bucket.new('my-new-bucket', client: s3_client).clear!
	
after, you can destroy the bucket

.. code-block:: ruby

	s3_client.delete_bucket(bucket: 'my-new-bucket')


Creating an Object
------------------

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

.. code-block:: ruby

	s3_client.put_object(
		key: 'hello.txt',
		body: 'Hello World!',
		bucket: 'my-new-bucket',
		content_type: 'text/plain'
	)


Change an Object's ACL
----------------------

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

.. code-block:: ruby

	s3_client.put_object_acl(bucket: 'my-new-bucket', key: 'hello.txt', acl: 'public-read')

	s3_client.put_object_acl(bucket: 'my-new-bucket', key: 'private.txt', acl: 'private')


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

This downloads the object ``poetry.pdf`` and saves it in
``/home/larry/documents/``

.. code-block:: ruby

	s3_client.get_object(bucket: 'my-new-bucket', key: 'poetry.pdf', response_target: '/home/larry/documents/poetry.pdf')


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

This deletes the object ``goodbye.txt``

.. code-block:: ruby

	s3_client.delete_object(key: 'goodbye.txt', bucket: 'my-new-bucket')


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).

.. code-block:: ruby

	puts Aws::S3::Object.new(
		key: 'hello.txt',
		bucket_name: 'my-new-bucket',
		client: s3_client
	).public_url

	puts Aws::S3::Object.new(
		key: 'secret_plans.txt',
		bucket_name: 'hermes_ceph_gem',
		client: s3_client
	).presigned_url(:get, expires_in: 60 * 60)

The output of this will look something like::

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

.. _`AWS::SDK`: http://docs.aws.amazon.com/sdkforruby/api/Aws/S3/Client.html



Ruby `AWS::S3`_ Examples (aws-s3 gem)
=====================================

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

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

.. code-block:: ruby

	AWS::S3::Base.establish_connection!(
		:server            => 'objects.dreamhost.com',
		:use_ssl           => true,
		:access_key_id     => 'my-access-key',
		:secret_access_key => 'my-secret-key'
	)


Listing Owned Buckets
---------------------

This gets a list of `AWS::S3::Bucket`_ objects that you own.
This also prints out the bucket name and creation date of each bucket.

.. code-block:: ruby

	AWS::S3::Service.buckets.each do |bucket|
		puts "#{bucket.name}\t#{bucket.creation_date}"
	end

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:: ruby

	AWS::S3::Bucket.create('my-new-bucket')


Listing a Bucket's Content
--------------------------

This gets a list of hashes with the contents of each object
This also prints out each object's name, the file size, and last
modified date.

.. code-block:: ruby

	new_bucket = AWS::S3::Bucket.find('my-new-bucket')
	new_bucket.each do |object|
		puts "#{object.key}\t#{object.about['content-length']}\t#{object.about['last-modified']}"
	end

The output will look something like this if the bucket has some files::

   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:: ruby

	AWS::S3::Bucket.delete('my-new-bucket')


Forced Delete for Non-empty Buckets
-----------------------------------

.. code-block:: ruby

	AWS::S3::Bucket.delete('my-new-bucket', :force => true)


Creating an Object
------------------

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

.. code-block:: ruby

	AWS::S3::S3Object.store(
		'hello.txt',
		'Hello World!',
		'my-new-bucket',
		:content_type => 'text/plain'
	)


Change an Object's ACL
----------------------

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

.. code-block:: ruby

	policy = AWS::S3::S3Object.acl('hello.txt', 'my-new-bucket')
	policy.grants = [ AWS::S3::ACL::Grant.grant(:public_read) ]
	AWS::S3::S3Object.acl('hello.txt', 'my-new-bucket', policy)

	policy = AWS::S3::S3Object.acl('secret_plans.txt', 'my-new-bucket')
	policy.grants = []
	AWS::S3::S3Object.acl('secret_plans.txt', 'my-new-bucket', policy)


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

This downloads the object ``poetry.pdf`` and saves it in
``/home/larry/documents/``

.. code-block:: ruby

	open('/home/larry/documents/poetry.pdf', 'w') do |file|
		AWS::S3::S3Object.stream('poetry.pdf', 'my-new-bucket') do |chunk|
			file.write(chunk)
		end
	end


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

This deletes the object ``goodbye.txt``

.. code-block:: ruby

	AWS::S3::S3Object.delete('goodbye.txt', 'my-new-bucket')


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).

.. code-block:: ruby

	puts AWS::S3::S3Object.url_for(
		'hello.txt',
		'my-new-bucket',
		:authenticated => false
	)

	puts AWS::S3::S3Object.url_for(
		'secret_plans.txt',
		'my-new-bucket',
		:expires_in => 60 * 60
	)

The output of this will look something like::

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

.. _`AWS::S3`: http://amazon.rubyforge.org/
.. _`AWS::S3::Bucket`: http://amazon.rubyforge.org/doc/