surat-aws-s3 0.0.1

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