aws-s3 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/COPYING +19 -0
- data/INSTALL +35 -0
- data/README +529 -0
- data/Rakefile +284 -0
- data/bin/s3sh +4 -0
- data/bin/setup.rb +10 -0
- data/lib/aws/s3.rb +64 -0
- data/lib/aws/s3/acl.rb +631 -0
- data/lib/aws/s3/authentication.rb +218 -0
- data/lib/aws/s3/base.rb +232 -0
- data/lib/aws/s3/bittorrent.rb +58 -0
- data/lib/aws/s3/bucket.rb +323 -0
- data/lib/aws/s3/connection.rb +212 -0
- data/lib/aws/s3/error.rb +69 -0
- data/lib/aws/s3/exceptions.rb +130 -0
- data/lib/aws/s3/extensions.rb +186 -0
- data/lib/aws/s3/logging.rb +163 -0
- data/lib/aws/s3/object.rb +565 -0
- data/lib/aws/s3/owner.rb +44 -0
- data/lib/aws/s3/parsing.rb +138 -0
- data/lib/aws/s3/response.rb +180 -0
- data/lib/aws/s3/service.rb +43 -0
- data/lib/aws/s3/version.rb +12 -0
- data/support/faster-xml-simple/lib/faster_xml_simple.rb +115 -0
- data/support/faster-xml-simple/test/regression_test.rb +16 -0
- data/support/faster-xml-simple/test/xml_simple_comparison_test.rb +22 -0
- data/support/rdoc/code_info.rb +211 -0
- data/test/acl_test.rb +243 -0
- data/test/authentication_test.rb +96 -0
- data/test/base_test.rb +143 -0
- data/test/bucket_test.rb +48 -0
- data/test/connection_test.rb +120 -0
- data/test/error_test.rb +75 -0
- data/test/extensions_test.rb +282 -0
- data/test/fixtures.rb +89 -0
- data/test/fixtures/buckets.yml +102 -0
- data/test/fixtures/errors.yml +34 -0
- data/test/fixtures/headers.yml +3 -0
- data/test/fixtures/logging.yml +15 -0
- data/test/fixtures/policies.yml +16 -0
- data/test/logging_test.rb +36 -0
- data/test/mocks/base.rb +89 -0
- data/test/object_test.rb +177 -0
- data/test/parsing_test.rb +82 -0
- data/test/remote/acl_test.rb +117 -0
- data/test/remote/bittorrent_test.rb +45 -0
- data/test/remote/bucket_test.rb +127 -0
- data/test/remote/logging_test.rb +82 -0
- data/test/remote/object_test.rb +267 -0
- data/test/remote/test_file.data +0 -0
- data/test/remote/test_helper.rb +30 -0
- data/test/response_test.rb +70 -0
- data/test/service_test.rb +26 -0
- data/test/test_helper.rb +82 -0
- metadata +125 -0
data/COPYING
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
#
|
2
|
+
# Copyright (c) 2006 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,35 @@
|
|
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
|
+
== Dependencies
|
30
|
+
|
31
|
+
AWS::S3 depends on XmlSimple (http://xml-simple.rubyforge.org/). When installing aws/s3 with
|
32
|
+
Rubygems, this dependency will be taken care of for you. Otherwise, installation instructions are listed on the xml-simple
|
33
|
+
site.
|
34
|
+
|
35
|
+
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.
|
data/README
ADDED
@@ -0,0 +1,529 @@
|
|
1
|
+
= AWS::S3
|
2
|
+
|
3
|
+
AWS::S3 is a Ruby library for Amazon's Simple Storage Service's REST API (http://aws.amazon.com/s3).
|
4
|
+
Full documentation of the currently supported API can be found at http://docs.amazonwebservices.com/AmazonS3/2006-03-01.
|
5
|
+
|
6
|
+
== Getting started
|
7
|
+
|
8
|
+
To get started you need to require 'aws/s3':
|
9
|
+
|
10
|
+
% irb -rubygems
|
11
|
+
irb(main):001:0> require 'aws/s3'
|
12
|
+
# => true
|
13
|
+
|
14
|
+
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.
|
15
|
+
|
16
|
+
% s3sh
|
17
|
+
>> Version
|
18
|
+
|
19
|
+
Before you can do anything, you must establish a connection using Base.establish_connection!. A basic connection would look something like this:
|
20
|
+
|
21
|
+
AWS::S3::Base.establish_connection!(
|
22
|
+
:access_key_id => 'abc',
|
23
|
+
:secret_access_key => '123'
|
24
|
+
)
|
25
|
+
|
26
|
+
The minimum connection options that you must specify are your access key id and your secret access key.
|
27
|
+
|
28
|
+
(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.)
|
29
|
+
|
30
|
+
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:
|
31
|
+
|
32
|
+
% cat .amazon_keys
|
33
|
+
export AMAZON_ACCESS_KEY_ID='abcdefghijklmnop'
|
34
|
+
export AMAZON_SECRET_ACCESS_KEY='1234567891012345'
|
35
|
+
|
36
|
+
Then load it in your shell's rc file.
|
37
|
+
|
38
|
+
% cat .zshrc
|
39
|
+
if [[ -f "$HOME/.amazon_keys" ]]; then
|
40
|
+
source "$HOME/.amazon_keys";
|
41
|
+
fi
|
42
|
+
|
43
|
+
See more connection details at AWS::S3::Connection::Management::ClassMethods.
|
44
|
+
|
45
|
+
|
46
|
+
== AWS::S3 Basics
|
47
|
+
=== The service, buckets and objects
|
48
|
+
|
49
|
+
The three main concepts of S3 are the service, buckets and objects.
|
50
|
+
|
51
|
+
==== The service
|
52
|
+
|
53
|
+
The service lets you find out general information about your account, like what buckets you have.
|
54
|
+
|
55
|
+
Service.buckets
|
56
|
+
# => []
|
57
|
+
|
58
|
+
|
59
|
+
==== Buckets
|
60
|
+
|
61
|
+
Buckets are containers for objects (the files you store on S3). To create a new bucket you just specify its name.
|
62
|
+
|
63
|
+
# Pick a unique name, or else you'll get an error
|
64
|
+
# if the name is already taken.
|
65
|
+
Bucket.create('jukebox')
|
66
|
+
|
67
|
+
Bucket names must be unique across the entire S3 system, sort of like domain names across the internet. If you try
|
68
|
+
to create a bucket with a name that is already taken, you will get an error.
|
69
|
+
|
70
|
+
Assuming the name you chose isn't already taken, your new bucket will now appear in the bucket list:
|
71
|
+
|
72
|
+
Service.buckets
|
73
|
+
# => [#<AWS::S3::Bucket @attributes={"name"=>"jukebox"}>]
|
74
|
+
|
75
|
+
Once you have succesfully created a bucket you can you can fetch it by name using Bucket.find.
|
76
|
+
|
77
|
+
music_bucket = Bucket.find('jukebox')
|
78
|
+
|
79
|
+
The bucket that is returned will contain a listing of all the objects in the bucket.
|
80
|
+
|
81
|
+
music_bucket.objects.size
|
82
|
+
# => 0
|
83
|
+
|
84
|
+
If all you are interested in is the objects of the bucket, you can get to them directly using Bucket.objects.
|
85
|
+
|
86
|
+
Bucket.objects('jukebox').size
|
87
|
+
# => 0
|
88
|
+
|
89
|
+
By default all objects will be returned, though there are several options you can use to limit what is returned, such as
|
90
|
+
specifying that only objects whose name is after a certain place in the alphabet be returned, and etc. Details about these options can
|
91
|
+
be found in the documentation for Bucket.find.
|
92
|
+
|
93
|
+
To add an object to a bucket you specify the name of the object, its value, and the bucket to put it in.
|
94
|
+
|
95
|
+
file = 'black-flowers.mp3'
|
96
|
+
S3Object.store(
|
97
|
+
file,
|
98
|
+
File.open(file),
|
99
|
+
'jukebox',
|
100
|
+
:content_type => 'audio/mpeg'
|
101
|
+
)
|
102
|
+
|
103
|
+
Though strictly not required, you should specify the content type of the file so that when you go to download it your browser will know how to handle the file.
|
104
|
+
|
105
|
+
You'll see your file has been added to it:
|
106
|
+
|
107
|
+
music_bucket.objects
|
108
|
+
# => [#<AWS::S3::S3Object '/jukebox/black-flowers.mp3'>]
|
109
|
+
|
110
|
+
You can treat your bucket like a hash and access objects by name:
|
111
|
+
|
112
|
+
jukebox['black-flowers.mp3']
|
113
|
+
# => #<AWS::S3::S3Object '/jukebox/black-flowers.mp3'>
|
114
|
+
|
115
|
+
In the event that you want to delete a bucket, you can use Bucket.delete.
|
116
|
+
|
117
|
+
Bucket.delete('jukebox')
|
118
|
+
|
119
|
+
Keep in mind, like unix directories, you can not delete a bucket unless it is empty. Trying to delete a bucket
|
120
|
+
that contains objects will raise a BucketNotEmpty exception.
|
121
|
+
|
122
|
+
Passing the :force => true option to delete will take care of deleting all the bucket's objects for you.
|
123
|
+
|
124
|
+
Bucket.delete('photos', :force => true)
|
125
|
+
# => true
|
126
|
+
|
127
|
+
|
128
|
+
==== Objects
|
129
|
+
|
130
|
+
S3Objects represent the data you store on S3. They have a key (their name) and a value (their data). All objects belong to a
|
131
|
+
bucket.
|
132
|
+
|
133
|
+
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:
|
134
|
+
|
135
|
+
S3Object.store(
|
136
|
+
'headshot.jpg',
|
137
|
+
File.open('headshot.jpg'),
|
138
|
+
'photos',
|
139
|
+
:content_type => 'image/jpg'
|
140
|
+
)
|
141
|
+
|
142
|
+
You can read more about storing files on S3 in the documentation for S3Object.store.
|
143
|
+
|
144
|
+
If you just want to fetch an object you've stored on S3, you just specify its name and its bucket:
|
145
|
+
|
146
|
+
picture = S3Object.find 'headshot.jpg', 'photos'
|
147
|
+
|
148
|
+
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.
|
149
|
+
You get the data for the file like this:
|
150
|
+
|
151
|
+
picture.value
|
152
|
+
|
153
|
+
You can fetch just the object's data directly:
|
154
|
+
|
155
|
+
S3Object.value 'headshot.jpg', 'photos'
|
156
|
+
|
157
|
+
Or stream it by passing a block to <tt>stream</tt>:
|
158
|
+
|
159
|
+
File.open('song.mp3', 'w') do |file|
|
160
|
+
S3Object.stream('song.mp3', 'jukebox') do |chunk|
|
161
|
+
file.write chunk
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
The data of the file, once download, is cached, so subsequent calls to <tt>value</tt> won't redownload the file unless you
|
166
|
+
tell the object to reload its <tt>value</tt>:
|
167
|
+
|
168
|
+
# Redownloads the file's data
|
169
|
+
song.value(:reload)
|
170
|
+
|
171
|
+
Other functionality includes:
|
172
|
+
|
173
|
+
# Copying an object
|
174
|
+
S3Object.copy 'headshot.jpg', 'headshot2.jpg', 'photos'
|
175
|
+
|
176
|
+
# Renaming an object
|
177
|
+
S3Object.rename 'headshot.jpg', 'portrait.jpg', 'photos'
|
178
|
+
|
179
|
+
# Deleting an object
|
180
|
+
S3Object.delete 'headshot.jpg', 'photos'
|
181
|
+
|
182
|
+
==== More about objects and their metadata
|
183
|
+
|
184
|
+
You can find out the content type of your object with the <tt>content_type</tt> method:
|
185
|
+
|
186
|
+
song.content_type
|
187
|
+
# => "audio/mpeg"
|
188
|
+
|
189
|
+
You can change the content type as well if you like:
|
190
|
+
|
191
|
+
song.content_type = 'application/octet-stream'
|
192
|
+
song.store
|
193
|
+
|
194
|
+
(Keep in mind that due to limitiations in S3's exposed API, the only way to change things like the content_type
|
195
|
+
is to PUT the object onto S3 again. In the case of large files, this will result in fully re-uploading the file.)
|
196
|
+
|
197
|
+
A bevie of information about an object can be had using the <tt>about</tt> method:
|
198
|
+
|
199
|
+
pp song.about
|
200
|
+
{"last-modified" => "Sat, 28 Oct 2006 21:29:26 GMT",
|
201
|
+
"content-type" => "binary/octect-stream",
|
202
|
+
"etag" => "\"dc629038ffc674bee6f62eb64ff3a\"",
|
203
|
+
"date" => "Sat, 28 Oct 2006 21:30:41 GMT",
|
204
|
+
"x-amz-request-id" => "B7BC68F55495B1C8",
|
205
|
+
"server" => "AmazonS3",
|
206
|
+
"content-length" => "3418766"}
|
207
|
+
|
208
|
+
You can get and set metadata for an object:
|
209
|
+
|
210
|
+
song.metadata
|
211
|
+
# => {}
|
212
|
+
song.metadata[:album] = "A River Ain't Too Much To Love"
|
213
|
+
# => "A River Ain't Too Much To Love"
|
214
|
+
song.metadata[:released] = 2005
|
215
|
+
pp song.metadata
|
216
|
+
{"x-amz-meta-released" => 2005,
|
217
|
+
"x-amz-meta-album" => "A River Ain't Too Much To Love"}
|
218
|
+
song.store
|
219
|
+
|
220
|
+
That metadata will be saved in S3 and is hence forth available from that object:
|
221
|
+
|
222
|
+
song = S3Object.find('black-flowers.mp3', 'jukebox')
|
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.metada[:released]
|
227
|
+
# => "2005"
|
228
|
+
song.metada[:released] = 2006
|
229
|
+
pp song.metada
|
230
|
+
{"x-amz-meta-released" => 2006,
|
231
|
+
"x-amz-meta-album" => "A River Ain't Too Much To Love"}
|
232
|
+
|
233
|
+
|
234
|
+
==== Streaming uploads
|
235
|
+
|
236
|
+
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.
|
237
|
+
If <tt>data</tt> is an I/O stream it will be read in segments and written to the socket incrementally. This approach
|
238
|
+
may be desirable for very large files so they are not read into memory all at once.
|
239
|
+
|
240
|
+
# Non streamed upload
|
241
|
+
S3Object.store('simple-text-file.txt',
|
242
|
+
'hello world!',
|
243
|
+
'marcel',
|
244
|
+
:content_type => 'text/plain')
|
245
|
+
|
246
|
+
# Streamed upload
|
247
|
+
S3Object.store('roots.mpeg',
|
248
|
+
File.open('roots.mpeg'),
|
249
|
+
'marcel',
|
250
|
+
:content_type => 'audio/mpeg')
|
251
|
+
|
252
|
+
|
253
|
+
== Setting the current bucket
|
254
|
+
==== Scoping operations to a specific bucket
|
255
|
+
|
256
|
+
If you plan on always using a specific bucket for certain files, you can skip always having to specify the bucket by creating
|
257
|
+
a subclass of Bucket or S3Object and telling it what bucket to use:
|
258
|
+
|
259
|
+
class JukeBoxSong < AWS::S3::S3Object
|
260
|
+
set_current_bucket_to 'jukebox'
|
261
|
+
end
|
262
|
+
|
263
|
+
For all methods that take a bucket name as an argument, the current bucket will be used if the bucket name argument is omitted.
|
264
|
+
|
265
|
+
other_song = '/Users/marcel/baby-please-come-home.mp3'
|
266
|
+
JukeBoxSong.store(
|
267
|
+
File.basename(other_song),
|
268
|
+
File.open(other_song),
|
269
|
+
:content_type => 'audio/mpeg'
|
270
|
+
)
|
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 infered, 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
|
+
:content_type => 'image/jpg', :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 costum 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
|
+
|
454
|
+
== Logging
|
455
|
+
==== Tracking requests made on a bucket
|
456
|
+
|
457
|
+
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:
|
458
|
+
|
459
|
+
Bucket.logging_enabled_for? 'jukebox'
|
460
|
+
# => false
|
461
|
+
|
462
|
+
Enabling it is easy:
|
463
|
+
|
464
|
+
Bucket.enable_logging_for('jukebox')
|
465
|
+
|
466
|
+
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:
|
467
|
+
|
468
|
+
Bucket.enable_logging_for(
|
469
|
+
'jukebox', 'target_bucket' => 'jukebox-logs'
|
470
|
+
)
|
471
|
+
|
472
|
+
Now instead of logging right into the jukebox bucket, the logs will go into the bucket called jukebox-logs.
|
473
|
+
|
474
|
+
Once logs have accumulated, you can access them using the <tt>log</tt> method:
|
475
|
+
|
476
|
+
pp Bucket.logs('jukebox')
|
477
|
+
[#<AWS::S3::S3Object '/jukebox-logs/log-2006-11-14-07-15-24-2061C35880A310A1'>,
|
478
|
+
#<AWS::S3::S3Object '/jukebox-logs/log-2006-11-14-08-15-27-D8EEF536EC09E6B3'>,
|
479
|
+
#<AWS::S3::S3Object '/jukebox-logs/log-2006-11-14-08-15-29-355812B2B15BD789'>]
|
480
|
+
|
481
|
+
Disabling logging is just as simple as enabling it:
|
482
|
+
|
483
|
+
Bucket.disable_logging_for('jukebox')
|
484
|
+
|
485
|
+
|
486
|
+
== Errors
|
487
|
+
==== When things go wrong
|
488
|
+
|
489
|
+
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
|
490
|
+
specific to the error. All exception that are raised as a result of a request returning an error response inherit from the
|
491
|
+
ResponseError exception. So should you choose to rescue any such exception, you can simple rescue ResponseError.
|
492
|
+
|
493
|
+
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
|
494
|
+
errors listed at http://docs.amazonwebservices.com/AmazonS3/2006-03-01/ErrorCodeList.html):
|
495
|
+
|
496
|
+
begin
|
497
|
+
Bucket.delete('jukebox')
|
498
|
+
rescue ResponseError => error
|
499
|
+
# ...
|
500
|
+
end
|
501
|
+
|
502
|
+
Once you've captured the exception, you can extract the error message from S3, as well as the full error response, which includes
|
503
|
+
things like the HTTP response code:
|
504
|
+
|
505
|
+
error
|
506
|
+
# => #<AWS::S3::BucketNotEmpty The bucket you tried to delete is not empty>
|
507
|
+
error.message
|
508
|
+
# => "The bucket you tried to delete is not empty"
|
509
|
+
error.response.code
|
510
|
+
# => 409
|
511
|
+
|
512
|
+
You could use this information to redisplay the error in a way you see fit, or just to log the error and continue on.
|
513
|
+
|
514
|
+
|
515
|
+
==== Accessing the last request's response
|
516
|
+
|
517
|
+
Sometimes methods that make requests to the S3 servers return some object, like a Bucket or an S3Object.
|
518
|
+
Othertimes they return just <tt>true</tt>. Other times they raise an exception that you may want to rescue. Despite all these
|
519
|
+
possible outcomes, every method that makes a request stores its response object for you in Service.response. You can always
|
520
|
+
get to the last request's response via Service.response.
|
521
|
+
|
522
|
+
objects = Bucket.objects('jukebox')
|
523
|
+
Service.response.success?
|
524
|
+
# => true
|
525
|
+
|
526
|
+
This is also useful when an error exception is raised in the console which you weren't expecting. You can
|
527
|
+
root around in the response to get more details of what might have gone wrong.
|
528
|
+
|
529
|
+
|