3 Ruby `AWS::SDK`_ Examples (aws-sdk gem ~>2)
4 ===========================================
9 You can setup the connection on global way:
14 endpoint: 'https://objects.dreamhost.com.',
15 access_key_id: 'my-access-key',
16 secret_access_key: 'my-secret-key',
17 force_path_style: true,
22 and instantiate a client object:
26 s3_client = Aws::S3::Client.new
31 This gets a list of buckets that you own.
32 This also prints out the bucket name and creation date of each bucket.
36 s3_client.list_buckets.buckets.each do |bucket|
37 puts "#{bucket.name}\t#{bucket.creation_date}"
40 The output will look something like this::
42 mahbuckat1 2011-04-21T18:05:39.000Z
43 mahbuckat2 2011-04-21T18:05:48.000Z
44 mahbuckat3 2011-04-21T18:07:18.000Z
50 This creates a new bucket called ``my-new-bucket``
54 s3_client.create_bucket(bucket: 'my-new-bucket')
56 If you want a private bucket:
58 `acl` option accepts: # private, public-read, public-read-write, authenticated-read
62 s3_client.create_bucket(bucket: 'my-new-bucket', acl: 'private')
65 Listing a Bucket's Content
66 --------------------------
68 This gets a list of hashes with the contents of each object
69 This also prints out each object's name, the file size, and last
74 s3_client.get_objects(bucket: 'my-new-bucket').contents.each do |object|
75 puts "#{object.key}\t#{object.size}\t#{object.last-modified}"
78 The output will look something like this if the bucket has some files::
80 myphoto1.jpg 251262 2011-08-08T21:35:48.000Z
81 myphoto2.jpg 262518 2011-08-08T21:38:01.000Z
87 The Bucket must be empty! Otherwise it won't work!
91 s3_client.delete_bucket(bucket: 'my-new-bucket')
94 Forced Delete for Non-empty Buckets
95 -----------------------------------
96 First, you need to clear the bucket:
100 Aws::S3::Bucket.new('my-new-bucket', client: s3_client).clear!
102 after, you can destroy the bucket
106 s3_client.delete_bucket(bucket: 'my-new-bucket')
112 This creates a file ``hello.txt`` with the string ``"Hello World!"``
116 s3_client.put_object(
118 body: 'Hello World!',
119 bucket: 'my-new-bucket',
120 content_type: 'text/plain'
124 Change an Object's ACL
125 ----------------------
127 This makes the object ``hello.txt`` to be publicly readable, and ``secret_plans.txt``
132 s3_client.put_object_acl(bucket: 'my-new-bucket', key: 'hello.txt', acl: 'public-read')
134 s3_client.put_object_acl(bucket: 'my-new-bucket', key: 'private.txt', acl: 'private')
137 Download an Object (to a file)
138 ------------------------------
140 This downloads the object ``poetry.pdf`` and saves it in
141 ``/home/larry/documents/``
145 s3_client.get_object(bucket: 'my-new-bucket', key: 'poetry.pdf', response_target: '/home/larry/documents/poetry.pdf')
151 This deletes the object ``goodbye.txt``
155 s3_client.delete_object(key: 'goodbye.txt', bucket: 'my-new-bucket')
158 Generate Object Download URLs (signed and unsigned)
159 ---------------------------------------------------
161 This generates an unsigned download URL for ``hello.txt``. This works
162 because we made ``hello.txt`` public by setting the ACL above.
163 This then generates a signed download URL for ``secret_plans.txt`` that
164 will work for 1 hour. Signed download URLs will work for the time
165 period even if the object is private (when the time period is up, the
166 URL will stop working).
170 puts Aws::S3::Object.new(
172 bucket_name: 'my-new-bucket',
176 puts Aws::S3::Object.new(
177 key: 'secret_plans.txt',
178 bucket_name: 'hermes_ceph_gem',
180 ).presigned_url(:get, expires_in: 60 * 60)
182 The output of this will look something like::
184 http://objects.dreamhost.com/my-bucket-name/hello.txt
185 http://objects.dreamhost.com/my-bucket-name/secret_plans.txt?Signature=XXXXXXXXXXXXXXXXXXXXXXXXXXX&Expires=1316027075&AWSAccessKeyId=XXXXXXXXXXXXXXXXXXX
187 .. _`AWS::SDK`: http://docs.aws.amazon.com/sdkforruby/api/Aws/S3/Client.html
191 Ruby `AWS::S3`_ Examples (aws-s3 gem)
192 =====================================
194 Creating a Connection
195 ---------------------
197 This creates a connection so that you can interact with the server.
201 AWS::S3::Base.establish_connection!(
202 :server => 'objects.dreamhost.com',
204 :access_key_id => 'my-access-key',
205 :secret_access_key => 'my-secret-key'
209 Listing Owned Buckets
210 ---------------------
212 This gets a list of `AWS::S3::Bucket`_ objects that you own.
213 This also prints out the bucket name and creation date of each bucket.
217 AWS::S3::Service.buckets.each do |bucket|
218 puts "#{bucket.name}\t#{bucket.creation_date}"
221 The output will look something like this::
223 mahbuckat1 2011-04-21T18:05:39.000Z
224 mahbuckat2 2011-04-21T18:05:48.000Z
225 mahbuckat3 2011-04-21T18:07:18.000Z
231 This creates a new bucket called ``my-new-bucket``
235 AWS::S3::Bucket.create('my-new-bucket')
238 Listing a Bucket's Content
239 --------------------------
241 This gets a list of hashes with the contents of each object
242 This also prints out each object's name, the file size, and last
247 new_bucket = AWS::S3::Bucket.find('my-new-bucket')
248 new_bucket.each do |object|
249 puts "#{object.key}\t#{object.about['content-length']}\t#{object.about['last-modified']}"
252 The output will look something like this if the bucket has some files::
254 myphoto1.jpg 251262 2011-08-08T21:35:48.000Z
255 myphoto2.jpg 262518 2011-08-08T21:38:01.000Z
261 The Bucket must be empty! Otherwise it won't work!
265 AWS::S3::Bucket.delete('my-new-bucket')
268 Forced Delete for Non-empty Buckets
269 -----------------------------------
273 AWS::S3::Bucket.delete('my-new-bucket', :force => true)
279 This creates a file ``hello.txt`` with the string ``"Hello World!"``
283 AWS::S3::S3Object.store(
287 :content_type => 'text/plain'
291 Change an Object's ACL
292 ----------------------
294 This makes the object ``hello.txt`` to be publicly readable, and ``secret_plans.txt``
299 policy = AWS::S3::S3Object.acl('hello.txt', 'my-new-bucket')
300 policy.grants = [ AWS::S3::ACL::Grant.grant(:public_read) ]
301 AWS::S3::S3Object.acl('hello.txt', 'my-new-bucket', policy)
303 policy = AWS::S3::S3Object.acl('secret_plans.txt', 'my-new-bucket')
305 AWS::S3::S3Object.acl('secret_plans.txt', 'my-new-bucket', policy)
308 Download an Object (to a file)
309 ------------------------------
311 This downloads the object ``poetry.pdf`` and saves it in
312 ``/home/larry/documents/``
316 open('/home/larry/documents/poetry.pdf', 'w') do |file|
317 AWS::S3::S3Object.stream('poetry.pdf', 'my-new-bucket') do |chunk|
326 This deletes the object ``goodbye.txt``
330 AWS::S3::S3Object.delete('goodbye.txt', 'my-new-bucket')
333 Generate Object Download URLs (signed and unsigned)
334 ---------------------------------------------------
336 This generates an unsigned download URL for ``hello.txt``. This works
337 because we made ``hello.txt`` public by setting the ACL above.
338 This then generates a signed download URL for ``secret_plans.txt`` that
339 will work for 1 hour. Signed download URLs will work for the time
340 period even if the object is private (when the time period is up, the
341 URL will stop working).
345 puts AWS::S3::S3Object.url_for(
348 :authenticated => false
351 puts AWS::S3::S3Object.url_for(
354 :expires_in => 60 * 60
357 The output of this will look something like::
359 http://objects.dreamhost.com/my-bucket-name/hello.txt
360 http://objects.dreamhost.com/my-bucket-name/secret_plans.txt?Signature=XXXXXXXXXXXXXXXXXXXXXXXXXXX&Expires=1316027075&AWSAccessKeyId=XXXXXXXXXXXXXXXXXXX
362 .. _`AWS::S3`: http://amazon.rubyforge.org/
363 .. _`AWS::S3::Bucket`: http://amazon.rubyforge.org/doc/