redaranj-right_aws 1.10.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,1185 @@
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
+ class S3Interface < RightAwsBase
27
+
28
+ USE_100_CONTINUE_PUT_SIZE = 1_000_000
29
+
30
+ include RightAwsBaseInterface
31
+
32
+ DEFAULT_HOST = 's3.amazonaws.com'
33
+ DEFAULT_PORT = 443
34
+ DEFAULT_PROTOCOL = 'https'
35
+ DEFAULT_SERVICE = '/'
36
+ REQUEST_TTL = 30
37
+ DEFAULT_EXPIRES_AFTER = 1 * 24 * 60 * 60 # One day's worth of seconds
38
+ ONE_YEAR_IN_SECONDS = 365 * 24 * 60 * 60
39
+ AMAZON_HEADER_PREFIX = 'x-amz-'
40
+ AMAZON_METADATA_PREFIX = 'x-amz-meta-'
41
+
42
+ @@bench = AwsBenchmarkingBlock.new
43
+ def self.bench_xml
44
+ @@bench.xml
45
+ end
46
+ def self.bench_s3
47
+ @@bench.service
48
+ end
49
+
50
+ # Params supported:
51
+ # :no_subdomains => true # do not use bucket as a part of domain name but as a part of path
52
+ @@params = {}
53
+ def self.params
54
+ @@params
55
+ end
56
+
57
+ # get custom option
58
+ def param(name)
59
+ # - check explicitly defined param (@params)
60
+ # - otherwise check implicitly defined one (@@params)
61
+ @params.has_key?(name) ? @params[name] : @@params[name]
62
+ end
63
+
64
+ # Creates new RightS3 instance.
65
+ #
66
+ # s3 = RightAws::S3Interface.new('1E3GDYEOGFJPIT7XXXXXX','hgTHt68JY07JKUY08ftHYtERkjgtfERn57XXXXXX', {:multi_thread => true, :logger => Logger.new('/tmp/x.log')}) #=> #<RightAws::S3Interface:0xb7b3c27c>
67
+ #
68
+ # Params is a hash:
69
+ #
70
+ # {:server => 's3.amazonaws.com' # Amazon service host: 's3.amazonaws.com'(default)
71
+ # :port => 443 # Amazon service port: 80 or 443(default)
72
+ # :protocol => 'https' # Amazon service protocol: 'http' or 'https'(default)
73
+ # :multi_thread => true|false # Multi-threaded (connection per each thread): true or false(default)
74
+ # :logger => Logger Object} # Logger instance: logs to STDOUT if omitted }
75
+ #
76
+ def initialize(aws_access_key_id=nil, aws_secret_access_key=nil, params={})
77
+ init({ :name => 'S3',
78
+ :default_host => ENV['S3_URL'] ? URI.parse(ENV['S3_URL']).host : DEFAULT_HOST,
79
+ :default_port => ENV['S3_URL'] ? URI.parse(ENV['S3_URL']).port : DEFAULT_PORT,
80
+ :default_service => ENV['S3_URL'] ? URI.parse(ENV['S3_URL']).path : DEFAULT_SERVICE,
81
+ :default_protocol => ENV['S3_URL'] ? URI.parse(ENV['S3_URL']).scheme : DEFAULT_PROTOCOL },
82
+ aws_access_key_id || ENV['AWS_ACCESS_KEY_ID'],
83
+ aws_secret_access_key || ENV['AWS_SECRET_ACCESS_KEY'],
84
+ params)
85
+ end
86
+
87
+
88
+ #-----------------------------------------------------------------
89
+ # Requests
90
+ #-----------------------------------------------------------------
91
+ # Produces canonical string for signing.
92
+ def canonical_string(method, path, headers={}, expires=nil) # :nodoc:
93
+ s3_headers = {}
94
+ headers.each do |key, value|
95
+ key = key.downcase
96
+ s3_headers[key] = value.to_s.strip if key[/^#{AMAZON_HEADER_PREFIX}|^content-md5$|^content-type$|^date$/o]
97
+ end
98
+ s3_headers['content-type'] ||= ''
99
+ s3_headers['content-md5'] ||= ''
100
+ s3_headers['date'] = '' if s3_headers.has_key? 'x-amz-date'
101
+ s3_headers['date'] = expires if expires
102
+ # prepare output string
103
+ out_string = "#{method}\n"
104
+ s3_headers.sort { |a, b| a[0] <=> b[0] }.each do |key, value|
105
+ out_string << (key[/^#{AMAZON_HEADER_PREFIX}/o] ? "#{key}:#{value}\n" : "#{value}\n")
106
+ end
107
+ # ignore everything after the question mark...
108
+ out_string << path.gsub(/\?.*$/, '')
109
+ # ...unless there is an acl or torrent parameter
110
+ out_string << '?acl' if path[/[&?]acl($|&|=)/]
111
+ out_string << '?torrent' if path[/[&?]torrent($|&|=)/]
112
+ out_string << '?location' if path[/[&?]location($|&|=)/]
113
+ out_string << '?logging' if path[/[&?]logging($|&|=)/] # this one is beta, no support for now
114
+ out_string
115
+ end
116
+
117
+ # http://docs.amazonwebservices.com/AmazonS3/2006-03-01/index.html?BucketRestrictions.html
118
+ def is_dns_bucket?(bucket_name)
119
+ bucket_name = bucket_name.to_s
120
+ return nil unless (3..63) === bucket_name.size
121
+ bucket_name.split('.').each do |component|
122
+ return nil unless component[/^[a-z0-9]([a-z0-9-]*[a-z0-9])?$/]
123
+ end
124
+ true
125
+ end
126
+
127
+ def fetch_request_params(headers) #:nodoc:
128
+ # default server to use
129
+ server = @params[:server]
130
+ service = @params[:service].to_s
131
+ service.chop! if service[%r{/$}] # remove trailing '/' from service
132
+ # extract bucket name and check it's dns compartibility
133
+ headers[:url].to_s[%r{^([a-z0-9._-]*)(/[^?]*)?(\?.+)?}i]
134
+ bucket_name, key_path, params_list = $1, $2, $3
135
+ # select request model
136
+ if !param(:no_subdomains) && is_dns_bucket?(bucket_name)
137
+ # fix a path
138
+ server = "#{bucket_name}.#{server}"
139
+ key_path ||= '/'
140
+ path = "#{service}#{key_path}#{params_list}"
141
+ else
142
+ path = "#{service}/#{bucket_name}#{key_path}#{params_list}"
143
+ end
144
+ path_to_sign = "#{service}/#{bucket_name}#{key_path}#{params_list}"
145
+ # path_to_sign = "/#{bucket_name}#{key_path}#{params_list}"
146
+ [ server, path, path_to_sign ]
147
+ end
148
+
149
+ # Generates request hash for REST API.
150
+ # Assumes that headers[:url] is URL encoded (use CGI::escape)
151
+ def generate_rest_request(method, headers) # :nodoc:
152
+ # calculate request data
153
+ server, path, path_to_sign = fetch_request_params(headers)
154
+ data = headers[:data]
155
+ # remove unset(==optional) and symbolyc keys
156
+ headers.each{ |key, value| headers.delete(key) if (value.nil? || key.is_a?(Symbol)) }
157
+ #
158
+ headers['content-type'] ||= ''
159
+ headers['date'] = Time.now.httpdate
160
+ # create request
161
+ request = "Net::HTTP::#{method.capitalize}".constantize.new(path)
162
+ request.body = data if data
163
+ # set request headers and meta headers
164
+ headers.each { |key, value| request[key.to_s] = value }
165
+ #generate auth strings
166
+ auth_string = canonical_string(request.method, path_to_sign, request.to_hash)
167
+ signature = AwsUtils::sign(@aws_secret_access_key, auth_string)
168
+ # set other headers
169
+ request['Authorization'] = "AWS #{@aws_access_key_id}:#{signature}"
170
+ # prepare output hash
171
+ { :request => request,
172
+ :server => server,
173
+ :port => @params[:port],
174
+ :protocol => @params[:protocol] }
175
+ end
176
+
177
+ # Sends request to Amazon and parses the response.
178
+ # Raises AwsError if any banana happened.
179
+ def request_info(request, parser, &block) # :nodoc:
180
+ request_info_impl(:s3_connection, @@bench, request, parser, &block)
181
+ end
182
+
183
+ # Returns an array of customer's buckets. Each item is a +hash+.
184
+ #
185
+ # s3.list_all_my_buckets #=>
186
+ # [{:owner_id => "00000000009314cc309ffe736daa2b264357476c7fea6efb2c3347ac3ab2792a",
187
+ # :owner_display_name => "root",
188
+ # :name => "bucket_name",
189
+ # :creation_date => "2007-04-19T18:47:43.000Z"}, ..., {...}]
190
+ #
191
+ def list_all_my_buckets(headers={})
192
+ req_hash = generate_rest_request('GET', headers.merge(:url=>''))
193
+ request_info(req_hash, S3ListAllMyBucketsParser.new(:logger => @logger))
194
+ rescue
195
+ on_exception
196
+ end
197
+
198
+ # Creates new bucket. Returns +true+ or an exception.
199
+ #
200
+ # # create a bucket at American server
201
+ # s3.create_bucket('my-awesome-bucket-us') #=> true
202
+ # # create a bucket at European server
203
+ # s3.create_bucket('my-awesome-bucket-eu', :location => :eu) #=> true
204
+ #
205
+ def create_bucket(bucket, headers={})
206
+ data = nil
207
+ unless headers[:location].blank?
208
+ data = "<CreateBucketConfiguration><LocationConstraint>#{headers[:location].to_s.upcase}</LocationConstraint></CreateBucketConfiguration>"
209
+ end
210
+ req_hash = generate_rest_request('PUT', headers.merge(:url=>bucket, :data => data))
211
+ request_info(req_hash, RightHttp2xxParser.new)
212
+ rescue Exception => e
213
+ # if the bucket exists AWS returns an error for the location constraint interface. Drop it
214
+ e.is_a?(RightAws::AwsError) && e.message.include?('BucketAlreadyOwnedByYou') ? true : on_exception
215
+ end
216
+
217
+ # Retrieve bucket location
218
+ #
219
+ # s3.create_bucket('my-awesome-bucket-us') #=> true
220
+ # puts s3.bucket_location('my-awesome-bucket-us') #=> '' (Amazon's default value assumed)
221
+ #
222
+ # s3.create_bucket('my-awesome-bucket-eu', :location => :eu) #=> true
223
+ # puts s3.bucket_location('my-awesome-bucket-eu') #=> 'EU'
224
+ #
225
+ def bucket_location(bucket, headers={})
226
+ req_hash = generate_rest_request('GET', headers.merge(:url=>"#{bucket}?location"))
227
+ request_info(req_hash, S3BucketLocationParser.new)
228
+ rescue
229
+ on_exception
230
+ end
231
+
232
+ # Retrieves the logging configuration for a bucket.
233
+ # Returns a hash of {:enabled, :targetbucket, :targetprefix}
234
+ #
235
+ # s3.interface.get_logging_parse(:bucket => "asset_bucket")
236
+ # => {:enabled=>true, :targetbucket=>"mylogbucket", :targetprefix=>"loggylogs/"}
237
+ #
238
+ #
239
+ def get_logging_parse(params)
240
+ AwsUtils.mandatory_arguments([:bucket], params)
241
+ AwsUtils.allow_only([:bucket, :headers], params)
242
+ params[:headers] = {} unless params[:headers]
243
+ req_hash = generate_rest_request('GET', params[:headers].merge(:url=>"#{params[:bucket]}?logging"))
244
+ request_info(req_hash, S3LoggingParser.new)
245
+ rescue
246
+ on_exception
247
+ end
248
+
249
+ # Sets logging configuration for a bucket from the XML configuration document.
250
+ # params:
251
+ # :bucket
252
+ # :xmldoc
253
+ def put_logging(params)
254
+ AwsUtils.mandatory_arguments([:bucket,:xmldoc], params)
255
+ AwsUtils.allow_only([:bucket,:xmldoc, :headers], params)
256
+ params[:headers] = {} unless params[:headers]
257
+ req_hash = generate_rest_request('PUT', params[:headers].merge(:url=>"#{params[:bucket]}?logging", :data => params[:xmldoc]))
258
+ request_info(req_hash, RightHttp2xxParser.new)
259
+ rescue
260
+ on_exception
261
+ end
262
+
263
+ # Deletes new bucket. Bucket must be empty! Returns +true+ or an exception.
264
+ #
265
+ # s3.delete_bucket('my_awesome_bucket') #=> true
266
+ #
267
+ # See also: force_delete_bucket method
268
+ #
269
+ def delete_bucket(bucket, headers={})
270
+ req_hash = generate_rest_request('DELETE', headers.merge(:url=>bucket))
271
+ request_info(req_hash, RightHttp2xxParser.new)
272
+ rescue
273
+ on_exception
274
+ end
275
+
276
+ # Returns an array of bucket's keys. Each array item (key data) is a +hash+.
277
+ #
278
+ # s3.list_bucket('my_awesome_bucket', { 'prefix'=>'t', 'marker'=>'', 'max-keys'=>5, delimiter=>'' }) #=>
279
+ # [{:key => "test1",
280
+ # :last_modified => "2007-05-18T07:00:59.000Z",
281
+ # :owner_id => "00000000009314cc309ffe736daa2b264357476c7fea6efb2c3347ac3ab2792a",
282
+ # :owner_display_name => "root",
283
+ # :e_tag => "000000000059075b964b07152d234b70",
284
+ # :storage_class => "STANDARD",
285
+ # :size => 3,
286
+ # :service=> {'is_truncated' => false,
287
+ # 'prefix' => "t",
288
+ # 'marker' => "",
289
+ # 'name' => "my_awesome_bucket",
290
+ # 'max-keys' => "5"}, ..., {...}]
291
+ #
292
+ def list_bucket(bucket, options={}, headers={})
293
+ bucket += '?'+options.map{|k, v| "#{k.to_s}=#{CGI::escape v.to_s}"}.join('&') unless options.blank?
294
+ req_hash = generate_rest_request('GET', headers.merge(:url=>bucket))
295
+ request_info(req_hash, S3ListBucketParser.new(:logger => @logger))
296
+ rescue
297
+ on_exception
298
+ end
299
+
300
+ # Incrementally list the contents of a bucket. Yields the following hash to a block:
301
+ # s3.incrementally_list_bucket('my_awesome_bucket', { 'prefix'=>'t', 'marker'=>'', 'max-keys'=>5, delimiter=>'' }) yields
302
+ # {
303
+ # :name => 'bucketname',
304
+ # :prefix => 'subfolder/',
305
+ # :marker => 'fileN.jpg',
306
+ # :max_keys => 234,
307
+ # :delimiter => '/',
308
+ # :is_truncated => true,
309
+ # :next_marker => 'fileX.jpg',
310
+ # :contents => [
311
+ # { :key => "file1",
312
+ # :last_modified => "2007-05-18T07:00:59.000Z",
313
+ # :e_tag => "000000000059075b964b07152d234b70",
314
+ # :size => 3,
315
+ # :storage_class => "STANDARD",
316
+ # :owner_id => "00000000009314cc309ffe736daa2b264357476c7fea6efb2c3347ac3ab2792a",
317
+ # :owner_display_name => "root"
318
+ # }, { :key, ...}, ... {:key, ...}
319
+ # ]
320
+ # :common_prefixes => [
321
+ # "prefix1",
322
+ # "prefix2",
323
+ # ...,
324
+ # "prefixN"
325
+ # ]
326
+ # }
327
+ def incrementally_list_bucket(bucket, options={}, headers={}, &block)
328
+ internal_options = options.symbolize_keys
329
+ begin
330
+ internal_bucket = bucket.dup
331
+ internal_bucket += '?'+internal_options.map{|k, v| "#{k.to_s}=#{CGI::escape v.to_s}"}.join('&') unless internal_options.blank?
332
+ req_hash = generate_rest_request('GET', headers.merge(:url=>internal_bucket))
333
+ response = request_info(req_hash, S3ImprovedListBucketParser.new(:logger => @logger))
334
+ there_are_more_keys = response[:is_truncated]
335
+ if(there_are_more_keys)
336
+ internal_options[:marker] = decide_marker(response)
337
+ total_results = response[:contents].length + response[:common_prefixes].length
338
+ internal_options[:'max-keys'] ? (internal_options[:'max-keys'] -= total_results) : nil
339
+ end
340
+ yield response
341
+ end while there_are_more_keys && under_max_keys(internal_options)
342
+ true
343
+ rescue
344
+ on_exception
345
+ end
346
+
347
+
348
+ private
349
+ def decide_marker(response)
350
+ return response[:next_marker].dup if response[:next_marker]
351
+ last_key = response[:contents].last[:key]
352
+ last_prefix = response[:common_prefixes].last
353
+ if(!last_key)
354
+ return nil if(!last_prefix)
355
+ last_prefix.dup
356
+ elsif(!last_prefix)
357
+ last_key.dup
358
+ else
359
+ last_key > last_prefix ? last_key.dup : last_prefix.dup
360
+ end
361
+ end
362
+
363
+ def under_max_keys(internal_options)
364
+ internal_options[:'max-keys'] ? internal_options[:'max-keys'] > 0 : true
365
+ end
366
+
367
+ public
368
+ # Saves object to Amazon. Returns +true+ or an exception.
369
+ # Any header starting with AMAZON_METADATA_PREFIX is considered
370
+ # user metadata. It will be stored with the object and returned
371
+ # when you retrieve the object. The total size of the HTTP
372
+ # request, not including the body, must be less than 4 KB.
373
+ #
374
+ # s3.put('my_awesome_bucket', 'log/current/1.log', 'Ola-la!', 'x-amz-meta-family'=>'Woho556!') #=> true
375
+ #
376
+ # This method is capable of 'streaming' uploads; that is, it can upload
377
+ # data from a file or other IO object without first reading all the data
378
+ # into memory. This is most useful for large PUTs - it is difficult to read
379
+ # a 2 GB file entirely into memory before sending it to S3.
380
+ # To stream an upload, pass an object that responds to 'read' (like the read
381
+ # method of IO) and to either 'lstat' or 'size'. For files, this means
382
+ # streaming is enabled by simply making the call:
383
+ #
384
+ # s3.put(bucket_name, 'S3keyname.forthisfile', File.open('localfilename.dat'))
385
+ #
386
+ # If the IO object you wish to stream from responds to the read method but
387
+ # doesn't implement lstat or size, you can extend the object dynamically
388
+ # to implement these methods, or define your own class which defines these
389
+ # methods. Be sure that your class returns 'nil' from read() after having
390
+ # read 'size' bytes. Otherwise S3 will drop the socket after
391
+ # 'Content-Length' bytes have been uploaded, and HttpConnection will
392
+ # interpret this as an error.
393
+ #
394
+ # This method now supports very large PUTs, where very large
395
+ # is > 2 GB.
396
+ #
397
+ # For Win32 users: Files and IO objects should be opened in binary mode. If
398
+ # a text mode IO object is passed to PUT, it will be converted to binary
399
+ # mode.
400
+ #
401
+
402
+ def put(bucket, key, data=nil, headers={})
403
+ # On Windows, if someone opens a file in text mode, we must reset it so
404
+ # to binary mode for streaming to work properly
405
+ if(data.respond_to?(:binmode))
406
+ data.binmode
407
+ end
408
+ if (data.respond_to?(:lstat) && data.lstat.size >= USE_100_CONTINUE_PUT_SIZE) ||
409
+ (data.respond_to?(:size) && data.size >= USE_100_CONTINUE_PUT_SIZE)
410
+ headers['expect'] = '100-continue'
411
+ end
412
+ req_hash = generate_rest_request('PUT', headers.merge(:url=>"#{bucket}/#{CGI::escape key}", :data=>data))
413
+ request_info(req_hash, RightHttp2xxParser.new)
414
+ rescue
415
+ on_exception
416
+ end
417
+
418
+
419
+
420
+ # New experimental API for uploading objects, introduced in RightAws 1.8.1.
421
+ # store_object is similar in function to the older function put, but returns the full response metadata. It also allows for optional verification
422
+ # of object md5 checksums on upload. Parameters are passed as hash entries and are checked for completeness as well as for spurious arguments.
423
+ # The hash of the response headers contains useful information like the Amazon request ID and the object ETag (MD5 checksum).
424
+ #
425
+ # If the optional :md5 argument is provided, store_object verifies that the given md5 matches the md5 returned by S3. The :verified_md5 field in the response hash is
426
+ # set true or false depending on the outcome of this check. If no :md5 argument is given, :verified_md5 will be false in the response.
427
+ #
428
+ # The optional argument of :headers allows the caller to specify arbitrary request header values.
429
+ #
430
+ # s3.store_object(:bucket => "foobucket", :key => "foo", :md5 => "a507841b1bc8115094b00bbe8c1b2954", :data => "polemonium" )
431
+ # => {"x-amz-id-2"=>"SVsnS2nfDaR+ixyJUlRKM8GndRyEMS16+oZRieamuL61pPxPaTuWrWtlYaEhYrI/",
432
+ # "etag"=>"\"a507841b1bc8115094b00bbe8c1b2954\"",
433
+ # "date"=>"Mon, 29 Sep 2008 18:57:46 GMT",
434
+ # :verified_md5=>true,
435
+ # "x-amz-request-id"=>"63916465939995BA",
436
+ # "server"=>"AmazonS3",
437
+ # "content-length"=>"0"}
438
+ #
439
+ # s3.store_object(:bucket => "foobucket", :key => "foo", :data => "polemonium" )
440
+ # => {"x-amz-id-2"=>"MAt9PLjgLX9UYJ5tV2fI/5dBZdpFjlzRVpWgBDpvZpl+V+gJFcBMW2L+LBstYpbR",
441
+ # "etag"=>"\"a507841b1bc8115094b00bbe8c1b2954\"",
442
+ # "date"=>"Mon, 29 Sep 2008 18:58:56 GMT",
443
+ # :verified_md5=>false,
444
+ # "x-amz-request-id"=>"3B25A996BC2CDD3B",
445
+ # "server"=>"AmazonS3",
446
+ # "content-length"=>"0"}
447
+
448
+ def store_object(params)
449
+ AwsUtils.allow_only([:bucket, :key, :data, :headers, :md5], params)
450
+ AwsUtils.mandatory_arguments([:bucket, :key, :data], params)
451
+ params[:headers] = {} unless params[:headers]
452
+
453
+ params[:data].binmode if(params[:data].respond_to?(:binmode)) # On Windows, if someone opens a file in text mode, we must reset it to binary mode for streaming to work properly
454
+ if (params[:data].respond_to?(:lstat) && params[:data].lstat.size >= USE_100_CONTINUE_PUT_SIZE) ||
455
+ (params[:data].respond_to?(:size) && params[:data].size >= USE_100_CONTINUE_PUT_SIZE)
456
+ params[:headers]['expect'] = '100-continue'
457
+ end
458
+
459
+ req_hash = generate_rest_request('PUT', params[:headers].merge(:url=>"#{params[:bucket]}/#{CGI::escape params[:key]}", :data=>params[:data]))
460
+ resp = request_info(req_hash, S3HttpResponseHeadParser.new)
461
+ if(params[:md5])
462
+ resp[:verified_md5] = (resp['etag'].gsub(/\"/, '') == params[:md5]) ? true : false
463
+ else
464
+ resp[:verified_md5] = false
465
+ end
466
+ resp
467
+ rescue
468
+ on_exception
469
+ end
470
+
471
+ # Identical in function to store_object, but requires verification that the returned ETag is identical to the checksum passed in by the user as the 'md5' argument.
472
+ # If the check passes, returns the response metadata with the "verified_md5" field set true. Raises an exception if the checksums conflict.
473
+ # This call is implemented as a wrapper around store_object and the user may gain different semantics by creating a custom wrapper.
474
+ #
475
+ # s3.store_object_and_verify(:bucket => "foobucket", :key => "foo", :md5 => "a507841b1bc8115094b00bbe8c1b2954", :data => "polemonium" )
476
+ # => {"x-amz-id-2"=>"IZN3XsH4FlBU0+XYkFTfHwaiF1tNzrm6dIW2EM/cthKvl71nldfVC0oVQyydzWpb",
477
+ # "etag"=>"\"a507841b1bc8115094b00bbe8c1b2954\"",
478
+ # "date"=>"Mon, 29 Sep 2008 18:38:32 GMT",
479
+ # :verified_md5=>true,
480
+ # "x-amz-request-id"=>"E8D7EA4FE00F5DF7",
481
+ # "server"=>"AmazonS3",
482
+ # "content-length"=>"0"}
483
+ #
484
+ # s3.store_object_and_verify(:bucket => "foobucket", :key => "foo", :md5 => "a507841b1bc8115094b00bbe8c1b2953", :data => "polemonium" )
485
+ # RightAws::AwsError: Uploaded object failed MD5 checksum verification: {"x-amz-id-2"=>"HTxVtd2bf7UHHDn+WzEH43MkEjFZ26xuYvUzbstkV6nrWvECRWQWFSx91z/bl03n",
486
+ # "etag"=>"\"a507841b1bc8115094b00bbe8c1b2954\"",
487
+ # "date"=>"Mon, 29 Sep 2008 18:38:41 GMT",
488
+ # :verified_md5=>false,
489
+ # "x-amz-request-id"=>"0D7ADE09F42606F2",
490
+ # "server"=>"AmazonS3",
491
+ # "content-length"=>"0"}
492
+ def store_object_and_verify(params)
493
+ AwsUtils.mandatory_arguments([:md5], params)
494
+ r = store_object(params)
495
+ r[:verified_md5] ? (return r) : (raise AwsError.new("Uploaded object failed MD5 checksum verification: #{r.inspect}"))
496
+ end
497
+
498
+ # Retrieves object data from Amazon. Returns a +hash+ or an exception.
499
+ #
500
+ # s3.get('my_awesome_bucket', 'log/curent/1.log') #=>
501
+ #
502
+ # {:object => "Ola-la!",
503
+ # :headers => {"last-modified" => "Wed, 23 May 2007 09:08:04 GMT",
504
+ # "content-type" => "",
505
+ # "etag" => "\"000000000096f4ee74bc4596443ef2a4\"",
506
+ # "date" => "Wed, 23 May 2007 09:08:03 GMT",
507
+ # "x-amz-id-2" => "ZZZZZZZZZZZZZZZZZZZZ1HJXZoehfrS4QxcxTdNGldR7w/FVqblP50fU8cuIMLiu",
508
+ # "x-amz-meta-family" => "Woho556!",
509
+ # "x-amz-request-id" => "0000000C246D770C",
510
+ # "server" => "AmazonS3",
511
+ # "content-length" => "7"}}
512
+ #
513
+ # If a block is provided, yields incrementally to the block as
514
+ # the response is read. For large responses, this function is ideal as
515
+ # the response can be 'streamed'. The hash containing header fields is
516
+ # still returned.
517
+ # Example:
518
+ # foo = File.new('./chunder.txt', File::CREAT|File::RDWR)
519
+ # rhdr = s3.get('aws-test', 'Cent5V1_7_1.img.part.00') do |chunk|
520
+ # foo.write(chunk)
521
+ # end
522
+ # foo.close
523
+ #
524
+
525
+ def get(bucket, key, headers={}, &block)
526
+ req_hash = generate_rest_request('GET', headers.merge(:url=>"#{bucket}/#{CGI::escape key}"))
527
+ request_info(req_hash, S3HttpResponseBodyParser.new, &block)
528
+ rescue
529
+ on_exception
530
+ end
531
+
532
+ # New experimental API for retrieving objects, introduced in RightAws 1.8.1.
533
+ # retrieve_object is similar in function to the older function get. It allows for optional verification
534
+ # of object md5 checksums on retrieval. Parameters are passed as hash entries and are checked for completeness as well as for spurious arguments.
535
+ #
536
+ # If the optional :md5 argument is provided, retrieve_object verifies that the given md5 matches the md5 returned by S3. The :verified_md5 field in the response hash is
537
+ # set true or false depending on the outcome of this check. If no :md5 argument is given, :verified_md5 will be false in the response.
538
+ #
539
+ # The optional argument of :headers allows the caller to specify arbitrary request header values.
540
+ # Mandatory arguments:
541
+ # :bucket - the bucket in which the object is stored
542
+ # :key - the object address (or path) within the bucket
543
+ # Optional arguments:
544
+ # :headers - hash of additional HTTP headers to include with the request
545
+ # :md5 - MD5 checksum against which to verify the retrieved object
546
+ #
547
+ # s3.retrieve_object(:bucket => "foobucket", :key => "foo")
548
+ # => {:verified_md5=>false,
549
+ # :headers=>{"last-modified"=>"Mon, 29 Sep 2008 18:58:56 GMT",
550
+ # "x-amz-id-2"=>"2Aj3TDz6HP5109qly//18uHZ2a1TNHGLns9hyAtq2ved7wmzEXDOPGRHOYEa3Qnp",
551
+ # "content-type"=>"",
552
+ # "etag"=>"\"a507841b1bc8115094b00bbe8c1b2954\"",
553
+ # "date"=>"Tue, 30 Sep 2008 00:52:44 GMT",
554
+ # "x-amz-request-id"=>"EE4855DE27A2688C",
555
+ # "server"=>"AmazonS3",
556
+ # "content-length"=>"10"},
557
+ # :object=>"polemonium"}
558
+ #
559
+ # s3.retrieve_object(:bucket => "foobucket", :key => "foo", :md5=>'a507841b1bc8115094b00bbe8c1b2954')
560
+ # => {:verified_md5=>true,
561
+ # :headers=>{"last-modified"=>"Mon, 29 Sep 2008 18:58:56 GMT",
562
+ # "x-amz-id-2"=>"mLWQcI+VuKVIdpTaPXEo84g0cz+vzmRLbj79TS8eFPfw19cGFOPxuLy4uGYVCvdH",
563
+ # "content-type"=>"", "etag"=>"\"a507841b1bc8115094b00bbe8c1b2954\"",
564
+ # "date"=>"Tue, 30 Sep 2008 00:53:08 GMT",
565
+ # "x-amz-request-id"=>"6E7F317356580599",
566
+ # "server"=>"AmazonS3",
567
+ # "content-length"=>"10"},
568
+ # :object=>"polemonium"}
569
+ # If a block is provided, yields incrementally to the block as
570
+ # the response is read. For large responses, this function is ideal as
571
+ # the response can be 'streamed'. The hash containing header fields is
572
+ # still returned.
573
+ def retrieve_object(params, &block)
574
+ AwsUtils.mandatory_arguments([:bucket, :key], params)
575
+ AwsUtils.allow_only([:bucket, :key, :headers, :md5], params)
576
+ params[:headers] = {} unless params[:headers]
577
+ req_hash = generate_rest_request('GET', params[:headers].merge(:url=>"#{params[:bucket]}/#{CGI::escape params[:key]}"))
578
+ resp = request_info(req_hash, S3HttpResponseBodyParser.new, &block)
579
+ resp[:verified_md5] = false
580
+ if(params[:md5] && (resp[:headers]['etag'].gsub(/\"/,'') == params[:md5]))
581
+ resp[:verified_md5] = true
582
+ end
583
+ resp
584
+ rescue
585
+ on_exception
586
+ end
587
+
588
+ # Identical in function to retrieve_object, but requires verification that the returned ETag is identical to the checksum passed in by the user as the 'md5' argument.
589
+ # If the check passes, returns the response metadata with the "verified_md5" field set true. Raises an exception if the checksums conflict.
590
+ # This call is implemented as a wrapper around retrieve_object and the user may gain different semantics by creating a custom wrapper.
591
+ def retrieve_object_and_verify(params, &block)
592
+ AwsUtils.mandatory_arguments([:md5], params)
593
+ resp = retrieve_object(params, &block)
594
+ return resp if resp[:verified_md5]
595
+ raise AwsError.new("Retrieved object failed MD5 checksum verification: #{resp.inspect}")
596
+ end
597
+
598
+ # Retrieves object metadata. Returns a +hash+ of http_response_headers.
599
+ #
600
+ # s3.head('my_awesome_bucket', 'log/curent/1.log') #=>
601
+ # {"last-modified" => "Wed, 23 May 2007 09:08:04 GMT",
602
+ # "content-type" => "",
603
+ # "etag" => "\"000000000096f4ee74bc4596443ef2a4\"",
604
+ # "date" => "Wed, 23 May 2007 09:08:03 GMT",
605
+ # "x-amz-id-2" => "ZZZZZZZZZZZZZZZZZZZZ1HJXZoehfrS4QxcxTdNGldR7w/FVqblP50fU8cuIMLiu",
606
+ # "x-amz-meta-family" => "Woho556!",
607
+ # "x-amz-request-id" => "0000000C246D770C",
608
+ # "server" => "AmazonS3",
609
+ # "content-length" => "7"}
610
+ #
611
+ def head(bucket, key, headers={})
612
+ req_hash = generate_rest_request('HEAD', headers.merge(:url=>"#{bucket}/#{CGI::escape key}"))
613
+ request_info(req_hash, S3HttpResponseHeadParser.new)
614
+ rescue
615
+ on_exception
616
+ end
617
+
618
+ # Deletes key. Returns +true+ or an exception.
619
+ #
620
+ # s3.delete('my_awesome_bucket', 'log/curent/1.log') #=> true
621
+ #
622
+ def delete(bucket, key='', headers={})
623
+ req_hash = generate_rest_request('DELETE', headers.merge(:url=>"#{bucket}/#{CGI::escape key}"))
624
+ request_info(req_hash, RightHttp2xxParser.new)
625
+ rescue
626
+ on_exception
627
+ end
628
+
629
+ # Copy an object.
630
+ # directive: :copy - copy meta-headers from source (default value)
631
+ # :replace - replace meta-headers by passed ones
632
+ #
633
+ # # copy a key with meta-headers
634
+ # s3.copy('b1', 'key1', 'b1', 'key1_copy') #=> {:e_tag=>"\"e8b...8d\"", :last_modified=>"2008-05-11T10:25:22.000Z"}
635
+ #
636
+ # # copy a key, overwrite meta-headers
637
+ # s3.copy('b1', 'key2', 'b1', 'key2_copy', :replace, 'x-amz-meta-family'=>'Woho555!') #=> {:e_tag=>"\"e8b...8d\"", :last_modified=>"2008-05-11T10:26:22.000Z"}
638
+ #
639
+ # see: http://docs.amazonwebservices.com/AmazonS3/2006-03-01/UsingCopyingObjects.html
640
+ # http://docs.amazonwebservices.com/AmazonS3/2006-03-01/RESTObjectCOPY.html
641
+ #
642
+ def copy(src_bucket, src_key, dest_bucket, dest_key=nil, directive=:copy, headers={})
643
+ dest_key ||= src_key
644
+ headers['x-amz-metadata-directive'] = directive.to_s.upcase
645
+ headers['x-amz-copy-source'] = "#{src_bucket}/#{CGI::escape src_key}"
646
+ req_hash = generate_rest_request('PUT', headers.merge(:url=>"#{dest_bucket}/#{CGI::escape dest_key}"))
647
+ request_info(req_hash, S3CopyParser.new)
648
+ rescue
649
+ on_exception
650
+ end
651
+
652
+ # Move an object.
653
+ # directive: :copy - copy meta-headers from source (default value)
654
+ # :replace - replace meta-headers by passed ones
655
+ #
656
+ # # move bucket1/key1 to bucket1/key2
657
+ # s3.move('bucket1', 'key1', 'bucket1', 'key2') #=> {:e_tag=>"\"e8b...8d\"", :last_modified=>"2008-05-11T10:27:22.000Z"}
658
+ #
659
+ # # move bucket1/key1 to bucket2/key2 with new meta-headers assignment
660
+ # s3.copy('bucket1', 'key1', 'bucket2', 'key2', :replace, 'x-amz-meta-family'=>'Woho555!') #=> {:e_tag=>"\"e8b...8d\"", :last_modified=>"2008-05-11T10:28:22.000Z"}
661
+ #
662
+ def move(src_bucket, src_key, dest_bucket, dest_key=nil, directive=:copy, headers={})
663
+ copy_result = copy(src_bucket, src_key, dest_bucket, dest_key, directive, headers)
664
+ # delete an original key if it differs from a destination one
665
+ delete(src_bucket, src_key) unless src_bucket == dest_bucket && src_key == dest_key
666
+ copy_result
667
+ end
668
+
669
+ # Rename an object.
670
+ #
671
+ # # rename bucket1/key1 to bucket1/key2
672
+ # s3.rename('bucket1', 'key1', 'key2') #=> {:e_tag=>"\"e8b...8d\"", :last_modified=>"2008-05-11T10:29:22.000Z"}
673
+ #
674
+ def rename(src_bucket, src_key, dest_key, headers={})
675
+ move(src_bucket, src_key, src_bucket, dest_key, :copy, headers)
676
+ end
677
+
678
+ # Retieves the ACL (access control policy) for a bucket or object. Returns a hash of headers and xml doc with ACL data. See: http://docs.amazonwebservices.com/AmazonS3/2006-03-01/RESTAccessPolicy.html.
679
+ #
680
+ # s3.get_acl('my_awesome_bucket', 'log/curent/1.log') #=>
681
+ # {:headers => {"x-amz-id-2"=>"B3BdDMDUz+phFF2mGBH04E46ZD4Qb9HF5PoPHqDRWBv+NVGeA3TOQ3BkVvPBjgxX",
682
+ # "content-type"=>"application/xml;charset=ISO-8859-1",
683
+ # "date"=>"Wed, 23 May 2007 09:40:16 GMT",
684
+ # "x-amz-request-id"=>"B183FA7AB5FBB4DD",
685
+ # "server"=>"AmazonS3",
686
+ # "transfer-encoding"=>"chunked"},
687
+ # :object => "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<AccessControlPolicy xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\"><Owner>
688
+ # <ID>16144ab2929314cc309ffe736daa2b264357476c7fea6efb2c3347ac3ab2792a</ID><DisplayName>root</DisplayName></Owner>
689
+ # <AccessControlList><Grant><Grantee xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"CanonicalUser\"><ID>
690
+ # 16144ab2929314cc309ffe736daa2b264357476c7fea6efb2c3347ac3ab2792a</ID><DisplayName>root</DisplayName></Grantee>
691
+ # <Permission>FULL_CONTROL</Permission></Grant></AccessControlList></AccessControlPolicy>" }
692
+ #
693
+ def get_acl(bucket, key='', headers={})
694
+ key = key.blank? ? '' : "/#{CGI::escape key}"
695
+ req_hash = generate_rest_request('GET', headers.merge(:url=>"#{bucket}#{key}?acl"))
696
+ request_info(req_hash, S3HttpResponseBodyParser.new)
697
+ rescue
698
+ on_exception
699
+ end
700
+
701
+ # Retieves the ACL (access control policy) for a bucket or object.
702
+ # Returns a hash of {:owner, :grantees}
703
+ #
704
+ # s3.get_acl_parse('my_awesome_bucket', 'log/curent/1.log') #=>
705
+ #
706
+ # { :grantees=>
707
+ # { "16...2a"=>
708
+ # { :display_name=>"root",
709
+ # :permissions=>["FULL_CONTROL"],
710
+ # :attributes=>
711
+ # { "xsi:type"=>"CanonicalUser",
712
+ # "xmlns:xsi"=>"http://www.w3.org/2001/XMLSchema-instance"}},
713
+ # "http://acs.amazonaws.com/groups/global/AllUsers"=>
714
+ # { :display_name=>"AllUsers",
715
+ # :permissions=>["READ"],
716
+ # :attributes=>
717
+ # { "xsi:type"=>"Group",
718
+ # "xmlns:xsi"=>"http://www.w3.org/2001/XMLSchema-instance"}}},
719
+ # :owner=>
720
+ # { :id=>"16..2a",
721
+ # :display_name=>"root"}}
722
+ #
723
+ def get_acl_parse(bucket, key='', headers={})
724
+ key = key.blank? ? '' : "/#{CGI::escape key}"
725
+ req_hash = generate_rest_request('GET', headers.merge(:url=>"#{bucket}#{key}?acl"))
726
+ acl = request_info(req_hash, S3AclParser.new(:logger => @logger))
727
+ result = {}
728
+ result[:owner] = acl[:owner]
729
+ result[:grantees] = {}
730
+ acl[:grantees].each do |grantee|
731
+ key = grantee[:id] || grantee[:uri]
732
+ if result[:grantees].key?(key)
733
+ result[:grantees][key][:permissions] << grantee[:permissions]
734
+ else
735
+ result[:grantees][key] =
736
+ { :display_name => grantee[:display_name] || grantee[:uri].to_s[/[^\/]*$/],
737
+ :permissions => grantee[:permissions].to_a,
738
+ :attributes => grantee[:attributes] }
739
+ end
740
+ end
741
+ result
742
+ rescue
743
+ on_exception
744
+ end
745
+
746
+ # Sets the ACL on a bucket or object.
747
+ def put_acl(bucket, key, acl_xml_doc, headers={})
748
+ key = key.blank? ? '' : "/#{CGI::escape key}"
749
+ req_hash = generate_rest_request('PUT', headers.merge(:url=>"#{bucket}#{key}?acl", :data=>acl_xml_doc))
750
+ request_info(req_hash, S3HttpResponseBodyParser.new)
751
+ rescue
752
+ on_exception
753
+ end
754
+
755
+ # Retieves the ACL (access control policy) for a bucket. Returns a hash of headers and xml doc with ACL data.
756
+ def get_bucket_acl(bucket, headers={})
757
+ return get_acl(bucket, '', headers)
758
+ rescue
759
+ on_exception
760
+ end
761
+
762
+ # Sets the ACL on a bucket only.
763
+ def put_bucket_acl(bucket, acl_xml_doc, headers={})
764
+ return put_acl(bucket, '', acl_xml_doc, headers)
765
+ rescue
766
+ on_exception
767
+ end
768
+
769
+
770
+ # Removes all keys from bucket. Returns +true+ or an exception.
771
+ #
772
+ # s3.clear_bucket('my_awesome_bucket') #=> true
773
+ #
774
+ def clear_bucket(bucket)
775
+ incrementally_list_bucket(bucket) do |results|
776
+ results[:contents].each { |key| delete(bucket, key[:key]) }
777
+ end
778
+ true
779
+ rescue
780
+ on_exception
781
+ end
782
+
783
+ # Deletes all keys in bucket then deletes bucket. Returns +true+ or an exception.
784
+ #
785
+ # s3.force_delete_bucket('my_awesome_bucket')
786
+ #
787
+ def force_delete_bucket(bucket)
788
+ clear_bucket(bucket)
789
+ delete_bucket(bucket)
790
+ rescue
791
+ on_exception
792
+ end
793
+
794
+ # Deletes all keys where the 'folder_key' may be assumed as 'folder' name. Returns an array of string keys that have been deleted.
795
+ #
796
+ # s3.list_bucket('my_awesome_bucket').map{|key_data| key_data[:key]} #=> ['test','test/2/34','test/3','test1','test1/logs']
797
+ # s3.delete_folder('my_awesome_bucket','test') #=> ['test','test/2/34','test/3']
798
+ #
799
+ def delete_folder(bucket, folder_key, separator='/')
800
+ folder_key.chomp!(separator)
801
+ allkeys = []
802
+ incrementally_list_bucket(bucket, { 'prefix' => folder_key }) do |results|
803
+ keys = results[:contents].map{ |s3_key| s3_key[:key][/^#{folder_key}($|#{separator}.*)/] ? s3_key[:key] : nil}.compact
804
+ keys.each{ |key| delete(bucket, key) }
805
+ allkeys << keys
806
+ end
807
+ allkeys
808
+ rescue
809
+ on_exception
810
+ end
811
+
812
+ # Retrieves object data only (headers are omitted). Returns +string+ or an exception.
813
+ #
814
+ # s3.get('my_awesome_bucket', 'log/curent/1.log') #=> 'Ola-la!'
815
+ #
816
+ def get_object(bucket, key, headers={})
817
+ get(bucket, key, headers)[:object]
818
+ rescue
819
+ on_exception
820
+ end
821
+
822
+ #-----------------------------------------------------------------
823
+ # Query API: Links
824
+ #-----------------------------------------------------------------
825
+
826
+ # Generates link for QUERY API
827
+ def generate_link(method, headers={}, expires=nil) #:nodoc:
828
+ # calculate request data
829
+ server, path, path_to_sign = fetch_request_params(headers)
830
+ # expiration time
831
+ expires ||= DEFAULT_EXPIRES_AFTER
832
+ expires = Time.now.utc + expires if expires.is_a?(Fixnum) && (expires < ONE_YEAR_IN_SECONDS)
833
+ expires = expires.to_i
834
+ # remove unset(==optional) and symbolyc keys
835
+ headers.each{ |key, value| headers.delete(key) if (value.nil? || key.is_a?(Symbol)) }
836
+ #generate auth strings
837
+ auth_string = canonical_string(method, path_to_sign, headers, expires)
838
+ signature = CGI::escape(Base64.encode64(OpenSSL::HMAC.digest(OpenSSL::Digest::Digest.new("sha1"), @aws_secret_access_key, auth_string)).strip)
839
+ # path building
840
+ addon = "Signature=#{signature}&Expires=#{expires}&AWSAccessKeyId=#{@aws_access_key_id}"
841
+ path += path[/\?/] ? "&#{addon}" : "?#{addon}"
842
+ "#{@params[:protocol]}://#{server}:#{@params[:port]}#{path}"
843
+ rescue
844
+ on_exception
845
+ end
846
+
847
+ # Generates link for 'ListAllMyBuckets'.
848
+ #
849
+ # s3.list_all_my_buckets_link #=> url string
850
+ #
851
+ def list_all_my_buckets_link(expires=nil, headers={})
852
+ generate_link('GET', headers.merge(:url=>''), expires)
853
+ rescue
854
+ on_exception
855
+ end
856
+
857
+ # Generates link for 'CreateBucket'.
858
+ #
859
+ # s3.create_bucket_link('my_awesome_bucket') #=> url string
860
+ #
861
+ def create_bucket_link(bucket, expires=nil, headers={})
862
+ generate_link('PUT', headers.merge(:url=>bucket), expires)
863
+ rescue
864
+ on_exception
865
+ end
866
+
867
+ # Generates link for 'DeleteBucket'.
868
+ #
869
+ # s3.delete_bucket_link('my_awesome_bucket') #=> url string
870
+ #
871
+ def delete_bucket_link(bucket, expires=nil, headers={})
872
+ generate_link('DELETE', headers.merge(:url=>bucket), expires)
873
+ rescue
874
+ on_exception
875
+ end
876
+
877
+ # Generates link for 'ListBucket'.
878
+ #
879
+ # s3.list_bucket_link('my_awesome_bucket') #=> url string
880
+ #
881
+ def list_bucket_link(bucket, options=nil, expires=nil, headers={})
882
+ bucket += '?' + options.map{|k, v| "#{k.to_s}=#{CGI::escape v.to_s}"}.join('&') unless options.blank?
883
+ generate_link('GET', headers.merge(:url=>bucket), expires)
884
+ rescue
885
+ on_exception
886
+ end
887
+
888
+ # Generates link for 'PutObject'.
889
+ #
890
+ # s3.put_link('my_awesome_bucket',key, object) #=> url string
891
+ #
892
+ def put_link(bucket, key, data=nil, expires=nil, headers={})
893
+ generate_link('PUT', headers.merge(:url=>"#{bucket}/#{CGI::escape key}", :data=>data), expires)
894
+ rescue
895
+ on_exception
896
+ end
897
+
898
+ # Generates link for 'GetObject'.
899
+ #
900
+ # if a bucket comply with virtual hosting naming then retuns a link with the
901
+ # bucket as a part of host name:
902
+ #
903
+ # s3.get_link('my-awesome-bucket',key) #=> https://my-awesome-bucket.s3.amazonaws.com:443/asia%2Fcustomers?Signature=nh7...
904
+ #
905
+ # otherwise returns an old style link (the bucket is a part of path):
906
+ #
907
+ # s3.get_link('my_awesome_bucket',key) #=> https://s3.amazonaws.com:443/my_awesome_bucket/asia%2Fcustomers?Signature=QAO...
908
+ #
909
+ # see http://docs.amazonwebservices.com/AmazonS3/2006-03-01/VirtualHosting.html
910
+ def get_link(bucket, key, expires=nil, headers={})
911
+ generate_link('GET', headers.merge(:url=>"#{bucket}/#{CGI::escape key}"), expires)
912
+ rescue
913
+ on_exception
914
+ end
915
+
916
+ # Generates link for 'HeadObject'.
917
+ #
918
+ # s3.head_link('my_awesome_bucket',key) #=> url string
919
+ #
920
+ def head_link(bucket, key, expires=nil, headers={})
921
+ generate_link('HEAD', headers.merge(:url=>"#{bucket}/#{CGI::escape key}"), expires)
922
+ rescue
923
+ on_exception
924
+ end
925
+
926
+ # Generates link for 'DeleteObject'.
927
+ #
928
+ # s3.delete_link('my_awesome_bucket',key) #=> url string
929
+ #
930
+ def delete_link(bucket, key, expires=nil, headers={})
931
+ generate_link('DELETE', headers.merge(:url=>"#{bucket}/#{CGI::escape key}"), expires)
932
+ rescue
933
+ on_exception
934
+ end
935
+
936
+
937
+ # Generates link for 'GetACL'.
938
+ #
939
+ # s3.get_acl_link('my_awesome_bucket',key) #=> url string
940
+ #
941
+ def get_acl_link(bucket, key='', headers={})
942
+ return generate_link('GET', headers.merge(:url=>"#{bucket}/#{CGI::escape key}?acl"))
943
+ rescue
944
+ on_exception
945
+ end
946
+
947
+ # Generates link for 'PutACL'.
948
+ #
949
+ # s3.put_acl_link('my_awesome_bucket',key) #=> url string
950
+ #
951
+ def put_acl_link(bucket, key='', headers={})
952
+ return generate_link('PUT', headers.merge(:url=>"#{bucket}/#{CGI::escape key}?acl"))
953
+ rescue
954
+ on_exception
955
+ end
956
+
957
+ # Generates link for 'GetBucketACL'.
958
+ #
959
+ # s3.get_acl_link('my_awesome_bucket',key) #=> url string
960
+ #
961
+ def get_bucket_acl_link(bucket, headers={})
962
+ return get_acl_link(bucket, '', headers)
963
+ rescue
964
+ on_exception
965
+ end
966
+
967
+ # Generates link for 'PutBucketACL'.
968
+ #
969
+ # s3.put_acl_link('my_awesome_bucket',key) #=> url string
970
+ #
971
+ def put_bucket_acl_link(bucket, acl_xml_doc, headers={})
972
+ return put_acl_link(bucket, '', acl_xml_doc, headers)
973
+ rescue
974
+ on_exception
975
+ end
976
+
977
+ #-----------------------------------------------------------------
978
+ # PARSERS:
979
+ #-----------------------------------------------------------------
980
+
981
+ class S3ListAllMyBucketsParser < RightAWSParser # :nodoc:
982
+ def reset
983
+ @result = []
984
+ @owner = {}
985
+ end
986
+ def tagstart(name, attributes)
987
+ @current_bucket = {} if name == 'Bucket'
988
+ end
989
+ def tagend(name)
990
+ case name
991
+ when 'ID' ; @owner[:owner_id] = @text
992
+ when 'DisplayName' ; @owner[:owner_display_name] = @text
993
+ when 'Name' ; @current_bucket[:name] = @text
994
+ when 'CreationDate'; @current_bucket[:creation_date] = @text
995
+ when 'Bucket' ; @result << @current_bucket.merge(@owner)
996
+ end
997
+ end
998
+ end
999
+
1000
+ class S3ListBucketParser < RightAWSParser # :nodoc:
1001
+ def reset
1002
+ @result = []
1003
+ @service = {}
1004
+ @current_key = {}
1005
+ end
1006
+ def tagstart(name, attributes)
1007
+ @current_key = {} if name == 'Contents'
1008
+ end
1009
+ def tagend(name)
1010
+ case name
1011
+ # service info
1012
+ when 'Name' ; @service['name'] = @text
1013
+ when 'Prefix' ; @service['prefix'] = @text
1014
+ when 'Marker' ; @service['marker'] = @text
1015
+ when 'MaxKeys' ; @service['max-keys'] = @text
1016
+ when 'Delimiter' ; @service['delimiter'] = @text
1017
+ when 'IsTruncated' ; @service['is_truncated'] = (@text =~ /false/ ? false : true)
1018
+ # key data
1019
+ when 'Key' ; @current_key[:key] = @text
1020
+ when 'LastModified'; @current_key[:last_modified] = @text
1021
+ when 'ETag' ; @current_key[:e_tag] = @text
1022
+ when 'Size' ; @current_key[:size] = @text.to_i
1023
+ when 'StorageClass'; @current_key[:storage_class] = @text
1024
+ when 'ID' ; @current_key[:owner_id] = @text
1025
+ when 'DisplayName' ; @current_key[:owner_display_name] = @text
1026
+ when 'Contents' ; @current_key[:service] = @service; @result << @current_key
1027
+ end
1028
+ end
1029
+ end
1030
+
1031
+ class S3ImprovedListBucketParser < RightAWSParser # :nodoc:
1032
+ def reset
1033
+ @result = {}
1034
+ @result[:contents] = []
1035
+ @result[:common_prefixes] = []
1036
+ @contents = []
1037
+ @current_key = {}
1038
+ @common_prefixes = []
1039
+ @in_common_prefixes = false
1040
+ end
1041
+ def tagstart(name, attributes)
1042
+ @current_key = {} if name == 'Contents'
1043
+ @in_common_prefixes = true if name == 'CommonPrefixes'
1044
+ end
1045
+ def tagend(name)
1046
+ case name
1047
+ # service info
1048
+ when 'Name' ; @result[:name] = @text
1049
+ # Amazon uses the same tag for the search prefix and for the entries
1050
+ # in common prefix...so use our simple flag to see which element
1051
+ # we are parsing
1052
+ when 'Prefix' ; @in_common_prefixes ? @common_prefixes << @text : @result[:prefix] = @text
1053
+ when 'Marker' ; @result[:marker] = @text
1054
+ when 'MaxKeys' ; @result[:max_keys] = @text
1055
+ when 'Delimiter' ; @result[:delimiter] = @text
1056
+ when 'IsTruncated' ; @result[:is_truncated] = (@text =~ /false/ ? false : true)
1057
+ when 'NextMarker' ; @result[:next_marker] = @text
1058
+ # key data
1059
+ when 'Key' ; @current_key[:key] = @text
1060
+ when 'LastModified'; @current_key[:last_modified] = @text
1061
+ when 'ETag' ; @current_key[:e_tag] = @text
1062
+ when 'Size' ; @current_key[:size] = @text.to_i
1063
+ when 'StorageClass'; @current_key[:storage_class] = @text
1064
+ when 'ID' ; @current_key[:owner_id] = @text
1065
+ when 'DisplayName' ; @current_key[:owner_display_name] = @text
1066
+ when 'Contents' ; @result[:contents] << @current_key
1067
+ # Common Prefix stuff
1068
+ when 'CommonPrefixes' ; @result[:common_prefixes] = @common_prefixes; @in_common_prefixes = false
1069
+ end
1070
+ end
1071
+ end
1072
+
1073
+ class S3BucketLocationParser < RightAWSParser # :nodoc:
1074
+ def reset
1075
+ @result = ''
1076
+ end
1077
+ def tagend(name)
1078
+ @result = @text if name == 'LocationConstraint'
1079
+ end
1080
+ end
1081
+
1082
+ class S3AclParser < RightAWSParser # :nodoc:
1083
+ def reset
1084
+ @result = {:grantees=>[], :owner=>{}}
1085
+ @current_grantee = {}
1086
+ end
1087
+ def tagstart(name, attributes)
1088
+ @current_grantee = { :attributes => attributes } if name=='Grantee'
1089
+ end
1090
+ def tagend(name)
1091
+ case name
1092
+ # service info
1093
+ when 'ID'
1094
+ if @xmlpath == 'AccessControlPolicy/Owner'
1095
+ @result[:owner][:id] = @text
1096
+ else
1097
+ @current_grantee[:id] = @text
1098
+ end
1099
+ when 'DisplayName'
1100
+ if @xmlpath == 'AccessControlPolicy/Owner'
1101
+ @result[:owner][:display_name] = @text
1102
+ else
1103
+ @current_grantee[:display_name] = @text
1104
+ end
1105
+ when 'URI'
1106
+ @current_grantee[:uri] = @text
1107
+ when 'Permission'
1108
+ @current_grantee[:permissions] = @text
1109
+ when 'Grant'
1110
+ @result[:grantees] << @current_grantee
1111
+ end
1112
+ end
1113
+ end
1114
+
1115
+ class S3LoggingParser < RightAWSParser # :nodoc:
1116
+ def reset
1117
+ @result = {:enabled => false, :targetbucket => '', :targetprefix => ''}
1118
+ @current_grantee = {}
1119
+ end
1120
+ def tagend(name)
1121
+ case name
1122
+ # service info
1123
+ when 'TargetBucket'
1124
+ if @xmlpath == 'BucketLoggingStatus/LoggingEnabled'
1125
+ @result[:targetbucket] = @text
1126
+ @result[:enabled] = true
1127
+ end
1128
+ when 'TargetPrefix'
1129
+ if @xmlpath == 'BucketLoggingStatus/LoggingEnabled'
1130
+ @result[:targetprefix] = @text
1131
+ @result[:enabled] = true
1132
+ end
1133
+ end
1134
+ end
1135
+ end
1136
+
1137
+ class S3CopyParser < RightAWSParser # :nodoc:
1138
+ def reset
1139
+ @result = {}
1140
+ end
1141
+ def tagend(name)
1142
+ case name
1143
+ when 'LastModified' : @result[:last_modified] = @text
1144
+ when 'ETag' : @result[:e_tag] = @text
1145
+ end
1146
+ end
1147
+ end
1148
+
1149
+ #-----------------------------------------------------------------
1150
+ # PARSERS: Non XML
1151
+ #-----------------------------------------------------------------
1152
+
1153
+ class S3HttpResponseParser # :nodoc:
1154
+ attr_reader :result
1155
+ def parse(response)
1156
+ @result = response
1157
+ end
1158
+ def headers_to_string(headers)
1159
+ result = {}
1160
+ headers.each do |key, value|
1161
+ value = value.to_s if value.is_a?(Array) && value.size<2
1162
+ result[key] = value
1163
+ end
1164
+ result
1165
+ end
1166
+ end
1167
+
1168
+ class S3HttpResponseBodyParser < S3HttpResponseParser # :nodoc:
1169
+ def parse(response)
1170
+ @result = {
1171
+ :object => response.body,
1172
+ :headers => headers_to_string(response.to_hash)
1173
+ }
1174
+ end
1175
+ end
1176
+
1177
+ class S3HttpResponseHeadParser < S3HttpResponseParser # :nodoc:
1178
+ def parse(response)
1179
+ @result = headers_to_string(response.to_hash)
1180
+ end
1181
+ end
1182
+
1183
+ end
1184
+
1185
+ end