alancse-aws-s3 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
data/COPYING ADDED
@@ -0,0 +1,19 @@
1
+ #
2
+ # Copyright (c) 2006-2007 Marcel Molina Jr. <marcel@vernix.org>
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining a copy of
5
+ # this software and associated documentation files (the "Software"), to deal in the
6
+ # Software without restriction, including without limitation the rights to use,
7
+ # copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
8
+ # Software, and to permit persons to whom the Software is furnished to do so,
9
+ # subject to the following conditions:
10
+ #
11
+ # The above copyright notice and this permission notice shall be included in all
12
+ # copies or substantial portions of the Software.
13
+ #
14
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
16
+ # FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
17
+ # COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18
+ # AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
19
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/INSTALL ADDED
@@ -0,0 +1,55 @@
1
+ == Rubygems
2
+
3
+ The easiest way to install aws/s3 is with Rubygems:
4
+
5
+ % sudo gem i aws-s3 -ry
6
+
7
+ == Directly from svn
8
+
9
+ % svn co svn://rubyforge.org/var/svn/amazon/s3/trunk aws
10
+
11
+ == As a Rails plugin
12
+
13
+ If you want to use aws/s3 with a Rails application, you can export the repository
14
+ into your plugins directory and then check it in:
15
+
16
+ % cd my-rails-application/vendor/plugins
17
+ % svn export svn://rubyforge.org/var/svn/amazon/s3/trunk aws
18
+ % svn add aws
19
+
20
+ Or you could pull it down with an svn:externals:
21
+
22
+ % cd my-rails-application/vendor/plugins
23
+ % svn propedit svn:externals .
24
+
25
+ Then add the following line, save and exit:
26
+
27
+ aws svn://rubyforge.org/var/svn/amazon/s3/trunk
28
+
29
+ If you go the svn route, be sure that you have all the dependencies installed. The list of dependencies follow.
30
+
31
+ == Dependencies
32
+
33
+ AWS::S3 requires Ruby 1.8.4 or greater.
34
+
35
+ It also has the following dependencies:
36
+
37
+ sudo gem i xml-simple -ry
38
+ sudo gem i builder -ry
39
+ sudo gem i mime-types -ry
40
+
41
+ === XML parsing (xml-simple)
42
+
43
+ AWS::S3 depends on XmlSimple (http://xml-simple.rubyforge.org/). When installing aws/s3 with
44
+ Rubygems, this dependency will be taken care of for you. Otherwise, installation instructions are listed on the xml-simple
45
+ site.
46
+
47
+ If your system has the Ruby libxml bindings installed (http://libxml.rubyforge.org/) they will be used instead of REXML (which is what XmlSimple uses). For those concerned with speed and efficiency, it would behoove you to install libxml (instructions here: http://libxml.rubyforge.org/install.html) as it is considerably faster and less expensive than REXML.
48
+
49
+ === XML generation (builder)
50
+
51
+ AWS::S3 also depends on the Builder library (http://builder.rubyforge.org/ and http://rubyforge.org/projects/builder/). This will also automatically be installed for you when using Rubygems.
52
+
53
+ === Content type inference (mime-types)
54
+
55
+ AWS::S3 depends on the MIME::Types library (http://mime-types.rubyforge.org/) to infer the content type of an object that does not explicitly specify it. This library will automatically be installed for you when using Rubygems.
data/README ADDED
@@ -0,0 +1,551 @@
1
+ This Fork is to provide support to European Buckets of Amazon S3, which changes concepts like the "Host" header (from s3.amazonaws.com to bucket_name.amazonaws.com)
2
+ With this fork, you can connect to/create US Buckets as well as EU Buckets.
3
+
4
+ = AWS::S3
5
+
6
+ AWS::S3 is a Ruby library for Amazon's Simple Storage Service's REST API (http://aws.amazon.com/s3).
7
+ Full documentation of the currently supported API can be found at http://docs.amazonwebservices.com/AmazonS3/2006-03-01.
8
+
9
+ == Getting started
10
+
11
+ To get started you need to require 'aws/s3':
12
+
13
+ % irb -rubygems
14
+ irb(main):001:0> require 'aws/s3'
15
+ # => true
16
+
17
+ The AWS::S3 library ships with an interactive shell called <tt>s3sh</tt>. From within it, you have access to all the operations the library exposes from the command line.
18
+
19
+ % s3sh
20
+ >> Version
21
+
22
+ Before you can do anything, you must establish a connection using Base.establish_connection!. A basic connection would look something like this:
23
+
24
+ AWS::S3::Base.establish_connection!(
25
+ :access_key_id => 'abc',
26
+ :secret_access_key => '123'
27
+ )
28
+
29
+ The minimum connection options that you must specify are your access key id and your secret access key.
30
+
31
+ (If you don't already have your access keys, all you need to sign up for the S3 service is an account at Amazon. You can sign up for S3 and get access keys by visiting http://aws.amazon.com/s3.)
32
+
33
+ For convenience, if you set two special environment variables with the value of your access keys, the console will automatically create a default connection for you. For example:
34
+
35
+ % cat .amazon_keys
36
+ export AMAZON_ACCESS_KEY_ID='abcdefghijklmnop'
37
+ export AMAZON_SECRET_ACCESS_KEY='1234567891012345'
38
+
39
+ Then load it in your shell's rc file.
40
+
41
+ % cat .zshrc
42
+ if [[ -f "$HOME/.amazon_keys" ]]; then
43
+ source "$HOME/.amazon_keys";
44
+ fi
45
+
46
+ See more connection details at AWS::S3::Connection::Management::ClassMethods.
47
+
48
+
49
+ == AWS::S3 Basics
50
+ === The service, buckets and objects
51
+
52
+ The three main concepts of S3 are the service, buckets and objects.
53
+
54
+ ==== The service
55
+
56
+ The service lets you find out general information about your account, like what buckets you have.
57
+
58
+ Service.buckets
59
+ # => []
60
+
61
+
62
+ ==== Buckets
63
+
64
+ Buckets are containers for objects (the files you store on S3). To create a new bucket you just specify its name.
65
+
66
+ # Pick a unique name, or else you'll get an error
67
+ # if the name is already taken.
68
+ Bucket.create('jukebox')
69
+
70
+ # To create an European bucket.
71
+ Bucket.create('jukebox', :location => :eu)
72
+
73
+ Bucket names must be unique across the entire S3 system, sort of like domain names across the internet. If you try
74
+ to create a bucket with a name that is already taken, you will get an error.
75
+
76
+ Assuming the name you chose isn't already taken, your new bucket will now appear in the bucket list:
77
+
78
+ Service.buckets
79
+ # => [#<AWS::S3::Bucket @attributes={"name"=>"jukebox"}>]
80
+
81
+ Once you have succesfully created a bucket you can you can fetch it by name using Bucket.find.
82
+
83
+ music_bucket = Bucket.find('jukebox')
84
+
85
+ The bucket that is returned will contain a listing of all the objects in the bucket.
86
+
87
+ music_bucket.objects.size
88
+ # => 0
89
+
90
+ If all you are interested in is the objects of the bucket, you can get to them directly using Bucket.objects.
91
+
92
+ Bucket.objects('jukebox').size
93
+ # => 0
94
+
95
+ By default all objects will be returned, though there are several options you can use to limit what is returned, such as
96
+ specifying that only objects whose name is after a certain place in the alphabet be returned, and etc. Details about these options can
97
+ be found in the documentation for Bucket.find.
98
+
99
+ To add an object to a bucket you specify the name of the object, its value, and the bucket to put it in.
100
+
101
+ file = 'black-flowers.mp3'
102
+ S3Object.store(file, open(file), 'jukebox')
103
+
104
+ You'll see your file has been added to it:
105
+
106
+ music_bucket.objects
107
+ # => [#<AWS::S3::S3Object '/jukebox/black-flowers.mp3'>]
108
+
109
+ You can treat your bucket like a hash and access objects by name:
110
+
111
+ jukebox['black-flowers.mp3']
112
+ # => #<AWS::S3::S3Object '/jukebox/black-flowers.mp3'>
113
+
114
+ In the event that you want to delete a bucket, you can use Bucket.delete.
115
+
116
+ Bucket.delete('jukebox')
117
+
118
+ Keep in mind, like unix directories, you can not delete a bucket unless it is empty. Trying to delete a bucket
119
+ that contains objects will raise a BucketNotEmpty exception.
120
+
121
+ Passing the :force => true option to delete will take care of deleting all the bucket's objects for you.
122
+
123
+ Bucket.delete('photos', :force => true)
124
+ # => true
125
+
126
+
127
+ ==== Objects
128
+
129
+ S3Objects represent the data you store on S3. They have a key (their name) and a value (their data). All objects belong to a
130
+ bucket.
131
+
132
+ You can store an object on S3 by specifying a key, its data and the name of the bucket you want to put it in:
133
+
134
+ S3Object.store('me.jpg', open('headshot.jpg'), 'photos')
135
+
136
+ The content type of the object will be inferred by its extension. If the appropriate content type can not be inferred, S3 defaults
137
+ to <tt>binary/octect-stream</tt>.
138
+
139
+ If you want to override this, you can explicitly indicate what content type the object should have with the <tt>:content_type</tt> option:
140
+
141
+ file = 'black-flowers.m4a'
142
+ S3Object.store(
143
+ file,
144
+ open(file),
145
+ 'jukebox',
146
+ :content_type => 'audio/mp4a-latm'
147
+ )
148
+
149
+ You can read more about storing files on S3 in the documentation for S3Object.store.
150
+
151
+ If you just want to fetch an object you've stored on S3, you just specify its name and its bucket:
152
+
153
+ picture = S3Object.find 'headshot.jpg', 'photos'
154
+
155
+ N.B. The actual data for the file is not downloaded in both the example where the file appeared in the bucket and when fetched directly.
156
+ You get the data for the file like this:
157
+
158
+ picture.value
159
+
160
+ You can fetch just the object's data directly:
161
+
162
+ S3Object.value 'headshot.jpg', 'photos'
163
+
164
+ Or stream it by passing a block to <tt>stream</tt>:
165
+
166
+ open('song.mp3', 'w') do |file|
167
+ S3Object.stream('song.mp3', 'jukebox') do |chunk|
168
+ file.write chunk
169
+ end
170
+ end
171
+
172
+ The data of the file, once download, is cached, so subsequent calls to <tt>value</tt> won't redownload the file unless you
173
+ tell the object to reload its <tt>value</tt>:
174
+
175
+ # Redownloads the file's data
176
+ song.value(:reload)
177
+
178
+ Other functionality includes:
179
+
180
+ # Check if an object exists?
181
+ S3Object.exists? 'headshot.jpg', 'photos'
182
+
183
+ # Copying an object
184
+ S3Object.copy 'headshot.jpg', 'headshot2.jpg', 'photos'
185
+
186
+ # Renaming an object
187
+ S3Object.rename 'headshot.jpg', 'portrait.jpg', 'photos'
188
+
189
+ # Deleting an object
190
+ S3Object.delete 'headshot.jpg', 'photos'
191
+
192
+ ==== More about objects and their metadata
193
+
194
+ You can find out the content type of your object with the <tt>content_type</tt> method:
195
+
196
+ song.content_type
197
+ # => "audio/mpeg"
198
+
199
+ You can change the content type as well if you like:
200
+
201
+ song.content_type = 'application/pdf'
202
+ song.store
203
+
204
+ (Keep in mind that due to limitiations in S3's exposed API, the only way to change things like the content_type
205
+ is to PUT the object onto S3 again. In the case of large files, this will result in fully re-uploading the file.)
206
+
207
+ A bevie of information about an object can be had using the <tt>about</tt> method:
208
+
209
+ pp song.about
210
+ {"last-modified" => "Sat, 28 Oct 2006 21:29:26 GMT",
211
+ "content-type" => "binary/octect-stream",
212
+ "etag" => "\"dc629038ffc674bee6f62eb64ff3a\"",
213
+ "date" => "Sat, 28 Oct 2006 21:30:41 GMT",
214
+ "x-amz-request-id" => "B7BC68F55495B1C8",
215
+ "server" => "AmazonS3",
216
+ "content-length" => "3418766"}
217
+
218
+ You can get and set metadata for an object:
219
+
220
+ song.metadata
221
+ # => {}
222
+ song.metadata[:album] = "A River Ain't Too Much To Love"
223
+ # => "A River Ain't Too Much To Love"
224
+ song.metadata[:released] = 2005
225
+ pp song.metadata
226
+ {"x-amz-meta-released" => 2005,
227
+ "x-amz-meta-album" => "A River Ain't Too Much To Love"}
228
+ song.store
229
+
230
+ That metadata will be saved in S3 and is hence forth available from that object:
231
+
232
+ song = S3Object.find('black-flowers.mp3', 'jukebox')
233
+ pp song.metadata
234
+ {"x-amz-meta-released" => "2005",
235
+ "x-amz-meta-album" => "A River Ain't Too Much To Love"}
236
+ song.metada[:released]
237
+ # => "2005"
238
+ song.metada[:released] = 2006
239
+ pp song.metada
240
+ {"x-amz-meta-released" => 2006,
241
+ "x-amz-meta-album" => "A River Ain't Too Much To Love"}
242
+
243
+
244
+ ==== Streaming uploads
245
+
246
+ When storing an object on the S3 servers using S3Object.store, the <tt>data</tt> argument can be a string or an I/O stream.
247
+ If <tt>data</tt> is an I/O stream it will be read in segments and written to the socket incrementally. This approach
248
+ may be desirable for very large files so they are not read into memory all at once.
249
+
250
+ # Non streamed upload
251
+ S3Object.store('greeting.txt', 'hello world!', 'marcel')
252
+
253
+ # Streamed upload
254
+ S3Object.store('roots.mpeg', open('roots.mpeg'), 'marcel')
255
+
256
+
257
+ == Setting the current bucket
258
+ ==== Scoping operations to a specific bucket
259
+
260
+ If you plan on always using a specific bucket for certain files, you can skip always having to specify the bucket by creating
261
+ a subclass of Bucket or S3Object and telling it what bucket to use:
262
+
263
+ class JukeBoxSong < AWS::S3::S3Object
264
+ set_current_bucket_to 'jukebox'
265
+ end
266
+
267
+ For all methods that take a bucket name as an argument, the current bucket will be used if the bucket name argument is omitted.
268
+
269
+ other_song = 'baby-please-come-home.mp3'
270
+ JukeBoxSong.store(other_song, open(other_song))
271
+
272
+ This time we didn't have to explicitly pass in the bucket name, as the JukeBoxSong class knows that it will
273
+ always use the 'jukebox' bucket.
274
+
275
+ "Astute readers", as they say, may have noticed that we used the third parameter to pass in the content type,
276
+ rather than the fourth parameter as we had the last time we created an object. If the bucket can be inferred, or
277
+ is explicitly set, as we've done in the JukeBoxSong class, then the third argument can be used to pass in
278
+ options.
279
+
280
+ Now all operations that would have required a bucket name no longer do.
281
+
282
+ other_song = JukeBoxSong.find('baby-please-come-home.mp3')
283
+
284
+
285
+ == BitTorrent
286
+ ==== Another way to download large files
287
+
288
+ Objects on S3 can be distributed via the BitTorrent file sharing protocol.
289
+
290
+ You can get a torrent file for an object by calling <tt>torrent_for</tt>:
291
+
292
+ S3Object.torrent_for 'kiss.jpg', 'marcel'
293
+
294
+ Or just call the <tt>torrent</tt> method if you already have the object:
295
+
296
+ song = S3Object.find 'kiss.jpg', 'marcel'
297
+ song.torrent
298
+
299
+ Calling <tt>grant_torrent_access_to</tt> on a object will allow anyone to anonymously
300
+ fetch the torrent file for that object:
301
+
302
+ S3Object.grant_torrent_access_to 'kiss.jpg', 'marcel'
303
+
304
+ Anonymous requests to
305
+
306
+ http://s3.amazonaws.com/marcel/kiss.jpg?torrent
307
+
308
+ will serve up the torrent file for that object.
309
+
310
+
311
+ == Access control
312
+ ==== Using canned access control policies
313
+
314
+ By default buckets are private. This means that only the owner has access rights to the bucket and its objects.
315
+ Objects in that bucket inherit the permission of the bucket unless otherwise specified. When an object is private, the owner can
316
+ generate a signed url that exposes the object to anyone who has that url. Alternatively, buckets and objects can be given other
317
+ access levels. Several canned access levels are defined:
318
+
319
+ * <tt>:private</tt> - Owner gets FULL_CONTROL. No one else has any access rights. This is the default.
320
+ * <tt>:public_read</tt> - Owner gets FULL_CONTROL and the anonymous principal is granted READ access. If this policy is used on an object, it can be read from a browser with no authentication.
321
+ * <tt>:public_read_write</tt> - Owner gets FULL_CONTROL, the anonymous principal is granted READ and WRITE access. This is a useful policy to apply to a bucket, if you intend for any anonymous user to PUT objects into the bucket.
322
+ * <tt>:authenticated_read</tt> - Owner gets FULL_CONTROL, and any principal authenticated as a registered Amazon S3 user is granted READ access.
323
+
324
+ You can set a canned access level when you create a bucket or an object by using the <tt>:access</tt> option:
325
+
326
+ S3Object.store(
327
+ 'kiss.jpg',
328
+ data,
329
+ 'marcel',
330
+ :access => :public_read
331
+ )
332
+
333
+ Since the image we created is publicly readable, we can access it directly from a browser by going to the corresponding bucket name
334
+ and specifying the object's key without a special authenticated url:
335
+
336
+ http://s3.amazonaws.com/marcel/kiss.jpg
337
+
338
+ ==== Building custum access policies
339
+
340
+ For both buckets and objects, you can use the <tt>acl</tt> method to see its access control policy:
341
+
342
+ policy = S3Object.acl('kiss.jpg', 'marcel')
343
+ pp policy.grants
344
+ [#<AWS::S3::ACL::Grant FULL_CONTROL to noradio>,
345
+ #<AWS::S3::ACL::Grant READ to AllUsers Group>]
346
+
347
+ Policies are made up of one or more grants which grant a specific permission to some grantee. Here we see the default FULL_CONTROL grant
348
+ to the owner of this object. There is also READ permission granted to the Allusers Group, which means anyone has read access for the object.
349
+
350
+ Say we wanted to grant access to anyone to read the access policy of this object. The current READ permission only grants them permission to read
351
+ the object itself (for example, from a browser) but it does not allow them to read the access policy. For that we will need to grant the AllUsers group the READ_ACP permission.
352
+
353
+ First we'll create a new grant object:
354
+
355
+ grant = ACL::Grant.new
356
+ # => #<AWS::S3::ACL::Grant (permission) to (grantee)>
357
+ grant.permission = 'READ_ACP'
358
+
359
+ Now we need to indicate who this grant is for. In other words, who the grantee is:
360
+
361
+ grantee = ACL::Grantee.new
362
+ # => #<AWS::S3::ACL::Grantee (xsi not set yet)>
363
+
364
+ There are three ways to specify a grantee: 1) by their internal amazon id, such as the one returned with an object's Owner,
365
+ 2) by their Amazon account email address or 3) by specifying a group. As of this writing you can not create custom groups, but
366
+ Amazon does provide three already: AllUsers, Authenticated and LogDelivery. In this case we want to provide the grant to all users.
367
+ This effectively means "anyone".
368
+
369
+ grantee.group = 'AllUsers'
370
+
371
+ Now that our grantee is setup, we'll associate it with the grant:
372
+
373
+ grant.grantee = grantee
374
+ grant
375
+ # => #<AWS::S3::ACL::Grant READ_ACP to AllUsers Group>
376
+
377
+ Are grant has all the information we need. Now that it's ready, we'll add it on to the object's access control policy's list of grants:
378
+
379
+ policy.grants << grant
380
+ pp policy.grants
381
+ [#<AWS::S3::ACL::Grant FULL_CONTROL to noradio>,
382
+ #<AWS::S3::ACL::Grant READ to AllUsers Group>,
383
+ #<AWS::S3::ACL::Grant READ_ACP to AllUsers Group>]
384
+
385
+ Now that the policy has the new grant, we reuse the <tt>acl</tt> method to persist the policy change:
386
+
387
+ S3Object.acl('kiss.jpg', 'marcel', policy)
388
+
389
+ If we fetch the object's policy again, we see that the grant has been added:
390
+
391
+ pp S3Object.acl('kiss.jpg', 'marcel').grants
392
+ [#<AWS::S3::ACL::Grant FULL_CONTROL to noradio>,
393
+ #<AWS::S3::ACL::Grant READ to AllUsers Group>,
394
+ #<AWS::S3::ACL::Grant READ_ACP to AllUsers Group>]
395
+
396
+ If we were to access this object's acl url from a browser:
397
+
398
+ http://s3.amazonaws.com/marcel/kiss.jpg?acl
399
+
400
+ we would be shown its access control policy.
401
+
402
+ ==== Pre-prepared grants
403
+
404
+ Alternatively, the ACL::Grant class defines a set of stock grant policies that you can fetch by name. In most cases, you can
405
+ just use one of these pre-prepared grants rather than building grants by hand. Two of these stock policies are <tt>:public_read</tt>
406
+ and <tt>:public_read_acp</tt>, which happen to be the two grants that we built by hand above. In this case we could have simply written:
407
+
408
+ policy.grants << ACL::Grant.grant(:public_read)
409
+ policy.grants << ACL::Grant.grant(:public_read_acp)
410
+ S3Object.acl('kiss.jpg', 'marcel', policy)
411
+
412
+ The full details can be found in ACL::Policy, ACL::Grant and ACL::Grantee.
413
+
414
+
415
+ ==== Accessing private objects from a browser
416
+
417
+ All private objects are accessible via an authenticated GET request to the S3 servers. You can generate an
418
+ authenticated url for an object like this:
419
+
420
+ S3Object.url_for('beluga_baby.jpg', 'marcel_molina')
421
+
422
+ By default authenticated urls expire 5 minutes after they were generated.
423
+
424
+ Expiration options can be specified either with an absolute time since the epoch with the <tt>:expires</tt> options,
425
+ or with a number of seconds relative to now with the <tt>:expires_in</tt> options:
426
+
427
+ # Absolute expiration date
428
+ # (Expires January 18th, 2038)
429
+ doomsday = Time.mktime(2038, 1, 18).to_i
430
+ S3Object.url_for('beluga_baby.jpg',
431
+ 'marcel',
432
+ :expires => doomsday)
433
+
434
+ # Expiration relative to now specified in seconds
435
+ # (Expires in 3 hours)
436
+ S3Object.url_for('beluga_baby.jpg',
437
+ 'marcel',
438
+ :expires_in => 60 * 60 * 3)
439
+
440
+ You can specify whether the url should go over SSL with the <tt>:use_ssl</tt> option:
441
+
442
+ # Url will use https protocol
443
+ S3Object.url_for('beluga_baby.jpg',
444
+ 'marcel',
445
+ :use_ssl => true)
446
+
447
+ By default, the ssl settings for the current connection will be used.
448
+
449
+ If you have an object handy, you can use its <tt>url</tt> method with the same objects:
450
+
451
+ song.url(:expires_in => 30)
452
+
453
+ To get an unauthenticated url for the object, such as in the case
454
+ when the object is publicly readable, pass the
455
+ <tt>:authenticated</tt> option with a value of <tt>false</tt>.
456
+
457
+ S3Object.url_for('beluga_baby.jpg',
458
+ 'marcel',
459
+ :authenticated => false)
460
+ # => http://s3.amazonaws.com/marcel/beluga_baby.jpg
461
+
462
+
463
+ == Logging
464
+ ==== Tracking requests made on a bucket
465
+
466
+ A bucket can be set to log the requests made on it. By default logging is turned off. You can check if a bucket has logging enabled:
467
+
468
+ Bucket.logging_enabled_for? 'jukebox'
469
+ # => false
470
+
471
+ Enabling it is easy:
472
+
473
+ Bucket.enable_logging_for('jukebox')
474
+
475
+ Unless you specify otherwise, logs will be written to the bucket you want to log. The logs are just like any other object. By default they will start with the prefix 'log-'. You can customize what bucket you want the logs to be delivered to, as well as customize what the log objects' key is prefixed with by setting the <tt>target_bucket</tt> and <tt>target_prefix</tt> option:
476
+
477
+ Bucket.enable_logging_for(
478
+ 'jukebox', 'target_bucket' => 'jukebox-logs'
479
+ )
480
+
481
+ Now instead of logging right into the jukebox bucket, the logs will go into the bucket called jukebox-logs.
482
+
483
+ Once logs have accumulated, you can access them using the <tt>logs</tt> method:
484
+
485
+ pp Bucket.logs('jukebox')
486
+ [#<AWS::S3::Logging::Log '/jukebox-logs/log-2006-11-14-07-15-24-2061C35880A310A1'>,
487
+ #<AWS::S3::Logging::Log '/jukebox-logs/log-2006-11-14-08-15-27-D8EEF536EC09E6B3'>,
488
+ #<AWS::S3::Logging::Log '/jukebox-logs/log-2006-11-14-08-15-29-355812B2B15BD789'>]
489
+
490
+ Each log has a <tt>lines</tt> method that gives you information about each request in that log. All the fields are available
491
+ as named methods. More information is available in Logging::Log::Line.
492
+
493
+ logs = Bucket.logs('jukebox')
494
+ log = logs.first
495
+ line = log.lines.first
496
+ line.operation
497
+ # => 'REST.GET.LOGGING_STATUS'
498
+ line.request_uri
499
+ # => 'GET /jukebox?logging HTTP/1.1'
500
+ line.remote_ip
501
+ # => "67.165.183.125"
502
+
503
+ Disabling logging is just as simple as enabling it:
504
+
505
+ Bucket.disable_logging_for('jukebox')
506
+
507
+
508
+ == Errors
509
+ ==== When things go wrong
510
+
511
+ Anything you do that makes a request to S3 could result in an error. If it does, the AWS::S3 library will raise an exception
512
+ specific to the error. All exception that are raised as a result of a request returning an error response inherit from the
513
+ ResponseError exception. So should you choose to rescue any such exception, you can simple rescue ResponseError.
514
+
515
+ Say you go to delete a bucket, but the bucket turns out to not be empty. This results in a BucketNotEmpty error (one of the many
516
+ errors listed at http://docs.amazonwebservices.com/AmazonS3/2006-03-01/ErrorCodeList.html):
517
+
518
+ begin
519
+ Bucket.delete('jukebox')
520
+ rescue ResponseError => error
521
+ # ...
522
+ end
523
+
524
+ Once you've captured the exception, you can extract the error message from S3, as well as the full error response, which includes
525
+ things like the HTTP response code:
526
+
527
+ error
528
+ # => #<AWS::S3::BucketNotEmpty The bucket you tried to delete is not empty>
529
+ error.message
530
+ # => "The bucket you tried to delete is not empty"
531
+ error.response.code
532
+ # => 409
533
+
534
+ You could use this information to redisplay the error in a way you see fit, or just to log the error and continue on.
535
+
536
+
537
+ ==== Accessing the last request's response
538
+
539
+ Sometimes methods that make requests to the S3 servers return some object, like a Bucket or an S3Object.
540
+ Othertimes they return just <tt>true</tt>. Other times they raise an exception that you may want to rescue. Despite all these
541
+ possible outcomes, every method that makes a request stores its response object for you in Service.response. You can always
542
+ get to the last request's response via Service.response.
543
+
544
+ objects = Bucket.objects('jukebox')
545
+ Service.response.success?
546
+ # => true
547
+
548
+ This is also useful when an error exception is raised in the console which you weren't expecting. You can
549
+ root around in the response to get more details of what might have gone wrong.
550
+
551
+