fmalamitsas-aws-s3 0.6.2.1254423625
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +107 -0
- data/COPYING +19 -0
- data/INSTALL +55 -0
- data/README.erb +58 -0
- data/Rakefile +334 -0
- data/TODO +26 -0
- data/aws-s3.gemspec +42 -0
- data/bin/s3sh +6 -0
- data/bin/setup.rb +10 -0
- data/lib/aws/s3.rb +60 -0
- data/lib/aws/s3/acl.rb +636 -0
- data/lib/aws/s3/authentication.rb +222 -0
- data/lib/aws/s3/base.rb +270 -0
- data/lib/aws/s3/bittorrent.rb +58 -0
- data/lib/aws/s3/bucket.rb +372 -0
- data/lib/aws/s3/connection.rb +288 -0
- data/lib/aws/s3/error.rb +69 -0
- data/lib/aws/s3/exceptions.rb +133 -0
- data/lib/aws/s3/extensions.rb +342 -0
- data/lib/aws/s3/logging.rb +317 -0
- data/lib/aws/s3/object.rb +626 -0
- data/lib/aws/s3/owner.rb +46 -0
- data/lib/aws/s3/parsing.rb +99 -0
- data/lib/aws/s3/response.rb +180 -0
- data/lib/aws/s3/service.rb +51 -0
- data/lib/aws/s3/version.rb +12 -0
- data/site/index.erb +41 -0
- data/site/public/images/box-and-gem.gif +0 -0
- data/site/public/images/favicon.ico +0 -0
- data/site/public/ruby.css +18 -0
- data/site/public/screen.css +99 -0
- data/support/faster-xml-simple/COPYING +18 -0
- data/support/faster-xml-simple/README +8 -0
- data/support/faster-xml-simple/Rakefile +54 -0
- data/support/faster-xml-simple/lib/faster_xml_simple.rb +190 -0
- data/support/faster-xml-simple/test/fixtures/test-1.rails.yml +4 -0
- data/support/faster-xml-simple/test/fixtures/test-1.xml +3 -0
- data/support/faster-xml-simple/test/fixtures/test-1.yml +4 -0
- data/support/faster-xml-simple/test/fixtures/test-2.rails.yml +6 -0
- data/support/faster-xml-simple/test/fixtures/test-2.xml +3 -0
- data/support/faster-xml-simple/test/fixtures/test-2.yml +6 -0
- data/support/faster-xml-simple/test/fixtures/test-3.rails.yml +6 -0
- data/support/faster-xml-simple/test/fixtures/test-3.xml +5 -0
- data/support/faster-xml-simple/test/fixtures/test-3.yml +6 -0
- data/support/faster-xml-simple/test/fixtures/test-4.rails.yml +5 -0
- data/support/faster-xml-simple/test/fixtures/test-4.xml +7 -0
- data/support/faster-xml-simple/test/fixtures/test-4.yml +5 -0
- data/support/faster-xml-simple/test/fixtures/test-5.rails.yml +8 -0
- data/support/faster-xml-simple/test/fixtures/test-5.xml +7 -0
- data/support/faster-xml-simple/test/fixtures/test-5.yml +8 -0
- data/support/faster-xml-simple/test/fixtures/test-6.rails.yml +43 -0
- data/support/faster-xml-simple/test/fixtures/test-6.xml +29 -0
- data/support/faster-xml-simple/test/fixtures/test-6.yml +41 -0
- data/support/faster-xml-simple/test/fixtures/test-7.rails.yml +23 -0
- data/support/faster-xml-simple/test/fixtures/test-7.xml +22 -0
- data/support/faster-xml-simple/test/fixtures/test-7.yml +22 -0
- data/support/faster-xml-simple/test/fixtures/test-8.rails.yml +14 -0
- data/support/faster-xml-simple/test/fixtures/test-8.xml +8 -0
- data/support/faster-xml-simple/test/fixtures/test-8.yml +11 -0
- data/support/faster-xml-simple/test/regression_test.rb +47 -0
- data/support/faster-xml-simple/test/test_helper.rb +17 -0
- data/support/faster-xml-simple/test/xml_simple_comparison_test.rb +46 -0
- data/support/rdoc/code_info.rb +211 -0
- data/test/acl_test.rb +254 -0
- data/test/authentication_test.rb +118 -0
- data/test/base_test.rb +136 -0
- data/test/bucket_test.rb +74 -0
- data/test/connection_test.rb +216 -0
- data/test/error_test.rb +70 -0
- data/test/extensions_test.rb +340 -0
- data/test/fixtures.rb +89 -0
- data/test/fixtures/buckets.yml +133 -0
- data/test/fixtures/errors.yml +34 -0
- data/test/fixtures/headers.yml +3 -0
- data/test/fixtures/logging.yml +15 -0
- data/test/fixtures/loglines.yml +5 -0
- data/test/fixtures/logs.yml +7 -0
- data/test/fixtures/policies.yml +16 -0
- data/test/logging_test.rb +89 -0
- data/test/mocks/fake_response.rb +26 -0
- data/test/object_test.rb +205 -0
- data/test/parsing_test.rb +66 -0
- data/test/remote/acl_test.rb +116 -0
- data/test/remote/bittorrent_test.rb +45 -0
- data/test/remote/bucket_test.rb +146 -0
- data/test/remote/logging_test.rb +82 -0
- data/test/remote/object_test.rb +379 -0
- data/test/remote/test_file.data +0 -0
- data/test/remote/test_helper.rb +33 -0
- data/test/response_test.rb +68 -0
- data/test/service_test.rb +23 -0
- data/test/test_helper.rb +118 -0
- metadata +241 -0
@@ -0,0 +1,222 @@
|
|
1
|
+
module AWS
|
2
|
+
module S3
|
3
|
+
# All authentication is taken care of for you by the AWS::S3 library. None the less, some details of the two types
|
4
|
+
# of authentication and when they are used may be of interest to some.
|
5
|
+
#
|
6
|
+
# === Header based authentication
|
7
|
+
#
|
8
|
+
# Header based authentication is achieved by setting a special <tt>Authorization</tt> header whose value
|
9
|
+
# is formatted like so:
|
10
|
+
#
|
11
|
+
# "AWS #{access_key_id}:#{encoded_canonical}"
|
12
|
+
#
|
13
|
+
# The <tt>access_key_id</tt> is the public key that is assigned by Amazon for a given account which you use when
|
14
|
+
# establishing your initial connection. The <tt>encoded_canonical</tt> is computed according to rules layed out
|
15
|
+
# by Amazon which we will describe presently.
|
16
|
+
#
|
17
|
+
# ==== Generating the encoded canonical string
|
18
|
+
#
|
19
|
+
# The "canonical string", generated by the CanonicalString class, is computed by collecting the current request method,
|
20
|
+
# a set of significant headers of the current request, and the current request path into a string.
|
21
|
+
# That canonical string is then encrypted with the <tt>secret_access_key</tt> assigned by Amazon. The resulting encrypted canonical
|
22
|
+
# string is then base 64 encoded.
|
23
|
+
#
|
24
|
+
# === Query string based authentication
|
25
|
+
#
|
26
|
+
# When accessing a restricted object from the browser, you can authenticate via the query string, by setting the following parameters:
|
27
|
+
#
|
28
|
+
# "AWSAccessKeyId=#{access_key_id}&Expires=#{expires}&Signature=#{encoded_canonical}"
|
29
|
+
#
|
30
|
+
# The QueryString class is responsible for generating the appropriate parameters for authentication via the
|
31
|
+
# query string.
|
32
|
+
#
|
33
|
+
# The <tt>access_key_id</tt> and <tt>encoded_canonical</tt> are the same as described in the Header based authentication section.
|
34
|
+
# The <tt>expires</tt> value dictates for how long the current url is valid (by default, it will expire in 5 minutes). Expiration can be specified
|
35
|
+
# either by an absolute time (expressed in seconds since the epoch), or in relative time (in number of seconds from now).
|
36
|
+
# Details of how to customize the expiration of the url are provided in the documentation for the QueryString class.
|
37
|
+
#
|
38
|
+
# All requests made by this library use header authentication. When a query string authenticated url is needed,
|
39
|
+
# the S3Object#url method will include the appropriate query string parameters.
|
40
|
+
#
|
41
|
+
# === Full authentication specification
|
42
|
+
#
|
43
|
+
# The full specification of the authentication protocol can be found at
|
44
|
+
# http://docs.amazonwebservices.com/AmazonS3/2006-03-01/RESTAuthentication.html
|
45
|
+
class Authentication
|
46
|
+
constant :AMAZON_HEADER_PREFIX, 'x-amz-'
|
47
|
+
|
48
|
+
# Signature is the abstract super class for the Header and QueryString authentication methods. It does the job
|
49
|
+
# of computing the canonical_string using the CanonicalString class as well as encoding the canonical string. The subclasses
|
50
|
+
# parameterize these computations and arrange them in a string form appropriate to how they are used, in one case a http request
|
51
|
+
# header value, and in the other case key/value query string parameter pairs.
|
52
|
+
class Signature < String #:nodoc:
|
53
|
+
attr_reader :request, :access_key_id, :secret_access_key, :current_host, :options
|
54
|
+
|
55
|
+
def initialize(request, access_key_id, secret_access_key, current_host = nil, options = {})
|
56
|
+
super()
|
57
|
+
@request, @access_key_id, @secret_access_key, @current_host = request, access_key_id, secret_access_key, current_host
|
58
|
+
@options = options
|
59
|
+
end
|
60
|
+
|
61
|
+
private
|
62
|
+
|
63
|
+
def canonical_string
|
64
|
+
options = {}
|
65
|
+
options[:expires] = expires if expires?
|
66
|
+
CanonicalString.new(request, current_host, options)
|
67
|
+
end
|
68
|
+
memoized :canonical_string
|
69
|
+
|
70
|
+
def encoded_canonical
|
71
|
+
digest = OpenSSL::Digest::Digest.new('sha1')
|
72
|
+
b64_hmac = [OpenSSL::HMAC.digest(digest, secret_access_key, canonical_string)].pack("m").strip
|
73
|
+
url_encode? ? CGI.escape(b64_hmac) : b64_hmac
|
74
|
+
end
|
75
|
+
|
76
|
+
def url_encode?
|
77
|
+
!@options[:url_encode].nil?
|
78
|
+
end
|
79
|
+
|
80
|
+
def expires?
|
81
|
+
is_a? QueryString
|
82
|
+
end
|
83
|
+
|
84
|
+
def date
|
85
|
+
request['date'].to_s.strip.empty? ? Time.now : Time.parse(request['date'])
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
# Provides header authentication by computing the value of the Authorization header. More details about the
|
90
|
+
# various authentication schemes can be found in the docs for its containing module, Authentication.
|
91
|
+
class Header < Signature #:nodoc:
|
92
|
+
def initialize(*args)
|
93
|
+
super
|
94
|
+
self << "AWS #{access_key_id}:#{encoded_canonical}"
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
# Provides query string authentication by computing the three authorization parameters: AWSAccessKeyId, Expires and Signature.
|
99
|
+
# More details about the various authentication schemes can be found in the docs for its containing module, Authentication.
|
100
|
+
class QueryString < Signature #:nodoc:
|
101
|
+
constant :DEFAULT_EXPIRY, 300 # 5 minutes
|
102
|
+
def initialize(*args)
|
103
|
+
super
|
104
|
+
options[:url_encode] = true
|
105
|
+
self << build
|
106
|
+
end
|
107
|
+
|
108
|
+
private
|
109
|
+
|
110
|
+
# Will return one of three values, in the following order of precedence:
|
111
|
+
#
|
112
|
+
# 1) Seconds since the epoch explicitly passed in the +:expires+ option
|
113
|
+
# 2) The current time in seconds since the epoch plus the number of seconds passed in
|
114
|
+
# the +:expires_in+ option
|
115
|
+
# 3) The current time in seconds since the epoch plus the default number of seconds (60 seconds)
|
116
|
+
def expires
|
117
|
+
return options[:expires] if options[:expires]
|
118
|
+
date.to_i + expires_in
|
119
|
+
end
|
120
|
+
|
121
|
+
def expires_in
|
122
|
+
options.has_key?(:expires_in) ? Integer(options[:expires_in]) : DEFAULT_EXPIRY
|
123
|
+
end
|
124
|
+
|
125
|
+
# Keep in alphabetical order
|
126
|
+
def build
|
127
|
+
"AWSAccessKeyId=#{access_key_id}&Expires=#{expires}&Signature=#{encoded_canonical}"
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
# The CanonicalString is used to generate an encrypted signature, signed with your secrect access key. It is composed of
|
132
|
+
# data related to the given request for which it provides authentication. This data includes the request method, request headers,
|
133
|
+
# and the request path. Both Header and QueryString use it to generate their signature.
|
134
|
+
class CanonicalString < String #:nodoc:
|
135
|
+
class << self
|
136
|
+
def default_headers
|
137
|
+
%w(content-type content-md5)
|
138
|
+
end
|
139
|
+
|
140
|
+
def interesting_headers
|
141
|
+
['content-md5', 'content-type', 'date', 'Host', amazon_header_prefix]
|
142
|
+
end
|
143
|
+
|
144
|
+
def amazon_header_prefix
|
145
|
+
/^#{AMAZON_HEADER_PREFIX}/io
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
attr_reader :request, :headers, :current_host
|
150
|
+
|
151
|
+
def initialize(request, current_host, options = {})
|
152
|
+
super()
|
153
|
+
@request = request
|
154
|
+
@current_host = current_host
|
155
|
+
@headers = {}
|
156
|
+
@options = options
|
157
|
+
# "For non-authenticated or anonymous requests. A NotImplemented error result code will be returned if
|
158
|
+
# an authenticated (signed) request specifies a Host: header other than 's3.amazonaws.com'"
|
159
|
+
# (from http://docs.amazonwebservices.com/AmazonS3/2006-03-01/VirtualHosting.html)
|
160
|
+
request['Host'] ||= DEFAULT_HOST
|
161
|
+
build
|
162
|
+
end
|
163
|
+
|
164
|
+
private
|
165
|
+
def build
|
166
|
+
self << "#{request.method}\n"
|
167
|
+
ensure_date_is_valid
|
168
|
+
|
169
|
+
initialize_headers
|
170
|
+
set_expiry!
|
171
|
+
|
172
|
+
headers.sort_by {|k, _| k}.each do |key, value|
|
173
|
+
value = value.to_s.strip
|
174
|
+
self << (key =~ self.class.amazon_header_prefix ? "#{key}:#{value}" : value)
|
175
|
+
self << "\n"
|
176
|
+
end
|
177
|
+
self << path
|
178
|
+
end
|
179
|
+
|
180
|
+
def initialize_headers
|
181
|
+
identify_interesting_headers
|
182
|
+
set_default_headers
|
183
|
+
end
|
184
|
+
|
185
|
+
def set_expiry!
|
186
|
+
self.headers['date'] = @options[:expires] if @options[:expires]
|
187
|
+
end
|
188
|
+
|
189
|
+
def ensure_date_is_valid
|
190
|
+
request['Date'] ||= Time.now.httpdate
|
191
|
+
end
|
192
|
+
|
193
|
+
def identify_interesting_headers
|
194
|
+
request.each do |key, value|
|
195
|
+
key = key.downcase # Can't modify frozen string so no bang
|
196
|
+
if self.class.interesting_headers.any? {|header| header === key}
|
197
|
+
self.headers[key] = value.to_s.strip
|
198
|
+
end
|
199
|
+
end
|
200
|
+
end
|
201
|
+
|
202
|
+
def set_default_headers
|
203
|
+
self.class.default_headers.each do |header|
|
204
|
+
self.headers[header] ||= ''
|
205
|
+
end
|
206
|
+
end
|
207
|
+
|
208
|
+
def path
|
209
|
+
[only_path, extract_significant_parameter].compact.join('?')
|
210
|
+
end
|
211
|
+
|
212
|
+
def extract_significant_parameter
|
213
|
+
request.path[/[&?](acl|torrent|logging)(?:&|=|$)/, 1]
|
214
|
+
end
|
215
|
+
|
216
|
+
def only_path
|
217
|
+
(current_host.nil? ? '' : "/#{current_host}") << request.path[/^[^?]*/]
|
218
|
+
end
|
219
|
+
end
|
220
|
+
end
|
221
|
+
end
|
222
|
+
end
|
data/lib/aws/s3/base.rb
ADDED
@@ -0,0 +1,270 @@
|
|
1
|
+
module AWS #:nodoc:
|
2
|
+
# AWS::S3 is a Ruby library for Amazon's Simple Storage Service's REST API (http://aws.amazon.com/s3).
|
3
|
+
# Full documentation of the currently supported API can be found at http://docs.amazonwebservices.com/AmazonS3/2006-03-01.
|
4
|
+
#
|
5
|
+
# == Getting started
|
6
|
+
#
|
7
|
+
# To get started you need to require 'aws/s3':
|
8
|
+
#
|
9
|
+
# % irb -rubygems
|
10
|
+
# irb(main):001:0> require 'aws/s3'
|
11
|
+
# # => true
|
12
|
+
#
|
13
|
+
# The AWS::S3 library ships with an interactive shell called <tt>s3sh</tt>. From within it, you have access to all the operations the library exposes from the command line.
|
14
|
+
#
|
15
|
+
# % s3sh
|
16
|
+
# >> Version
|
17
|
+
#
|
18
|
+
# Before you can do anything, you must establish a connection using Base.establish_connection!. A basic connection would look something like this:
|
19
|
+
#
|
20
|
+
# AWS::S3::Base.establish_connection!(
|
21
|
+
# :access_key_id => 'abc',
|
22
|
+
# :secret_access_key => '123'
|
23
|
+
# )
|
24
|
+
#
|
25
|
+
# The minimum connection options that you must specify are your access key id and your secret access key.
|
26
|
+
#
|
27
|
+
# (If you don't already have your access keys, all you need to sign up for the S3 service is an account at Amazon. You can sign up for S3 and get access keys by visiting http://aws.amazon.com/s3.)
|
28
|
+
#
|
29
|
+
# For convenience, if you set two special environment variables with the value of your access keys, the console will automatically create a default connection for you. For example:
|
30
|
+
#
|
31
|
+
# % cat .amazon_keys
|
32
|
+
# export AMAZON_ACCESS_KEY_ID='abcdefghijklmnop'
|
33
|
+
# export AMAZON_SECRET_ACCESS_KEY='1234567891012345'
|
34
|
+
#
|
35
|
+
# Then load it in your shell's rc file.
|
36
|
+
#
|
37
|
+
# % cat .zshrc
|
38
|
+
# if [[ -f "$HOME/.amazon_keys" ]]; then
|
39
|
+
# source "$HOME/.amazon_keys";
|
40
|
+
# fi
|
41
|
+
#
|
42
|
+
# See more connection details at AWS::S3::Connection::Management::ClassMethods.
|
43
|
+
module S3
|
44
|
+
constant :DEFAULT_HOST, 's3.amazonaws.com'
|
45
|
+
|
46
|
+
# AWS::S3::Base is the abstract super class of all classes who make requests against S3, such as the built in
|
47
|
+
# Service, Bucket and S3Object classes. It provides methods for making requests, inferring or setting response classes,
|
48
|
+
# processing request options, and accessing attributes from S3's response data.
|
49
|
+
#
|
50
|
+
# Establishing a connection with the Base class is the entry point to using the library:
|
51
|
+
#
|
52
|
+
# AWS::S3::Base.establish_connection!(:access_key_id => '...', :secret_access_key => '...')
|
53
|
+
#
|
54
|
+
# The <tt>:access_key_id</tt> and <tt>:secret_access_key</tt> are the two required connection options. More
|
55
|
+
# details can be found in the docs for Connection::Management::ClassMethods.
|
56
|
+
#
|
57
|
+
# Extensive examples can be found in the README[link:files/README.html].
|
58
|
+
class Base
|
59
|
+
|
60
|
+
cattr_accessor :current_host
|
61
|
+
|
62
|
+
class << self
|
63
|
+
# Wraps the current connection's request method and picks the appropriate response class to wrap the response in.
|
64
|
+
# If the response is an error, it will raise that error as an exception. All such exceptions can be caught by rescuing
|
65
|
+
# their superclass, the ResponseError exception class.
|
66
|
+
#
|
67
|
+
# It is unlikely that you would call this method directly. Subclasses of Base have convenience methods for each http request verb
|
68
|
+
# that wrap calls to request.
|
69
|
+
def request(verb, path, options = {}, body = nil, attempts = 0, &block)
|
70
|
+
Service.response = nil
|
71
|
+
process_options!(options, verb)
|
72
|
+
response = response_class.new(connection.request(verb, path, options, body, attempts, current_host, &block))
|
73
|
+
Service.response = response
|
74
|
+
|
75
|
+
Error::Response.new(response.response).error.raise if response.error?
|
76
|
+
if attempts > 0 && !current_host.match(".#{DEFAULT_HOST}")
|
77
|
+
establish_connection!(:server => DEFAULT_HOST)
|
78
|
+
end
|
79
|
+
response
|
80
|
+
# Once in a while, a request to S3 returns an internal error. A glitch in the matrix I presume. Since these
|
81
|
+
# errors are few and far between the request method will rescue InternalErrors the first three times they encouter them
|
82
|
+
# and will retry the request again. Most of the time the second attempt will work.
|
83
|
+
rescue InternalError, RequestTimeout
|
84
|
+
if attempts == 3
|
85
|
+
raise
|
86
|
+
else
|
87
|
+
attempts += 1
|
88
|
+
retry
|
89
|
+
end
|
90
|
+
rescue
|
91
|
+
raise unless response
|
92
|
+
if response.redirect?
|
93
|
+
new_host = response.parsed['endpoint']
|
94
|
+
establish_connection!(:server => new_host)
|
95
|
+
end
|
96
|
+
attempts == 3 || !response.redirect? ? raise : (attempts += 1; retry)
|
97
|
+
end
|
98
|
+
|
99
|
+
[:get, :post, :put, :delete, :head].each do |verb|
|
100
|
+
class_eval(<<-EVAL, __FILE__, __LINE__)
|
101
|
+
def #{verb}(path, headers = {}, body = nil, &block)
|
102
|
+
request(:#{verb}, path, headers, body, &block)
|
103
|
+
end
|
104
|
+
EVAL
|
105
|
+
end
|
106
|
+
|
107
|
+
# Called when a method which requires a bucket name is called without that bucket name specified. It will try to
|
108
|
+
# infer the current bucket by looking for it as the subdomain of the current connection's address. If no subdomain
|
109
|
+
# is found, CurrentBucketNotSpecified will be raised.
|
110
|
+
#
|
111
|
+
# MusicBucket.establish_connection! :server => 'jukeboxzero.s3.amazonaws.com'
|
112
|
+
# MusicBucket.connection.server
|
113
|
+
# => 'jukeboxzero.s3.amazonaws.com'
|
114
|
+
# MusicBucket.current_bucket
|
115
|
+
# => 'jukeboxzero'
|
116
|
+
#
|
117
|
+
# Rather than infering the current bucket from the subdomain, the current class' bucket can be explicitly set with
|
118
|
+
# set_current_bucket_to.
|
119
|
+
|
120
|
+
def current_host
|
121
|
+
@@current_host
|
122
|
+
end
|
123
|
+
|
124
|
+
def current_host=(host)
|
125
|
+
@@current_host = host
|
126
|
+
end
|
127
|
+
|
128
|
+
def current_bucket
|
129
|
+
connection.subdomain or raise CurrentBucketNotSpecified.new(connection.http.address)
|
130
|
+
end
|
131
|
+
|
132
|
+
# If you plan on always using a specific bucket for certain files, you can skip always having to specify the bucket by creating
|
133
|
+
# a subclass of Bucket or S3Object and telling it what bucket to use:
|
134
|
+
#
|
135
|
+
# class JukeBoxSong < AWS::S3::S3Object
|
136
|
+
# set_current_bucket_to 'jukebox'
|
137
|
+
# end
|
138
|
+
#
|
139
|
+
# For all methods that take a bucket name as an argument, the current bucket will be used if the bucket name argument is omitted.
|
140
|
+
#
|
141
|
+
# other_song = 'baby-please-come-home.mp3'
|
142
|
+
# JukeBoxSong.store(other_song, open(other_song))
|
143
|
+
#
|
144
|
+
# This time we didn't have to explicitly pass in the bucket name, as the JukeBoxSong class knows that it will
|
145
|
+
# always use the 'jukebox' bucket.
|
146
|
+
#
|
147
|
+
# "Astute readers", as they say, may have noticed that we used the third parameter to pass in the content type,
|
148
|
+
# rather than the fourth parameter as we had the last time we created an object. If the bucket can be inferred, or
|
149
|
+
# is explicitly set, as we've done in the JukeBoxSong class, then the third argument can be used to pass in
|
150
|
+
# options.
|
151
|
+
#
|
152
|
+
# Now all operations that would have required a bucket name no longer do.
|
153
|
+
#
|
154
|
+
# other_song = JukeBoxSong.find('baby-please-come-home.mp3')
|
155
|
+
def set_current_bucket_to(name)
|
156
|
+
raise ArgumentError, "`#{__method__}' must be called on a subclass of #{self.name}" if self == AWS::S3::Base
|
157
|
+
instance_eval(<<-EVAL)
|
158
|
+
def current_bucket
|
159
|
+
'#{name}'
|
160
|
+
end
|
161
|
+
EVAL
|
162
|
+
end
|
163
|
+
alias_method :current_bucket=, :set_current_bucket_to
|
164
|
+
|
165
|
+
private
|
166
|
+
|
167
|
+
def response_class
|
168
|
+
FindResponseClass.for(self)
|
169
|
+
end
|
170
|
+
|
171
|
+
def process_options!(options, verb)
|
172
|
+
options.replace(RequestOptions.process(options, verb))
|
173
|
+
end
|
174
|
+
|
175
|
+
# Using the conventions layed out in the <tt>response_class</tt> works for more than 80% of the time.
|
176
|
+
# There are a few edge cases though where we want a given class to wrap its responses in different
|
177
|
+
# response classes depending on which method is being called.
|
178
|
+
def respond_with(klass)
|
179
|
+
eval(<<-EVAL, binding, __FILE__, __LINE__)
|
180
|
+
def new_response_class
|
181
|
+
#{klass}
|
182
|
+
end
|
183
|
+
|
184
|
+
class << self
|
185
|
+
alias_method :old_response_class, :response_class
|
186
|
+
alias_method :response_class, :new_response_class
|
187
|
+
end
|
188
|
+
EVAL
|
189
|
+
|
190
|
+
yield
|
191
|
+
ensure
|
192
|
+
# Restore the original version
|
193
|
+
eval(<<-EVAL, binding, __FILE__, __LINE__)
|
194
|
+
class << self
|
195
|
+
alias_method :response_class, :old_response_class
|
196
|
+
end
|
197
|
+
EVAL
|
198
|
+
end
|
199
|
+
|
200
|
+
def bucket_name(name)
|
201
|
+
name || current_bucket
|
202
|
+
end
|
203
|
+
|
204
|
+
class RequestOptions < Hash #:nodoc:
|
205
|
+
attr_reader :options, :verb
|
206
|
+
|
207
|
+
class << self
|
208
|
+
def process(*args, &block)
|
209
|
+
new(*args, &block).process!
|
210
|
+
end
|
211
|
+
end
|
212
|
+
|
213
|
+
def initialize(options, verb = :get)
|
214
|
+
@options = options.to_normalized_options
|
215
|
+
@verb = verb
|
216
|
+
super()
|
217
|
+
end
|
218
|
+
|
219
|
+
def process!
|
220
|
+
set_access_controls! if verb == :put
|
221
|
+
replace(options)
|
222
|
+
end
|
223
|
+
|
224
|
+
private
|
225
|
+
def set_access_controls!
|
226
|
+
ACL::OptionProcessor.process!(options)
|
227
|
+
end
|
228
|
+
end
|
229
|
+
end
|
230
|
+
|
231
|
+
def initialize(attributes = {}) #:nodoc:
|
232
|
+
@attributes = attributes
|
233
|
+
end
|
234
|
+
|
235
|
+
private
|
236
|
+
attr_reader :attributes
|
237
|
+
|
238
|
+
def current_host
|
239
|
+
self.class.current_host
|
240
|
+
end
|
241
|
+
|
242
|
+
def current_host=(host)
|
243
|
+
self.class.current_host = host
|
244
|
+
end
|
245
|
+
|
246
|
+
def connection
|
247
|
+
self.class.connection
|
248
|
+
end
|
249
|
+
|
250
|
+
def http
|
251
|
+
connection.http
|
252
|
+
end
|
253
|
+
|
254
|
+
def request(*args, &block)
|
255
|
+
self.class.request(*args, &block)
|
256
|
+
end
|
257
|
+
|
258
|
+
def method_missing(method, *args, &block)
|
259
|
+
case
|
260
|
+
when attributes.has_key?(method.to_s)
|
261
|
+
attributes[method.to_s]
|
262
|
+
when attributes.has_key?(method)
|
263
|
+
attributes[method]
|
264
|
+
else
|
265
|
+
super
|
266
|
+
end
|
267
|
+
end
|
268
|
+
end
|
269
|
+
end
|
270
|
+
end
|