ijin-right_aws 1.11.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. data/History.txt +239 -0
  2. data/Manifest.txt +46 -0
  3. data/README.txt +167 -0
  4. data/Rakefile +125 -0
  5. data/VERSION +1 -0
  6. data/lib/acf/right_acf_interface.rb +413 -0
  7. data/lib/acw/right_acw_interface.rb +249 -0
  8. data/lib/as/right_as_interface.rb +690 -0
  9. data/lib/awsbase/benchmark_fix.rb +39 -0
  10. data/lib/awsbase/right_awsbase.rb +931 -0
  11. data/lib/awsbase/support.rb +115 -0
  12. data/lib/ec2/right_ec2.rb +617 -0
  13. data/lib/ec2/right_ec2_ebs.rb +451 -0
  14. data/lib/ec2/right_ec2_images.rb +373 -0
  15. data/lib/ec2/right_ec2_instances.rb +760 -0
  16. data/lib/ec2/right_ec2_monitoring.rb +70 -0
  17. data/lib/ec2/right_ec2_reserved_instances.rb +167 -0
  18. data/lib/ec2/right_ec2_vpc.rb +571 -0
  19. data/lib/elb/right_elb_interface.rb +407 -0
  20. data/lib/rds/right_rds_interface.rb +998 -0
  21. data/lib/right_aws.rb +79 -0
  22. data/lib/s3/right_s3.rb +1102 -0
  23. data/lib/s3/right_s3_interface.rb +1195 -0
  24. data/lib/sdb/active_sdb.rb +930 -0
  25. data/lib/sdb/right_sdb_interface.rb +672 -0
  26. data/lib/sqs/right_sqs.rb +388 -0
  27. data/lib/sqs/right_sqs_gen2.rb +343 -0
  28. data/lib/sqs/right_sqs_gen2_interface.rb +523 -0
  29. data/lib/sqs/right_sqs_interface.rb +594 -0
  30. data/test/acf/test_helper.rb +2 -0
  31. data/test/acf/test_right_acf.rb +146 -0
  32. data/test/awsbase/test_helper.rb +2 -0
  33. data/test/awsbase/test_right_awsbase.rb +12 -0
  34. data/test/ec2/test_helper.rb +2 -0
  35. data/test/ec2/test_right_ec2.rb +108 -0
  36. data/test/http_connection.rb +87 -0
  37. data/test/rds/test_helper.rb +2 -0
  38. data/test/rds/test_right_rds.rb +120 -0
  39. data/test/s3/test_helper.rb +2 -0
  40. data/test/s3/test_right_s3.rb +419 -0
  41. data/test/s3/test_right_s3_stubbed.rb +95 -0
  42. data/test/sdb/test_active_sdb.rb +299 -0
  43. data/test/sdb/test_helper.rb +3 -0
  44. data/test/sdb/test_right_sdb.rb +247 -0
  45. data/test/sqs/test_helper.rb +2 -0
  46. data/test/sqs/test_right_sqs.rb +291 -0
  47. data/test/sqs/test_right_sqs_gen2.rb +276 -0
  48. data/test/test_credentials.rb +37 -0
  49. data/test/ts_right_aws.rb +14 -0
  50. metadata +122 -0
@@ -0,0 +1,79 @@
1
+ #
2
+ # Copyright (c) 2007-2008 RightScale Inc
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining
5
+ # a copy of this software and associated documentation files (the
6
+ # "Software"), to deal in the Software without restriction, including
7
+ # without limitation the rights to use, copy, modify, merge, publish,
8
+ # distribute, sublicense, and/or sell copies of the Software, and to
9
+ # permit persons to whom the Software is furnished to do so, subject to
10
+ # the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ #
23
+
24
+ require 'benchmark'
25
+ require 'net/https'
26
+ require 'uri'
27
+ require 'time'
28
+ require "cgi"
29
+ require "base64"
30
+ require "rexml/document"
31
+ require "openssl"
32
+ require "digest/sha1"
33
+
34
+ require 'rubygems'
35
+ require 'right_http_connection'
36
+
37
+ $:.unshift(File.dirname(__FILE__))
38
+ require 'awsbase/benchmark_fix'
39
+ require 'awsbase/support'
40
+ require 'awsbase/right_awsbase'
41
+ require 'ec2/right_ec2'
42
+ require 'ec2/right_ec2_images'
43
+ require 'ec2/right_ec2_instances'
44
+ require 'ec2/right_ec2_ebs'
45
+ require 'ec2/right_ec2_reserved_instances'
46
+ require 'ec2/right_ec2_vpc'
47
+ require 'ec2/right_ec2_monitoring'
48
+ require 'elb/right_elb_interface'
49
+ require 'acw/right_acw_interface'
50
+ require 'as/right_as_interface'
51
+ require 's3/right_s3_interface'
52
+ require 's3/right_s3'
53
+ require 'sqs/right_sqs_interface'
54
+ require 'sqs/right_sqs'
55
+ require 'sqs/right_sqs_gen2_interface'
56
+ require 'sqs/right_sqs_gen2'
57
+ require 'sdb/right_sdb_interface'
58
+ require 'acf/right_acf_interface'
59
+ require 'rds/right_rds_interface'
60
+
61
+
62
+ module RightAws #:nodoc:
63
+ module VERSION #:nodoc:
64
+ MAJOR = 1
65
+ MINOR = 11
66
+ TINY = 0
67
+
68
+ STRING = [MAJOR, MINOR, TINY].join('.')
69
+ end
70
+ end
71
+
72
+ #-
73
+
74
+ # We also want everything available in the Rightscale namespace for backward
75
+ # compatibility reasons.
76
+ module Rightscale #:nodoc:
77
+ include RightAws
78
+ extend RightAws
79
+ end
@@ -0,0 +1,1102 @@
1
+ #
2
+ # Copyright (c) 2007-2008 RightScale Inc
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining
5
+ # a copy of this software and associated documentation files (the
6
+ # "Software"), to deal in the Software without restriction, including
7
+ # without limitation the rights to use, copy, modify, merge, publish,
8
+ # distribute, sublicense, and/or sell copies of the Software, and to
9
+ # permit persons to whom the Software is furnished to do so, subject to
10
+ # the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ #
23
+
24
+ module RightAws
25
+
26
+ # = RightAws::S3 -- RightScale's Amazon S3 interface
27
+ # The RightAws::S3 class provides a complete interface to Amazon's Simple
28
+ # Storage Service.
29
+ # For explanations of the semantics
30
+ # of each call, please refer to Amazon's documentation at
31
+ # http://developer.amazonwebservices.com/connect/kbcategory.jspa?categoryID=48
32
+ #
33
+ # See examples below for the bucket and buckets methods.
34
+ #
35
+ # Error handling: all operations raise an RightAws::AwsError in case
36
+ # of problems. Note that transient errors are automatically retried.
37
+ #
38
+ # It is a good way to use domain naming style getting a name for the buckets.
39
+ # See http://docs.amazonwebservices.com/AmazonS3/2006-03-01/UsingBucket.html
40
+ # about the naming convention for the buckets. This case they can be accessed using a virtual domains.
41
+ #
42
+ # Let assume you have 3 buckets: 'awesome-bucket', 'awesome_bucket' and 'AWEsomE-bucket'.
43
+ # The first ones objects can be accessed as: http:// awesome-bucket.s3.amazonaws.com/key/object
44
+ #
45
+ # But the rest have to be accessed as:
46
+ # http:// s3.amazonaws.com/awesome_bucket/key/object and http:// s3.amazonaws.com/AWEsomE-bucket/key/object
47
+ #
48
+ # See: http://docs.amazonwebservices.com/AmazonS3/2006-03-01/VirtualHosting.html for better explanation.
49
+ #
50
+ class S3
51
+ attr_reader :interface
52
+
53
+ # Create a new handle to an S3 account. All handles share the same per process or per thread
54
+ # HTTP connection to Amazon S3. Each handle is for a specific account.
55
+ # The +params+ are passed through as-is to RightAws::S3Interface.new
56
+ #
57
+ # Params is a hash:
58
+ #
59
+ # {:server => 's3.amazonaws.com' # Amazon service host: 's3.amazonaws.com'(default)
60
+ # :port => 443 # Amazon service port: 80 or 443(default)
61
+ # :protocol => 'https' # Amazon service protocol: 'http' or 'https'(default)
62
+ # :multi_thread => true|false # Multi-threaded (connection per each thread): true or false(default)
63
+ # :logger => Logger Object} # Logger instance: logs to STDOUT if omitted }
64
+ def initialize(aws_access_key_id=nil, aws_secret_access_key=nil, params={})
65
+ @interface = S3Interface.new(aws_access_key_id, aws_secret_access_key, params)
66
+ end
67
+
68
+ # Retrieve a list of buckets.
69
+ # Returns an array of RightAws::S3::Bucket instances.
70
+ # # Create handle to S3 account
71
+ # s3 = RightAws::S3.new(aws_access_key_id, aws_secret_access_key)
72
+ # my_buckets_names = s3.buckets.map{|b| b.name}
73
+ # puts "Buckets on S3: #{my_bucket_names.join(', ')}"
74
+ def buckets
75
+ @interface.list_all_my_buckets.map! do |entry|
76
+ owner = Owner.new(entry[:owner_id], entry[:owner_display_name])
77
+ Bucket.new(self, entry[:name], entry[:creation_date], owner)
78
+ end
79
+ end
80
+
81
+ # Retrieve an individual bucket.
82
+ # If the bucket does not exist and +create+ is set, a new bucket
83
+ # is created on S3. Launching this method with +create+=+true+ may
84
+ # affect on the bucket's ACL if the bucket already exists.
85
+ # Returns a RightAws::S3::Bucket instance or +nil+ if the bucket does not exist
86
+ # and +create+ is not set.
87
+ #
88
+ # s3 = RightAws::S3.new(aws_access_key_id, aws_secret_access_key)
89
+ # bucket1 = s3.bucket('my_awesome_bucket_1')
90
+ # bucket1.keys #=> exception here if the bucket does not exists
91
+ # ...
92
+ # bucket2 = s3.bucket('my_awesome_bucket_2', true)
93
+ # bucket2.keys #=> list of keys
94
+ # # create a bucket at the European location with public read access
95
+ # bucket3 = s3.bucket('my-awesome-bucket-3', true, 'public-read', :location => :eu)
96
+ #
97
+ # see http://docs.amazonwebservices.com/AmazonS3/2006-03-01/RESTAccessPolicy.html
98
+ # (section: Canned Access Policies)
99
+ #
100
+ def bucket(name, create=false, perms=nil, headers={})
101
+ headers['x-amz-acl'] = perms if perms
102
+ @interface.create_bucket(name, headers) if create
103
+ buckets.each { |bucket| return bucket if bucket.name == name }
104
+ nil
105
+ end
106
+
107
+
108
+ class Bucket
109
+ attr_reader :s3, :name, :owner, :creation_date
110
+
111
+ # Create a Bucket instance.
112
+ # If the bucket does not exist and +create+ is set, a new bucket
113
+ # is created on S3. Launching this method with +create+=+true+ may
114
+ # affect on the bucket's ACL if the bucket already exists.
115
+ # Returns Bucket instance or +nil+ if the bucket does not exist
116
+ # and +create+ is not set.
117
+ #
118
+ # s3 = RightAws::S3.new(aws_access_key_id, aws_secret_access_key)
119
+ # ...
120
+ # bucket1 = RightAws::S3::Bucket.create(s3, 'my_awesome_bucket_1')
121
+ # bucket1.keys #=> exception here if the bucket does not exists
122
+ # ...
123
+ # bucket2 = RightAws::S3::Bucket.create(s3, 'my_awesome_bucket_2', true)
124
+ # bucket2.keys #=> list of keys
125
+ # # create a bucket at the European location with public read access
126
+ # bucket3 = RightAws::S3::Bucket.create(s3,'my-awesome-bucket-3', true, 'public-read', :location => :eu)
127
+ #
128
+ # see http://docs.amazonwebservices.com/AmazonS3/2006-03-01/RESTAccessPolicy.html
129
+ # (section: Canned Access Policies)
130
+ #
131
+ def self.create(s3, name, create=false, perms=nil, headers={})
132
+ s3.bucket(name, create, perms, headers)
133
+ end
134
+
135
+
136
+ # Create a bucket instance. In normal use this method should
137
+ # not be called directly.
138
+ # Use RightAws::S3::Bucket.create or RightAws::S3.bucket instead.
139
+ def initialize(s3, name, creation_date=nil, owner=nil)
140
+ @s3 = s3
141
+ @name = name
142
+ @owner = owner
143
+ @creation_date = creation_date
144
+ if @creation_date && !@creation_date.is_a?(Time)
145
+ @creation_date = Time.parse(@creation_date)
146
+ end
147
+ end
148
+
149
+ # Return bucket name as a String.
150
+ #
151
+ # bucket = RightAws::S3.bucket('my_awesome_bucket')
152
+ # puts bucket #=> 'my_awesome_bucket'
153
+ #
154
+ def to_s
155
+ @name.to_s
156
+ end
157
+ alias_method :full_name, :to_s
158
+
159
+ # Return a public link to bucket.
160
+ #
161
+ # bucket.public_link #=> 'https://s3.amazonaws.com:443/my_awesome_bucket'
162
+ #
163
+ def public_link
164
+ params = @s3.interface.params
165
+ "#{params[:protocol]}://#{params[:server]}:#{params[:port]}/#{full_name}"
166
+ end
167
+
168
+ # Returns the bucket location
169
+ def location
170
+ @location ||= @s3.interface.bucket_location(@name)
171
+ end
172
+
173
+ # Retrieves the logging configuration for a bucket.
174
+ # Returns a hash of {:enabled, :targetbucket, :targetprefix}
175
+ #
176
+ # bucket.logging_info()
177
+ # => {:enabled=>true, :targetbucket=>"mylogbucket", :targetprefix=>"loggylogs/"}
178
+ def logging_info
179
+ @s3.interface.get_logging_parse(:bucket => @name)
180
+ end
181
+
182
+ # Enables S3 server access logging on a bucket. The target bucket must have been properly configured to receive server
183
+ # access logs.
184
+ # Params:
185
+ # :targetbucket - either the target bucket object or the name of the target bucket
186
+ # :targetprefix - the prefix under which all logs should be stored
187
+ #
188
+ # bucket.enable_logging(:targetbucket=>"mylogbucket", :targetprefix=>"loggylogs/")
189
+ # => true
190
+ def enable_logging(params)
191
+ AwsUtils.mandatory_arguments([:targetbucket, :targetprefix], params)
192
+ AwsUtils.allow_only([:targetbucket, :targetprefix], params)
193
+ xmldoc = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><BucketLoggingStatus xmlns=\"http://doc.s3.amazonaws.com/2006-03-01\"><LoggingEnabled><TargetBucket>#{params[:targetbucket]}</TargetBucket><TargetPrefix>#{params[:targetprefix]}</TargetPrefix></LoggingEnabled></BucketLoggingStatus>"
194
+ @s3.interface.put_logging(:bucket => @name, :xmldoc => xmldoc)
195
+ end
196
+
197
+ # Disables S3 server access logging on a bucket. Takes no arguments.
198
+ def disable_logging
199
+ xmldoc = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><BucketLoggingStatus xmlns=\"http://doc.s3.amazonaws.com/2006-03-01\"></BucketLoggingStatus>"
200
+ @s3.interface.put_logging(:bucket => @name, :xmldoc => xmldoc)
201
+ end
202
+
203
+ # Retrieve a group of keys from Amazon.
204
+ # +options+ is a hash: { 'prefix'=>'', 'marker'=>'', 'max-keys'=>5, 'delimiter'=>'' }).
205
+ # Retrieves meta-headers information if +head+ it +true+.
206
+ # Returns an array of Key instances.
207
+ #
208
+ # bucket.keys #=> # returns all keys from bucket
209
+ # bucket.keys('prefix' => 'logs') #=> # returns all keys that starts with 'logs'
210
+ #
211
+ def keys(options={}, head=false)
212
+ keys_and_service(options, head)[0]
213
+ end
214
+
215
+ # Same as +keys+ method but return an array of [keys, service_data].
216
+ # where +service_data+ is a hash with additional output information.
217
+ #
218
+ # keys, service = bucket.keys_and_service({'max-keys'=> 2, 'prefix' => 'logs'})
219
+ # p keys #=> # 2 keys array
220
+ # p service #=> {"max-keys"=>"2", "prefix"=>"logs", "name"=>"my_awesome_bucket", "marker"=>"", "is_truncated"=>true}
221
+ #
222
+ def keys_and_service(options={}, head=false)
223
+ opt = {}; options.each{ |key, value| opt[key.to_s] = value }
224
+ service_data = {}
225
+ thislist = {}
226
+ list = []
227
+ @s3.interface.incrementally_list_bucket(@name, opt) do |thislist|
228
+ thislist[:contents].each do |entry|
229
+ owner = Owner.new(entry[:owner_id], entry[:owner_display_name])
230
+ key = Key.new(self, entry[:key], nil, {}, {}, entry[:last_modified], entry[:e_tag], entry[:size], entry[:storage_class], owner)
231
+ key.head if head
232
+ list << key
233
+ end
234
+ end
235
+ thislist.each_key do |key|
236
+ service_data[key] = thislist[key] unless (key == :contents || key == :common_prefixes)
237
+ end
238
+ [list, service_data]
239
+ end
240
+
241
+ # Retrieve key information from Amazon.
242
+ # The +key_name+ is a +String+ or Key instance.
243
+ # Retrieves meta-header information if +head+ is +true+.
244
+ # Returns new Key instance.
245
+ #
246
+ # key = bucket.key('logs/today/1.log', true) #=> #<RightAws::S3::Key:0xb7b1e240 ... >
247
+ # # is the same as:
248
+ # key = RightAws::S3::Key.create(bucket, 'logs/today/1.log')
249
+ # key.head
250
+ #
251
+ def key(key_name, head=false)
252
+ raise 'Key name can not be empty.' if key_name.blank?
253
+ key_instance = nil
254
+ # if this key exists - find it ....
255
+ keys({'prefix'=>key_name}, head).each do |key|
256
+ if key.name == key_name.to_s
257
+ key_instance = key
258
+ break
259
+ end
260
+ end
261
+ # .... else this key is unknown
262
+ unless key_instance
263
+ key_instance = Key.create(self, key_name.to_s)
264
+ end
265
+ key_instance
266
+ end
267
+
268
+ # Store object data.
269
+ # The +key+ is a +String+ or Key instance.
270
+ # Returns +true+.
271
+ #
272
+ # bucket.put('logs/today/1.log', 'Olala!') #=> true
273
+ #
274
+ def put(key, data=nil, meta_headers={}, perms=nil, headers={})
275
+ key = Key.create(self, key.to_s, data, meta_headers) unless key.is_a?(Key)
276
+ key.put(data, perms, headers)
277
+ end
278
+
279
+ # Retrieve object data from Amazon.
280
+ # The +key+ is a +String+ or Key.
281
+ # Returns Key instance.
282
+ #
283
+ # key = bucket.get('logs/today/1.log') #=>
284
+ # puts key.data #=> 'sasfasfasdf'
285
+ #
286
+ def get(key, headers={})
287
+ key = Key.create(self, key.to_s) unless key.is_a?(Key)
288
+ key.get(headers)
289
+ end
290
+
291
+ # Rename object. Returns RightAws::S3::Key instance.
292
+ #
293
+ # new_key = bucket.rename_key('logs/today/1.log','logs/today/2.log') #=> #<RightAws::S3::Key:0xb7b1e240 ... >
294
+ # puts key.name #=> 'logs/today/2.log'
295
+ # key.exists? #=> true
296
+ #
297
+ def rename_key(old_key_or_name, new_name)
298
+ old_key_or_name = Key.create(self, old_key_or_name.to_s) unless old_key_or_name.is_a?(Key)
299
+ old_key_or_name.rename(new_name)
300
+ old_key_or_name
301
+ end
302
+
303
+ # Create an object copy. Returns a destination RightAws::S3::Key instance.
304
+ #
305
+ # new_key = bucket.copy_key('logs/today/1.log','logs/today/2.log') #=> #<RightAws::S3::Key:0xb7b1e240 ... >
306
+ # puts key.name #=> 'logs/today/2.log'
307
+ # key.exists? #=> true
308
+ #
309
+ def copy_key(old_key_or_name, new_key_or_name)
310
+ old_key_or_name = Key.create(self, old_key_or_name.to_s) unless old_key_or_name.is_a?(Key)
311
+ old_key_or_name.copy(new_key_or_name)
312
+ end
313
+
314
+ # Move an object to other location. Returns a destination RightAws::S3::Key instance.
315
+ #
316
+ # new_key = bucket.copy_key('logs/today/1.log','logs/today/2.log') #=> #<RightAws::S3::Key:0xb7b1e240 ... >
317
+ # puts key.name #=> 'logs/today/2.log'
318
+ # key.exists? #=> true
319
+ #
320
+ def move_key(old_key_or_name, new_key_or_name)
321
+ old_key_or_name = Key.create(self, old_key_or_name.to_s) unless old_key_or_name.is_a?(Key)
322
+ old_key_or_name.move(new_key_or_name)
323
+ end
324
+
325
+ # Remove all keys from a bucket.
326
+ # Returns +true+.
327
+ #
328
+ # bucket.clear #=> true
329
+ #
330
+ def clear
331
+ @s3.interface.clear_bucket(@name)
332
+ end
333
+
334
+ # Delete all keys where the 'folder_key' can be interpreted
335
+ # as a 'folder' name.
336
+ # Returns an array of string keys that have been deleted.
337
+ #
338
+ # bucket.keys.map{|key| key.name}.join(', ') #=> 'test, test/2/34, test/3, test1, test1/logs'
339
+ # bucket.delete_folder('test') #=> ['test','test/2/34','test/3']
340
+ #
341
+ def delete_folder(folder, separator='/')
342
+ @s3.interface.delete_folder(@name, folder, separator)
343
+ end
344
+
345
+ # Delete a bucket. Bucket must be empty.
346
+ # If +force+ is set, clears and deletes the bucket.
347
+ # Returns +true+.
348
+ #
349
+ # bucket.delete(true) #=> true
350
+ #
351
+ def delete(force=false)
352
+ force ? @s3.interface.force_delete_bucket(@name) : @s3.interface.delete_bucket(@name)
353
+ end
354
+
355
+ # Return a list of grantees.
356
+ #
357
+ def grantees
358
+ Grantee::grantees(self)
359
+ end
360
+
361
+ end
362
+
363
+
364
+ class Key
365
+ attr_reader :bucket, :name, :last_modified, :e_tag, :size, :storage_class, :owner
366
+ attr_accessor :headers, :meta_headers
367
+ attr_writer :data
368
+
369
+ # Separate Amazon meta headers from other headers
370
+ def self.split_meta(headers) #:nodoc:
371
+ hash = headers.dup
372
+ meta = {}
373
+ hash.each do |key, value|
374
+ if key[/^#{S3Interface::AMAZON_METADATA_PREFIX}/]
375
+ meta[key.gsub(S3Interface::AMAZON_METADATA_PREFIX,'')] = value
376
+ hash.delete(key)
377
+ end
378
+ end
379
+ [hash, meta]
380
+ end
381
+
382
+ def self.add_meta_prefix(meta_headers, prefix=S3Interface::AMAZON_METADATA_PREFIX)
383
+ meta = {}
384
+ meta_headers.each do |meta_header, value|
385
+ if meta_header[/#{prefix}/]
386
+ meta[meta_header] = value
387
+ else
388
+ meta["#{S3Interface::AMAZON_METADATA_PREFIX}#{meta_header}"] = value
389
+ end
390
+ end
391
+ meta
392
+ end
393
+
394
+
395
+ # Create a new Key instance, but do not create the actual key.
396
+ # The +name+ is a +String+.
397
+ # Returns a new Key instance.
398
+ #
399
+ # key = RightAws::S3::Key.create(bucket, 'logs/today/1.log') #=> #<RightAws::S3::Key:0xb7b1e240 ... >
400
+ # key.exists? #=> true | false
401
+ # key.put('Woohoo!') #=> true
402
+ # key.exists? #=> true
403
+ #
404
+ def self.create(bucket, name, data=nil, meta_headers={})
405
+ new(bucket, name, data, {}, meta_headers)
406
+ end
407
+
408
+ # Create a new Key instance, but do not create the actual key.
409
+ # In normal use this method should not be called directly.
410
+ # Use RightAws::S3::Key.create or bucket.key() instead.
411
+ #
412
+ def initialize(bucket, name, data=nil, headers={}, meta_headers={},
413
+ last_modified=nil, e_tag=nil, size=nil, storage_class=nil, owner=nil)
414
+ raise 'Bucket must be a Bucket instance.' unless bucket.is_a?(Bucket)
415
+ @bucket = bucket
416
+ @name = name
417
+ @data = data
418
+ @e_tag = e_tag
419
+ @size = size.to_i
420
+ @storage_class = storage_class
421
+ @owner = owner
422
+ @last_modified = last_modified
423
+ if @last_modified && !@last_modified.is_a?(Time)
424
+ @last_modified = Time.parse(@last_modified)
425
+ end
426
+ @headers, @meta_headers = self.class.split_meta(headers)
427
+ @meta_headers.merge!(meta_headers)
428
+ end
429
+
430
+ # Return key name as a String.
431
+ #
432
+ # key = RightAws::S3::Key.create(bucket, 'logs/today/1.log') #=> #<RightAws::S3::Key:0xb7b1e240 ... >
433
+ # puts key #=> 'logs/today/1.log'
434
+ #
435
+ def to_s
436
+ @name.to_s
437
+ end
438
+
439
+ # Return the full S3 path to this key (bucket/key).
440
+ #
441
+ # key.full_name #=> 'my_awesome_bucket/cool_key'
442
+ #
443
+ def full_name(separator='/')
444
+ "#{@bucket.to_s}#{separator}#{@name}"
445
+ end
446
+
447
+ # Return a public link to a key.
448
+ #
449
+ # key.public_link #=> 'https://s3.amazonaws.com:443/my_awesome_bucket/cool_key'
450
+ #
451
+ def public_link
452
+ params = @bucket.s3.interface.params
453
+ "#{params[:protocol]}://#{params[:server]}:#{params[:port]}/#{full_name('/')}"
454
+ end
455
+
456
+ # Return Key data. Retrieve this data from Amazon if it is the first time call.
457
+ # TODO TRB 6/19/07 What does the above mean? Clarify.
458
+ #
459
+ def data
460
+ get if !@data and exists?
461
+ @data
462
+ end
463
+
464
+ # Retrieve object data and attributes from Amazon.
465
+ # Returns a +String+.
466
+ #
467
+ def get(headers={})
468
+ response = @bucket.s3.interface.get(@bucket.name, @name, headers)
469
+ @data = response[:object]
470
+ @headers, @meta_headers = self.class.split_meta(response[:headers])
471
+ refresh(false)
472
+ @data
473
+ end
474
+
475
+ # Store object data on S3.
476
+ # Parameter +data+ is a +String+ or S3Object instance.
477
+ # Returns +true+.
478
+ #
479
+ # key = RightAws::S3::Key.create(bucket, 'logs/today/1.log')
480
+ # key.data = 'Qwerty'
481
+ # key.put #=> true
482
+ # ...
483
+ # key.put('Olala!') #=> true
484
+ #
485
+ def put(data=nil, perms=nil, headers={})
486
+ headers['x-amz-acl'] = perms if perms
487
+ @data = data || @data
488
+ meta = self.class.add_meta_prefix(@meta_headers)
489
+ @bucket.s3.interface.put(@bucket.name, @name, @data, meta.merge(headers))
490
+ end
491
+
492
+ # Rename an object. Returns new object name.
493
+ #
494
+ # key = RightAws::S3::Key.create(bucket, 'logs/today/1.log') #=> #<RightAws::S3::Key:0xb7b1e240 ... >
495
+ # key.rename('logs/today/2.log') #=> 'logs/today/2.log'
496
+ # puts key.name #=> 'logs/today/2.log'
497
+ # key.exists? #=> true
498
+ #
499
+ def rename(new_name)
500
+ @bucket.s3.interface.rename(@bucket.name, @name, new_name)
501
+ @name = new_name
502
+ end
503
+
504
+ # Create an object copy. Returns a destination RightAws::S3::Key instance.
505
+ #
506
+ # # Key instance as destination
507
+ # key1 = RightAws::S3::Key.create(bucket, 'logs/today/1.log') #=> #<RightAws::S3::Key:0xb7b1e240 ... >
508
+ # key2 = RightAws::S3::Key.create(bucket, 'logs/today/2.log') #=> #<RightAws::S3::Key:0xb7b5e240 ... >
509
+ # key1.put('Olala!') #=> true
510
+ # key1.copy(key2) #=> #<RightAws::S3::Key:0xb7b5e240 ... >
511
+ # key1.exists? #=> true
512
+ # key2.exists? #=> true
513
+ # puts key2.data #=> 'Olala!'
514
+ #
515
+ # # String as destination
516
+ # key = RightAws::S3::Key.create(bucket, 'logs/today/777.log') #=> #<RightAws::S3::Key:0xb7b1e240 ... >
517
+ # key.put('Olala!') #=> true
518
+ # new_key = key.copy('logs/today/888.log') #=> #<RightAws::S3::Key:0xb7b5e240 ... >
519
+ # key.exists? #=> true
520
+ # new_key.exists? #=> true
521
+ #
522
+ def copy(new_key_or_name)
523
+ new_key_or_name = Key.create(@bucket, new_key_or_name.to_s) unless new_key_or_name.is_a?(Key)
524
+ @bucket.s3.interface.copy(@bucket.name, @name, new_key_or_name.bucket.name, new_key_or_name.name)
525
+ new_key_or_name
526
+ end
527
+
528
+ # Move an object to other location. Returns a destination RightAws::S3::Key instance.
529
+ #
530
+ # # Key instance as destination
531
+ # key1 = RightAws::S3::Key.create(bucket, 'logs/today/1.log') #=> #<RightAws::S3::Key:0xb7b1e240 ... >
532
+ # key2 = RightAws::S3::Key.create(bucket, 'logs/today/2.log') #=> #<RightAws::S3::Key:0xb7b5e240 ... >
533
+ # key1.put('Olala!') #=> true
534
+ # key1.move(key2) #=> #<RightAws::S3::Key:0xb7b5e240 ... >
535
+ # key1.exists? #=> false
536
+ # key2.exists? #=> true
537
+ # puts key2.data #=> 'Olala!'
538
+ #
539
+ # # String as destination
540
+ # key = RightAws::S3::Key.create(bucket, 'logs/today/777.log') #=> #<RightAws::S3::Key:0xb7b1e240 ... >
541
+ # key.put('Olala!') #=> true
542
+ # new_key = key.move('logs/today/888.log') #=> #<RightAws::S3::Key:0xb7b5e240 ... >
543
+ # key.exists? #=> false
544
+ # new_key.exists? #=> true
545
+ #
546
+ def move(new_key_or_name)
547
+ new_key_or_name = Key.create(@bucket, new_key_or_name.to_s) unless new_key_or_name.is_a?(Key)
548
+ @bucket.s3.interface.move(@bucket.name, @name, new_key_or_name.bucket.name, new_key_or_name.name)
549
+ new_key_or_name
550
+ end
551
+
552
+ # Retrieve key info from bucket and update attributes.
553
+ # Refresh meta-headers (by calling +head+ method) if +head+ is set.
554
+ # Returns +true+ if the key exists in bucket and +false+ otherwise.
555
+ #
556
+ # key = RightAws::S3::Key.create(bucket, 'logs/today/1.log')
557
+ # key.e_tag #=> nil
558
+ # key.meta_headers #=> {}
559
+ # key.refresh #=> true
560
+ # key.e_tag #=> '12345678901234567890bf11094484b6'
561
+ # key.meta_headers #=> {"family"=>"qwerty", "name"=>"asdfg"}
562
+ #
563
+ def refresh(head=true)
564
+ new_key = @bucket.key(self)
565
+ @last_modified = new_key.last_modified
566
+ @e_tag = new_key.e_tag
567
+ @size = new_key.size
568
+ @storage_class = new_key.storage_class
569
+ @owner = new_key.owner
570
+ if @last_modified
571
+ self.head
572
+ true
573
+ else
574
+ @headers = @meta_headers = {}
575
+ false
576
+ end
577
+ end
578
+
579
+ # Updates headers and meta-headers from S3.
580
+ # Returns +true+.
581
+ #
582
+ # key.meta_headers #=> {"family"=>"qwerty"}
583
+ # key.head #=> true
584
+ # key.meta_headers #=> {"family"=>"qwerty", "name"=>"asdfg"}
585
+ #
586
+ def head
587
+ @headers, @meta_headers = self.class.split_meta(@bucket.s3.interface.head(@bucket, @name))
588
+ true
589
+ end
590
+
591
+ # Reload meta-headers only. Returns meta-headers hash.
592
+ #
593
+ # key.reload_meta #=> {"family"=>"qwerty", "name"=>"asdfg"}
594
+ #
595
+ def reload_meta
596
+ @meta_headers = self.class.split_meta(@bucket.s3.interface.head(@bucket, @name)).last
597
+ end
598
+
599
+ # Replace meta-headers by new hash at S3. Returns new meta-headers hash.
600
+ #
601
+ # key.reload_meta #=> {"family"=>"qwerty", "name"=>"asdfg"}
602
+ # key.save_meta #=> {"family"=>"oops", "race" => "troll"}
603
+ # key.reload_meta #=> {"family"=>"oops", "race" => "troll"}
604
+ #
605
+ def save_meta(meta_headers)
606
+ meta = self.class.add_meta_prefix(meta_headers)
607
+ @bucket.s3.interface.copy(@bucket.name, @name, @bucket.name, @name, :replace, meta)
608
+ @meta_headers = self.class.split_meta(meta)[1]
609
+ end
610
+
611
+ # Check for existence of the key in the given bucket.
612
+ # Returns +true+ or +false+.
613
+ #
614
+ # key = RightAws::S3::Key.create(bucket,'logs/today/1.log')
615
+ # key.exists? #=> false
616
+ # key.put('Woohoo!') #=> true
617
+ # key.exists? #=> true
618
+ #
619
+ def exists?
620
+ @bucket.key(self).last_modified ? true : false
621
+ end
622
+
623
+ # Remove key from bucket.
624
+ # Returns +true+.
625
+ #
626
+ # key.delete #=> true
627
+ #
628
+ def delete
629
+ raise 'Key name must be specified.' if @name.blank?
630
+ @bucket.s3.interface.delete(@bucket, @name)
631
+ end
632
+
633
+ # Return a list of grantees.
634
+ #
635
+ def grantees
636
+ Grantee::grantees(self)
637
+ end
638
+
639
+ end
640
+
641
+
642
+ class Owner
643
+ attr_reader :id, :name
644
+
645
+ def initialize(id, name)
646
+ @id = id
647
+ @name = name
648
+ end
649
+
650
+ # Return Owner name as a +String+.
651
+ def to_s
652
+ @name
653
+ end
654
+ end
655
+
656
+
657
+ # There are 2 ways to set permissions for a bucket or key (called a +thing+ below):
658
+ #
659
+ # 1 . Use +perms+ param to set 'Canned Access Policies' when calling the <tt>bucket.create</tt>,
660
+ # <tt>bucket.put</tt> and <tt>key.put</tt> methods.
661
+ # The +perms+ param can take these values: 'private', 'public-read', 'public-read-write' and
662
+ # 'authenticated-read'.
663
+ # (see http://docs.amazonwebservices.com/AmazonS3/2006-03-01/RESTAccessPolicy.html).
664
+ #
665
+ # bucket = s3.bucket('bucket_for_kd_test_13', true, 'public-read')
666
+ # key.put('Woohoo!','public-read-write' )
667
+ #
668
+ # 2 . Use Grantee instances (the permission is a +String+ or an +Array+ of: 'READ', 'WRITE',
669
+ # 'READ_ACP', 'WRITE_ACP', 'FULL_CONTROL'):
670
+ #
671
+ # bucket = s3.bucket('my_awesome_bucket', true)
672
+ # grantee1 = RightAws::S3::Grantee.new(bucket, 'a123b...223c', FULL_CONTROL, :apply)
673
+ # grantee2 = RightAws::S3::Grantee.new(bucket, 'xy3v3...5fhp', [READ, WRITE], :apply)
674
+ #
675
+ # There is only one way to get and to remove permission (via Grantee instances):
676
+ #
677
+ # grantees = bucket.grantees # a list of Grantees that have any access for this bucket
678
+ # grantee1 = RightAws::S3::Grantee.new(bucket, 'a123b...223c')
679
+ # grantee1.perms #=> returns a list of perms for this grantee to that bucket
680
+ # ...
681
+ # grantee1.drop # remove all perms for this grantee
682
+ # grantee2.revoke('WRITE') # revoke write access only
683
+ #
684
+ class Grantee
685
+ # A bucket or a key the grantee has an access to.
686
+ attr_reader :thing
687
+ # Grantee Amazon id.
688
+ attr_reader :id
689
+ # Grantee display name.
690
+ attr_reader :name
691
+ # Array of permissions.
692
+ attr_accessor :perms
693
+
694
+ # Retrieve Owner information and a list of Grantee instances that have
695
+ # a access to this thing (bucket or key).
696
+ #
697
+ # bucket = s3.bucket('my_awesome_bucket', true, 'public-read')
698
+ # ...
699
+ # RightAws::S3::Grantee.owner_and_grantees(bucket) #=> [owner, grantees]
700
+ #
701
+ def self.owner_and_grantees(thing)
702
+ if thing.is_a?(Bucket)
703
+ bucket, key = thing, ''
704
+ else
705
+ bucket, key = thing.bucket, thing
706
+ end
707
+ hash = bucket.s3.interface.get_acl_parse(bucket.to_s, key.to_s)
708
+ owner = Owner.new(hash[:owner][:id], hash[:owner][:display_name])
709
+
710
+ grantees = []
711
+ hash[:grantees].each do |id, params|
712
+ grantees << new(thing, id, params[:permissions], nil, params[:display_name])
713
+ end
714
+ [owner, grantees]
715
+ end
716
+
717
+ # Retrieves a list of Grantees instances that have an access to this thing(bucket or key).
718
+ #
719
+ # bucket = s3.bucket('my_awesome_bucket', true, 'public-read')
720
+ # ...
721
+ # RightAws::S3::Grantee.grantees(bucket) #=> grantees
722
+ #
723
+ def self.grantees(thing)
724
+ owner_and_grantees(thing)[1]
725
+ end
726
+
727
+ def self.put_acl(thing, owner, grantees) #:nodoc:
728
+ if thing.is_a?(Bucket)
729
+ bucket, key = thing, ''
730
+ else
731
+ bucket, key = thing.bucket, thing
732
+ end
733
+ body = "<AccessControlPolicy>" +
734
+ "<Owner>" +
735
+ "<ID>#{owner.id}</ID>" +
736
+ "<DisplayName>#{owner.name}</DisplayName>" +
737
+ "</Owner>" +
738
+ "<AccessControlList>" +
739
+ grantees.map{|grantee| grantee.to_xml}.join +
740
+ "</AccessControlList>" +
741
+ "</AccessControlPolicy>"
742
+ bucket.s3.interface.put_acl(bucket.to_s, key.to_s, body)
743
+ end
744
+
745
+ # Create a new Grantee instance.
746
+ # Grantee +id+ must exist on S3. If +action+ == :refresh, then retrieve
747
+ # permissions from S3 and update @perms. If +action+ == :apply, then apply
748
+ # perms to +thing+ at S3. If +action+ == :apply_and_refresh then it performs.
749
+ # both the actions. This is used for the new grantees that had no perms to
750
+ # this thing before. The default action is :refresh.
751
+ #
752
+ # bucket = s3.bucket('my_awesome_bucket', true, 'public-read')
753
+ # grantee1 = RightAws::S3::Grantee.new(bucket, 'a123b...223c', FULL_CONTROL)
754
+ # ...
755
+ # grantee2 = RightAws::S3::Grantee.new(bucket, 'abcde...asdf', [FULL_CONTROL, READ], :apply)
756
+ # grantee3 = RightAws::S3::Grantee.new(bucket, 'aaaaa...aaaa', 'READ', :apply_and_refresh)
757
+ #
758
+ def initialize(thing, id, perms=[], action=:refresh, name=nil)
759
+ @thing = thing
760
+ @id = id
761
+ @name = name
762
+ @perms = perms.to_a
763
+ case action
764
+ when :apply then apply
765
+ when :refresh then refresh
766
+ when :apply_and_refresh then apply; refresh
767
+ end
768
+ end
769
+
770
+ # Return +true+ if the grantee has any permissions to the thing.
771
+ def exists?
772
+ self.class.grantees(@thing).each do |grantee|
773
+ return true if @id == grantee.id
774
+ end
775
+ false
776
+ end
777
+
778
+ # Return Grantee type (+String+): "Group", "AmazonCustomerByEmail" or "CanonicalUser".
779
+ def type
780
+ case @id
781
+ when /^http:/ then "Group"
782
+ when /@/ then "AmazonCustomerByEmail"
783
+ else "CanonicalUser"
784
+ end
785
+ end
786
+
787
+ # Return a name or an id.
788
+ def to_s
789
+ @name || @id
790
+ end
791
+
792
+ # Add permissions for grantee.
793
+ # Permissions: 'READ', 'WRITE', 'READ_ACP', 'WRITE_ACP', 'FULL_CONTROL'.
794
+ # See http://docs.amazonwebservices.com/AmazonS3/2006-03-01/UsingPermissions.html .
795
+ # Returns +true+.
796
+ #
797
+ # grantee.grant('FULL_CONTROL') #=> true
798
+ # grantee.grant('FULL_CONTROL','WRITE','READ') #=> true
799
+ # grantee.grant(['WRITE_ACP','READ','READ_ACP']) #=> true
800
+ #
801
+ def grant(*permissions)
802
+ permissions.flatten!
803
+ old_perms = @perms.dup
804
+ @perms += permissions
805
+ @perms.uniq!
806
+ return true if @perms == old_perms
807
+ apply
808
+ end
809
+
810
+ # Revoke permissions for grantee.
811
+ # Permissions: 'READ', 'WRITE', 'READ_ACP', 'WRITE_ACP', 'FULL_CONTROL'
812
+ # See http://docs.amazonwebservices.com/AmazonS3/2006-03-01/UsingPermissions.html .
813
+ # Default value is 'FULL_CONTROL'.
814
+ # Returns +true+.
815
+ #
816
+ # grantee.revoke('READ') #=> true
817
+ # grantee.revoke('FULL_CONTROL','WRITE') #=> true
818
+ # grantee.revoke(['READ_ACP','WRITE_ACP']) #=> true
819
+ #
820
+ def revoke(*permissions)
821
+ permissions.flatten!
822
+ old_perms = @perms.dup
823
+ @perms -= permissions
824
+ @perms.uniq!
825
+ return true if @perms == old_perms
826
+ apply
827
+ end
828
+
829
+ # Revoke all permissions for this grantee.
830
+ # Returns +true+.
831
+ #
832
+ # grantee.drop #=> true
833
+ #
834
+ def drop
835
+ @perms = []
836
+ apply
837
+ end
838
+
839
+ # Refresh grantee perms for its +thing+.
840
+ # Returns +true+ if the grantee has perms for this +thing+ or
841
+ # +false+ otherwise, and updates @perms value as a side-effect.
842
+ #
843
+ # grantee.grant('FULL_CONTROL') #=> true
844
+ # grantee.refresh #=> true
845
+ # grantee.drop #=> true
846
+ # grantee.refresh #=> false
847
+ #
848
+ def refresh
849
+ @perms = []
850
+ self.class.grantees(@thing).each do |grantee|
851
+ if @id == grantee.id
852
+ @name = grantee.name
853
+ @perms = grantee.perms
854
+ return true
855
+ end
856
+ end
857
+ false
858
+ end
859
+
860
+ # Apply current grantee @perms to +thing+. This method is called internally by the +grant+
861
+ # and +revoke+ methods. In normal use this method should not
862
+ # be called directly.
863
+ #
864
+ # grantee.perms = ['FULL_CONTROL']
865
+ # grantee.apply #=> true
866
+ #
867
+ def apply
868
+ @perms.uniq!
869
+ owner, grantees = self.class.owner_and_grantees(@thing)
870
+ # walk through all the grantees and replace the data for the current one and ...
871
+ grantees.map! { |grantee| grantee.id == @id ? self : grantee }
872
+ # ... if this grantee is not known - add this bad boy to a list
873
+ grantees << self unless grantees.include?(self)
874
+ # set permissions
875
+ self.class.put_acl(@thing, owner, grantees)
876
+ end
877
+
878
+ def to_xml # :nodoc:
879
+ id_str = case @id
880
+ when /^http/ then "<URI>#{@id}</URI>"
881
+ when /@/ then "<EmailAddress>#{@id}</EmailAddress>"
882
+ else "<ID>#{@id}</ID>"
883
+ end
884
+ grants = ''
885
+ @perms.each do |perm|
886
+ grants << "<Grant>" +
887
+ "<Grantee xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " +
888
+ "xsi:type=\"#{type}\">#{id_str}</Grantee>" +
889
+ "<Permission>#{perm}</Permission>" +
890
+ "</Grant>"
891
+ end
892
+ grants
893
+ end
894
+
895
+ end
896
+
897
+ end
898
+
899
+ # RightAws::S3Generator and RightAws::S3Generator::Bucket methods:
900
+ #
901
+ # s3g = RightAws::S3Generator.new('1...2', 'nx...Y6') #=> #<RightAws::S3Generator:0xb7b5cc94>
902
+ #
903
+ # # List all buckets(method 'GET'):
904
+ # buckets_list = s3g.buckets #=> 'https://s3.amazonaws.com:443/?Signature=Y...D&Expires=1180941864&AWSAccessKeyId=1...2'
905
+ # # Create bucket link (method 'PUT'):
906
+ # bucket = s3g.bucket('my_awesome_bucket') #=> #<RightAws::S3Generator::Bucket:0xb7bcbda8>
907
+ # link_to_create = bucket.create_link(1.hour) #=> https://s3.amazonaws.com:443/my_awesome_bucket?Signature=4...D&Expires=1180942132&AWSAccessKeyId=1...2
908
+ # # ... or:
909
+ # bucket = RightAws::S3Generator::Bucket.create(s3g, 'my_awesome_bucket') #=> #<RightAws::S3Generator::Bucket:0xb7bcbda8>
910
+ # link_to_create = bucket.create_link(1.hour) #=> https://s3.amazonaws.com:443/my_awesome_bucket?Signature=4...D&Expires=1180942132&AWSAccessKeyId=1...2
911
+ # # ... or:
912
+ # bucket = RightAws::S3Generator::Bucket.new(s3g, 'my_awesome_bucket') #=> #<RightAws::S3Generator::Bucket:0xb7bcbda8>
913
+ # link_to_create = bucket.create_link(1.hour) #=> https://s3.amazonaws.com:443/my_awesome_bucket?Signature=4...D&Expires=1180942132&AWSAccessKeyId=1...2
914
+ # # List bucket(method 'GET'):
915
+ # bucket.keys(1.day) #=> https://s3.amazonaws.com:443/my_awesome_bucket?Signature=i...D&Expires=1180942620&AWSAccessKeyId=1...2
916
+ # # Create/put key (method 'PUT'):
917
+ # bucket.put('my_cool_key') #=> https://s3.amazonaws.com:443/my_awesome_bucket/my_cool_key?Signature=q...D&Expires=1180943094&AWSAccessKeyId=1...2
918
+ # # Get key data (method 'GET'):
919
+ # bucket.get('logs/today/1.log', 1.hour) #=> https://s3.amazonaws.com:443/my_awesome_bucket/my_cool_key?Signature=h...M%3D&Expires=1180820032&AWSAccessKeyId=1...2
920
+ # # Delete bucket (method 'DELETE'):
921
+ # bucket.delete(2.hour) #=> https://s3.amazonaws.com:443/my_awesome_bucket/logs%2Ftoday%2F1.log?Signature=4...D&Expires=1180820032&AWSAccessKeyId=1...2
922
+ #
923
+ # RightAws::S3Generator::Key methods:
924
+ #
925
+ # # Create Key instance:
926
+ # key = RightAws::S3Generator::Key.new(bicket, 'my_cool_key') #=> #<RightAws::S3Generator::Key:0xb7b7394c>
927
+ # # Put key data (method 'PUT'):
928
+ # key.put #=> https://s3.amazonaws.com:443/my_awesome_bucket/my_cool_key?Signature=2...D&Expires=1180943302&AWSAccessKeyId=1...2
929
+ # # Get key data (method 'GET'):
930
+ # key.get #=> https://s3.amazonaws.com:443/my_awesome_bucket/my_cool_key?Signature=a...D&Expires=1180820032&AWSAccessKeyId=1...2
931
+ # # Head key (method 'HEAD'):
932
+ # key.head #=> https://s3.amazonaws.com:443/my_awesome_bucket/my_cool_key?Signature=b...D&Expires=1180820032&AWSAccessKeyId=1...2
933
+ # # Delete key (method 'DELETE'):
934
+ # key.delete #=> https://s3.amazonaws.com:443/my_awesome_bucket/my_cool_key?Signature=x...D&Expires=1180820032&AWSAccessKeyId=1...2
935
+ #
936
+ class S3Generator
937
+ attr_reader :interface
938
+
939
+ def initialize(aws_access_key_id, aws_secret_access_key, params={})
940
+ @interface = S3Interface.new(aws_access_key_id, aws_secret_access_key, params)
941
+ end
942
+
943
+ # Generate link to list all buckets
944
+ #
945
+ # s3.buckets(1.hour)
946
+ #
947
+ def buckets(expires=nil, headers={})
948
+ @interface.list_all_my_buckets_link(expires, headers)
949
+ end
950
+
951
+ # Create new S3LinkBucket instance and generate link to create it at S3.
952
+ #
953
+ # bucket= s3.bucket('my_owesome_bucket')
954
+ #
955
+ def bucket(name, expires=nil, headers={})
956
+ Bucket.create(self, name.to_s)
957
+ end
958
+
959
+ class Bucket
960
+ attr_reader :s3, :name
961
+
962
+ def to_s
963
+ @name
964
+ end
965
+ alias_method :full_name, :to_s
966
+
967
+ # Return a public link to bucket.
968
+ #
969
+ # bucket.public_link #=> 'https://s3.amazonaws.com:443/my_awesome_bucket'
970
+ #
971
+ def public_link
972
+ params = @s3.interface.params
973
+ "#{params[:protocol]}://#{params[:server]}:#{params[:port]}/#{full_name}"
974
+ end
975
+
976
+ # Create new S3LinkBucket instance and generate creation link for it.
977
+ def self.create(s3, name, expires=nil, headers={})
978
+ new(s3, name.to_s)
979
+ end
980
+
981
+ # Create new S3LinkBucket instance.
982
+ def initialize(s3, name)
983
+ @s3, @name = s3, name.to_s
984
+ end
985
+
986
+ # Return a link to create this bucket.
987
+ #
988
+ def create_link(expires=nil, headers={})
989
+ @s3.interface.create_bucket_link(@name, expires, headers)
990
+ end
991
+
992
+ # Generate link to list keys.
993
+ #
994
+ # bucket.keys
995
+ # bucket.keys('prefix'=>'logs')
996
+ #
997
+ def keys(options=nil, expires=nil, headers={})
998
+ @s3.interface.list_bucket_link(@name, options, expires, headers)
999
+ end
1000
+
1001
+ # Return a S3Generator::Key instance.
1002
+ #
1003
+ # bucket.key('my_cool_key').get #=> https://s3.amazonaws.com:443/my_awesome_bucket/my_cool_key?Signature=B...D&Expires=1180820032&AWSAccessKeyId=1...2
1004
+ # bucket.key('my_cool_key').delete #=> https://s3.amazonaws.com:443/my_awesome_bucket/my_cool_key?Signature=B...D&Expires=1180820098&AWSAccessKeyId=1...2
1005
+ #
1006
+ def key(name)
1007
+ Key.new(self, name)
1008
+ end
1009
+
1010
+ # Generates link to PUT key data.
1011
+ #
1012
+ # puts bucket.put('logs/today/1.log', 2.hour)
1013
+ #
1014
+ def put(key, meta_headers={}, expires=nil, headers={})
1015
+ meta = RightAws::S3::Key.add_meta_prefix(meta_headers)
1016
+ @s3.interface.put_link(@name, key.to_s, nil, expires, meta.merge(headers))
1017
+ end
1018
+
1019
+ # Generate link to GET key data.
1020
+ #
1021
+ # bucket.get('logs/today/1.log', 1.hour)
1022
+ #
1023
+ def get(key, expires=nil, headers={})
1024
+ @s3.interface.get_link(@name, key.to_s, expires, headers)
1025
+ end
1026
+
1027
+ # Generate link to delete bucket.
1028
+ #
1029
+ # bucket.delete(2.hour)
1030
+ #
1031
+ def delete(expires=nil, headers={})
1032
+ @s3.interface.delete_bucket_link(@name, expires, headers)
1033
+ end
1034
+ end
1035
+
1036
+
1037
+ class Key
1038
+ attr_reader :bucket, :name
1039
+
1040
+ def to_s
1041
+ @name
1042
+ end
1043
+
1044
+ # Return a full S# name (bucket/key).
1045
+ #
1046
+ # key.full_name #=> 'my_awesome_bucket/cool_key'
1047
+ #
1048
+ def full_name(separator='/')
1049
+ "#{@bucket.to_s}#{separator}#{@name}"
1050
+ end
1051
+
1052
+ # Return a public link to key.
1053
+ #
1054
+ # key.public_link #=> 'https://s3.amazonaws.com:443/my_awesome_bucket/cool_key'
1055
+ #
1056
+ def public_link
1057
+ params = @bucket.s3.interface.params
1058
+ "#{params[:protocol]}://#{params[:server]}:#{params[:port]}/#{full_name('/')}"
1059
+ end
1060
+
1061
+ def initialize(bucket, name, meta_headers={})
1062
+ @bucket = bucket
1063
+ @name = name.to_s
1064
+ @meta_headers = meta_headers
1065
+ raise 'Key name can not be empty.' if @name.blank?
1066
+ end
1067
+
1068
+ # Generate link to PUT key data.
1069
+ #
1070
+ # puts bucket.put('logs/today/1.log', '123', 2.hour) #=> https://s3.amazonaws.com:443/my_awesome_bucket/logs%2Ftoday%2F1.log?Signature=B...D&Expires=1180820032&AWSAccessKeyId=1...2
1071
+ #
1072
+ def put(expires=nil, headers={})
1073
+ @bucket.put(@name.to_s, @meta_headers, expires, headers)
1074
+ end
1075
+
1076
+ # Generate link to GET key data.
1077
+ #
1078
+ # bucket.get('logs/today/1.log', 1.hour) #=> https://s3.amazonaws.com:443/my_awesome_bucket/logs%2Ftoday%2F1.log?Signature=h...M%3D&Expires=1180820032&AWSAccessKeyId=1...2
1079
+ #
1080
+ def get(expires=nil, headers={})
1081
+ @bucket.s3.interface.get_link(@bucket.to_s, @name, expires, headers)
1082
+ end
1083
+
1084
+ # Generate link to delete key.
1085
+ #
1086
+ # bucket.delete(2.hour) #=> https://s3.amazonaws.com:443/my_awesome_bucket/logs%2Ftoday%2F1.log?Signature=4...D&Expires=1180820032&AWSAccessKeyId=1...2
1087
+ #
1088
+ def delete(expires=nil, headers={})
1089
+ @bucket.s3.interface.delete_link(@bucket.to_s, @name, expires, headers)
1090
+ end
1091
+
1092
+ # Generate link to head key.
1093
+ #
1094
+ # bucket.head(2.hour) #=> https://s3.amazonaws.com:443/my_awesome_bucket/logs%2Ftoday%2F1.log?Signature=4...D&Expires=1180820032&AWSAccessKeyId=1...2
1095
+ #
1096
+ def head(expires=nil, headers={})
1097
+ @bucket.s3.interface.head_link(@bucket.to_s, @name, expires, headers)
1098
+ end
1099
+ end
1100
+ end
1101
+
1102
+ end