s3fsr 1.4
Sign up to get free protection for your applications and to get access to all the features.
- data/bin/s3fsr +26 -0
- data/lib/aws-matt/s3/acl.rb +636 -0
- data/lib/aws-matt/s3/authentication.rb +221 -0
- data/lib/aws-matt/s3/base.rb +236 -0
- data/lib/aws-matt/s3/bittorrent.rb +58 -0
- data/lib/aws-matt/s3/bucket.rb +348 -0
- data/lib/aws-matt/s3/connection.rb +323 -0
- data/lib/aws-matt/s3/error.rb +69 -0
- data/lib/aws-matt/s3/exceptions.rb +133 -0
- data/lib/aws-matt/s3/extensions.rb +324 -0
- data/lib/aws-matt/s3/logging.rb +311 -0
- data/lib/aws-matt/s3/object.rb +619 -0
- data/lib/aws-matt/s3/owner.rb +44 -0
- data/lib/aws-matt/s3/parsing.rb +99 -0
- data/lib/aws-matt/s3/response.rb +180 -0
- data/lib/aws-matt/s3/service.rb +51 -0
- data/lib/aws-matt/s3/version.rb +12 -0
- data/lib/aws-matt/s3.rb +61 -0
- data/lib/s3fsr.rb +296 -0
- metadata +72 -0
@@ -0,0 +1,348 @@
|
|
1
|
+
module AWS
|
2
|
+
module S3
|
3
|
+
# Buckets are containers for objects (the files you store on S3). To create a new bucket you just specify its name.
|
4
|
+
#
|
5
|
+
# # Pick a unique name, or else you'll get an error
|
6
|
+
# # if the name is already taken.
|
7
|
+
# Bucket.create('jukebox')
|
8
|
+
#
|
9
|
+
# Bucket names must be unique across the entire S3 system, sort of like domain names across the internet. If you try
|
10
|
+
# to create a bucket with a name that is already taken, you will get an error.
|
11
|
+
#
|
12
|
+
# Assuming the name you chose isn't already taken, your new bucket will now appear in the bucket list:
|
13
|
+
#
|
14
|
+
# Service.buckets
|
15
|
+
# # => [#<AWS::S3::Bucket @attributes={"name"=>"jukebox"}>]
|
16
|
+
#
|
17
|
+
# Once you have succesfully created a bucket you can you can fetch it by name using Bucket.find.
|
18
|
+
#
|
19
|
+
# music_bucket = Bucket.find('jukebox')
|
20
|
+
#
|
21
|
+
# The bucket that is returned will contain a listing of all the objects in the bucket.
|
22
|
+
#
|
23
|
+
# music_bucket.objects.size
|
24
|
+
# # => 0
|
25
|
+
#
|
26
|
+
# If all you are interested in is the objects of the bucket, you can get to them directly using Bucket.objects.
|
27
|
+
#
|
28
|
+
# Bucket.objects('jukebox').size
|
29
|
+
# # => 0
|
30
|
+
#
|
31
|
+
# By default all objects will be returned, though there are several options you can use to limit what is returned, such as
|
32
|
+
# specifying that only objects whose name is after a certain place in the alphabet be returned, and etc. Details about these options can
|
33
|
+
# be found in the documentation for Bucket.find.
|
34
|
+
#
|
35
|
+
# To add an object to a bucket you specify the name of the object, its value, and the bucket to put it in.
|
36
|
+
#
|
37
|
+
# file = 'black-flowers.mp3'
|
38
|
+
# S3Object.store(file, open(file), 'jukebox')
|
39
|
+
#
|
40
|
+
# You'll see your file has been added to it:
|
41
|
+
#
|
42
|
+
# music_bucket.objects
|
43
|
+
# # => [#<AWS::S3::S3Object '/jukebox/black-flowers.mp3'>]
|
44
|
+
#
|
45
|
+
# You can treat your bucket like a hash and access objects by name:
|
46
|
+
#
|
47
|
+
# jukebox['black-flowers.mp3']
|
48
|
+
# # => #<AWS::S3::S3Object '/jukebox/black-flowers.mp3'>
|
49
|
+
#
|
50
|
+
# In the event that you want to delete a bucket, you can use Bucket.delete.
|
51
|
+
#
|
52
|
+
# Bucket.delete('jukebox')
|
53
|
+
#
|
54
|
+
# Keep in mind, like unix directories, you can not delete a bucket unless it is empty. Trying to delete a bucket
|
55
|
+
# that contains objects will raise a BucketNotEmpty exception.
|
56
|
+
#
|
57
|
+
# Passing the :force => true option to delete will take care of deleting all the bucket's objects for you.
|
58
|
+
#
|
59
|
+
# Bucket.delete('photos', :force => true)
|
60
|
+
# # => true
|
61
|
+
class Bucket < Base
|
62
|
+
class << self
|
63
|
+
# Creates a bucket named <tt>name</tt>.
|
64
|
+
#
|
65
|
+
# Bucket.create('jukebox')
|
66
|
+
#
|
67
|
+
# Your bucket name must be unique across all of S3. If the name
|
68
|
+
# you request has already been taken, you will get a 409 Conflict response, and a BucketAlreadyExists exception
|
69
|
+
# will be raised.
|
70
|
+
#
|
71
|
+
# By default new buckets have their access level set to private. You can override this using the <tt>:access</tt> option.
|
72
|
+
#
|
73
|
+
# Bucket.create('internet_drop_box', :access => :public_read_write)
|
74
|
+
#
|
75
|
+
# The full list of access levels that you can set on Bucket and S3Object creation are listed in the README[link:files/README.html]
|
76
|
+
# in the section called 'Setting access levels'.
|
77
|
+
def create(name, options = {})
|
78
|
+
validate_name!(name)
|
79
|
+
put("/#{name}", options).success?
|
80
|
+
end
|
81
|
+
|
82
|
+
# Fetches the bucket named <tt>name</tt>.
|
83
|
+
#
|
84
|
+
# Bucket.find('jukebox')
|
85
|
+
#
|
86
|
+
# If a default bucket is inferable from the current connection's subdomain, or if set explicitly with Base.set_current_bucket,
|
87
|
+
# it will be used if no bucket is specified.
|
88
|
+
#
|
89
|
+
# MusicBucket.current_bucket
|
90
|
+
# => 'jukebox'
|
91
|
+
# MusicBucket.find.name
|
92
|
+
# => 'jukebox'
|
93
|
+
#
|
94
|
+
# By default all objects contained in the bucket will be returned (sans their data) along with the bucket.
|
95
|
+
# You can access your objects using the Bucket#objects method.
|
96
|
+
#
|
97
|
+
# Bucket.find('jukebox').objects
|
98
|
+
#
|
99
|
+
# There are several options which allow you to limit which objects are retrieved. The list of object filtering options
|
100
|
+
# are listed in the documentation for Bucket.objects.
|
101
|
+
def find(name = nil, options = {})
|
102
|
+
new(get(path(name, options)).bucket)
|
103
|
+
end
|
104
|
+
|
105
|
+
# Return just the objects in the bucket named <tt>name</tt>.
|
106
|
+
#
|
107
|
+
# By default all objects of the named bucket will be returned. There are options, though, for filtering
|
108
|
+
# which objects are returned.
|
109
|
+
#
|
110
|
+
# === Object filtering options
|
111
|
+
#
|
112
|
+
# * <tt>:max_keys</tt> - The maximum number of keys you'd like to see in the response body.
|
113
|
+
# The server may return fewer than this many keys, but will not return more.
|
114
|
+
#
|
115
|
+
# Bucket.objects('jukebox').size
|
116
|
+
# # => 3
|
117
|
+
# Bucket.objects('jukebox', :max_keys => 1).size
|
118
|
+
# # => 1
|
119
|
+
#
|
120
|
+
# * <tt>:prefix</tt> - Restricts the response to only contain results that begin with the specified prefix.
|
121
|
+
#
|
122
|
+
# Bucket.objects('jukebox')
|
123
|
+
# # => [<AWS::S3::S3Object '/jazz/miles.mp3'>, <AWS::S3::S3Object '/jazz/dolphy.mp3'>, <AWS::S3::S3Object '/classical/malher.mp3'>]
|
124
|
+
# Bucket.objects('jukebox', :prefix => 'classical')
|
125
|
+
# # => [<AWS::S3::S3Object '/classical/malher.mp3'>]
|
126
|
+
#
|
127
|
+
# * <tt>:marker</tt> - Marker specifies where in the result set to resume listing. It restricts the response
|
128
|
+
# to only contain results that occur alphabetically _after_ the value of marker. To retrieve the next set of results,
|
129
|
+
# use the last key from the current page of results as the marker in your next request.
|
130
|
+
#
|
131
|
+
# # Skip 'mahler'
|
132
|
+
# Bucket.objects('jukebox', :marker => 'mb')
|
133
|
+
# # => [<AWS::S3::S3Object '/jazz/miles.mp3'>]
|
134
|
+
#
|
135
|
+
# === Examples
|
136
|
+
#
|
137
|
+
# # Return no more than 2 objects whose key's are listed alphabetically after the letter 'm'.
|
138
|
+
# Bucket.objects('jukebox', :marker => 'm', :max_keys => 2)
|
139
|
+
# # => [<AWS::S3::S3Object '/jazz/miles.mp3'>, <AWS::S3::S3Object '/classical/malher.mp3'>]
|
140
|
+
#
|
141
|
+
# # Return no more than 2 objects whose key's are listed alphabetically after the letter 'm' and have the 'jazz' prefix.
|
142
|
+
# Bucket.objects('jukebox', :marker => 'm', :max_keys => 2, :prefix => 'jazz')
|
143
|
+
# # => [<AWS::S3::S3Object '/jazz/miles.mp3'>]
|
144
|
+
def objects(name = nil, options = {})
|
145
|
+
find(name, options).object_cache
|
146
|
+
end
|
147
|
+
|
148
|
+
def common_prefixes(name= nil, options = {})
|
149
|
+
find(name, options).common_prefix_cache
|
150
|
+
end
|
151
|
+
|
152
|
+
# Deletes the bucket named <tt>name</tt>.
|
153
|
+
#
|
154
|
+
# All objects in the bucket must be deleted before the bucket can be deleted. If the bucket is not empty,
|
155
|
+
# BucketNotEmpty will be raised.
|
156
|
+
#
|
157
|
+
# You can side step this issue by passing the :force => true option to delete which will take care of
|
158
|
+
# emptying the bucket before deleting it.
|
159
|
+
#
|
160
|
+
# Bucket.delete('photos', :force => true)
|
161
|
+
#
|
162
|
+
# Only the owner of a bucket can delete a bucket, regardless of the bucket's access control policy.
|
163
|
+
def delete(name = nil, options = {})
|
164
|
+
find(name).delete_all if options[:force]
|
165
|
+
|
166
|
+
name = path(name)
|
167
|
+
Base.delete(name).success?
|
168
|
+
end
|
169
|
+
|
170
|
+
# List all your buckets. This is a convenient wrapper around AWS::S3::Service.buckets.
|
171
|
+
def list(reload = false)
|
172
|
+
Service.buckets(reload)
|
173
|
+
end
|
174
|
+
|
175
|
+
private
|
176
|
+
def validate_name!(name)
|
177
|
+
raise InvalidBucketName.new(name) unless name =~ /^[-\w.]{3,255}$/
|
178
|
+
end
|
179
|
+
|
180
|
+
def path(name, options = {})
|
181
|
+
if name.is_a?(Hash)
|
182
|
+
options = name
|
183
|
+
name = nil
|
184
|
+
end
|
185
|
+
"/#{bucket_name(name)}#{RequestOptions.process(options).to_query_string}"
|
186
|
+
end
|
187
|
+
end
|
188
|
+
|
189
|
+
attr_reader :object_cache, :common_prefix_cache #:nodoc:
|
190
|
+
|
191
|
+
include Enumerable
|
192
|
+
|
193
|
+
def initialize(attributes = {}) #:nodoc:
|
194
|
+
super
|
195
|
+
@object_cache = []
|
196
|
+
@common_prefix_cache = []
|
197
|
+
build_contents!
|
198
|
+
end
|
199
|
+
|
200
|
+
# Fetches the object named <tt>object_key</tt>, or nil if the bucket does not contain an object with the
|
201
|
+
# specified key.
|
202
|
+
#
|
203
|
+
# bucket.objects
|
204
|
+
# => [#<AWS::S3::S3Object '/marcel_molina/beluga_baby.jpg'>,
|
205
|
+
# #<AWS::S3::S3Object '/marcel_molina/tongue_overload.jpg'>]
|
206
|
+
# bucket['beluga_baby.jpg']
|
207
|
+
# => #<AWS::S3::S3Object '/marcel_molina/beluga_baby.jpg'>
|
208
|
+
def [](object_key)
|
209
|
+
detect {|file| file.key == object_key.to_s}
|
210
|
+
end
|
211
|
+
|
212
|
+
# Initializes a new S3Object belonging to the current bucket.
|
213
|
+
#
|
214
|
+
# object = bucket.new_object
|
215
|
+
# object.value = data
|
216
|
+
# object.key = 'classical/mahler.mp3'
|
217
|
+
# object.store
|
218
|
+
# bucket.objects.include?(object)
|
219
|
+
# => true
|
220
|
+
def new_object(attributes = {})
|
221
|
+
object = S3Object.new(attributes)
|
222
|
+
register(object)
|
223
|
+
object
|
224
|
+
end
|
225
|
+
|
226
|
+
# List S3Object's of the bucket.
|
227
|
+
#
|
228
|
+
# Once fetched the objects will be cached. You can reload the objects by passing <tt>:reload</tt>.
|
229
|
+
#
|
230
|
+
# bucket.objects(:reload)
|
231
|
+
#
|
232
|
+
# You can also filter the objects using the same options listed in Bucket.objects.
|
233
|
+
#
|
234
|
+
# bucket.objects(:prefix => 'jazz')
|
235
|
+
#
|
236
|
+
# Using these filtering options will implictly reload the objects.
|
237
|
+
#
|
238
|
+
# To reclaim all the objects for the bucket you can pass in :reload again.
|
239
|
+
def objects(options = {})
|
240
|
+
if options.is_a?(Hash)
|
241
|
+
reload = !options.empty?
|
242
|
+
else
|
243
|
+
reload = options
|
244
|
+
options = {}
|
245
|
+
end
|
246
|
+
|
247
|
+
reload!(options) if reload || object_cache.empty?
|
248
|
+
object_cache
|
249
|
+
end
|
250
|
+
|
251
|
+
def common_prefixes(options = {})
|
252
|
+
if options.is_a?(Hash)
|
253
|
+
reload = !options.empty?
|
254
|
+
else
|
255
|
+
reload = options
|
256
|
+
options = {}
|
257
|
+
end
|
258
|
+
|
259
|
+
reload!(options) if reload || common_prefix_cache.empty?
|
260
|
+
common_prefix_cache
|
261
|
+
end
|
262
|
+
|
263
|
+
# Iterates over the objects in the bucket.
|
264
|
+
#
|
265
|
+
# bucket.each do |object|
|
266
|
+
# # Do something with the object ...
|
267
|
+
# end
|
268
|
+
def each(&block)
|
269
|
+
# Dup the collection since we might be destructively modifying the object_cache during the iteration.
|
270
|
+
objects.dup.each(&block)
|
271
|
+
end
|
272
|
+
|
273
|
+
# Returns true if there are no objects in the bucket.
|
274
|
+
def empty?
|
275
|
+
objects.empty? && common_prefixes.empty?
|
276
|
+
end
|
277
|
+
|
278
|
+
# Returns the number of objects in the bucket.
|
279
|
+
def size
|
280
|
+
objects.size
|
281
|
+
end
|
282
|
+
|
283
|
+
# Deletes the bucket. See its class method counter part Bucket.delete for caveats about bucket deletion and how to ensure
|
284
|
+
# a bucket is deleted no matter what.
|
285
|
+
def delete(options = {})
|
286
|
+
self.class.delete(name, options)
|
287
|
+
end
|
288
|
+
|
289
|
+
# Delete all files in the bucket. Use with caution. Can not be undone.
|
290
|
+
def delete_all
|
291
|
+
each do |object|
|
292
|
+
object.delete
|
293
|
+
end
|
294
|
+
self
|
295
|
+
end
|
296
|
+
alias_method :clear, :delete_all
|
297
|
+
|
298
|
+
# Buckets observe their objects and have this method called when one of their objects
|
299
|
+
# is either stored or deleted.
|
300
|
+
def update(action, object) #:nodoc:
|
301
|
+
case action
|
302
|
+
when :stored then add object unless objects.include?(object)
|
303
|
+
when :deleted then object_cache.delete(object)
|
304
|
+
end
|
305
|
+
end
|
306
|
+
|
307
|
+
private
|
308
|
+
def build_contents!
|
309
|
+
if has_contents? then
|
310
|
+
attributes.delete('contents').each do |content|
|
311
|
+
add new_object(content)
|
312
|
+
end
|
313
|
+
end
|
314
|
+
|
315
|
+
if attributes['common_prefixes']
|
316
|
+
attributes.delete('common_prefixes').each do |common_prefix|
|
317
|
+
common_prefix_cache << common_prefix['prefix']
|
318
|
+
end
|
319
|
+
end
|
320
|
+
end
|
321
|
+
|
322
|
+
def has_contents?
|
323
|
+
attributes.has_key?('contents')
|
324
|
+
end
|
325
|
+
|
326
|
+
def add(object)
|
327
|
+
register(object)
|
328
|
+
object_cache << object
|
329
|
+
end
|
330
|
+
|
331
|
+
def register(object)
|
332
|
+
object.bucket = self
|
333
|
+
end
|
334
|
+
|
335
|
+
def reload!(options = {})
|
336
|
+
object_cache.clear
|
337
|
+
self.class.objects(name, options).each do |object|
|
338
|
+
add object
|
339
|
+
end
|
340
|
+
|
341
|
+
common_prefix_cache.clear
|
342
|
+
self.class.common_prefixes(name, options).each do |common_prefix|
|
343
|
+
common_prefix_cache << common_prefix
|
344
|
+
end
|
345
|
+
end
|
346
|
+
end
|
347
|
+
end
|
348
|
+
end
|
@@ -0,0 +1,323 @@
|
|
1
|
+
module AWS
|
2
|
+
module S3
|
3
|
+
class Connection #:nodoc:
|
4
|
+
class << self
|
5
|
+
def connect(options = {})
|
6
|
+
new(options)
|
7
|
+
end
|
8
|
+
|
9
|
+
def prepare_path(path)
|
10
|
+
path = path.remove_extended unless path.utf8?
|
11
|
+
URI.escape(path)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
attr_reader :access_key_id, :secret_access_key, :http, :options
|
16
|
+
|
17
|
+
# Creates a new connection. Connections make the actual requests to S3, though these requests are usually
|
18
|
+
# called from subclasses of Base.
|
19
|
+
#
|
20
|
+
# For details on establishing connections, check the Connection::Management::ClassMethods.
|
21
|
+
def initialize(options = {})
|
22
|
+
@options = Options.new(options)
|
23
|
+
connect
|
24
|
+
end
|
25
|
+
|
26
|
+
def request(verb, path, headers = {}, body = nil, attempts = 0, &block)
|
27
|
+
body.rewind if body.respond_to?(:rewind) unless attempts.zero?
|
28
|
+
|
29
|
+
requester = Proc.new do
|
30
|
+
path = self.class.prepare_path(path) if attempts.zero? # Only escape the path once
|
31
|
+
request = request_method(verb).new(path, headers)
|
32
|
+
ensure_content_type!(request)
|
33
|
+
add_user_agent!(request)
|
34
|
+
authenticate!(request)
|
35
|
+
if body
|
36
|
+
if body.respond_to?(:read)
|
37
|
+
request.body_stream = body
|
38
|
+
else
|
39
|
+
request.body = body
|
40
|
+
end
|
41
|
+
request.content_length = body.respond_to?(:lstat) ? body.stat.size : body.size
|
42
|
+
else
|
43
|
+
request.content_length = 0
|
44
|
+
end
|
45
|
+
http.request(request, &block)
|
46
|
+
end
|
47
|
+
|
48
|
+
if persistent?
|
49
|
+
http.start unless http.started?
|
50
|
+
requester.call
|
51
|
+
else
|
52
|
+
http.start(&requester)
|
53
|
+
end
|
54
|
+
rescue Errno::EPIPE, Timeout::Error, Errno::EINVAL, EOFError
|
55
|
+
@http = create_connection
|
56
|
+
attempts == 3 ? raise : (attempts += 1; retry)
|
57
|
+
end
|
58
|
+
|
59
|
+
def url_for(path, options = {})
|
60
|
+
authenticate = options.delete(:authenticated)
|
61
|
+
# Default to true unless explicitly false
|
62
|
+
authenticate = true if authenticate.nil?
|
63
|
+
path = self.class.prepare_path(path)
|
64
|
+
request = request_method(:get).new(path, {})
|
65
|
+
query_string = query_string_authentication(request, options)
|
66
|
+
returning "#{protocol(options)}#{http.address}#{port_string}#{path}" do |url|
|
67
|
+
url << "?#{query_string}" if authenticate
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def subdomain
|
72
|
+
http.address[/^([^.]+).#{DEFAULT_HOST}$/, 1]
|
73
|
+
end
|
74
|
+
|
75
|
+
def persistent?
|
76
|
+
options[:persistent]
|
77
|
+
end
|
78
|
+
|
79
|
+
def protocol(options = {})
|
80
|
+
# This always trumps http.use_ssl?
|
81
|
+
if options[:use_ssl] == false
|
82
|
+
'http://'
|
83
|
+
elsif options[:use_ssl] || http.use_ssl?
|
84
|
+
'https://'
|
85
|
+
else
|
86
|
+
'http://'
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
private
|
91
|
+
def extract_keys!
|
92
|
+
missing_keys = []
|
93
|
+
extract_key = Proc.new {|key| options[key] || (missing_keys.push(key); nil)}
|
94
|
+
@access_key_id = extract_key[:access_key_id]
|
95
|
+
@secret_access_key = extract_key[:secret_access_key]
|
96
|
+
raise MissingAccessKey.new(missing_keys) unless missing_keys.empty?
|
97
|
+
end
|
98
|
+
|
99
|
+
def create_connection
|
100
|
+
http = http_class.new(options[:server], options[:port])
|
101
|
+
http.use_ssl = !options[:use_ssl].nil? || options[:port] == 443
|
102
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
103
|
+
http
|
104
|
+
end
|
105
|
+
|
106
|
+
def http_class
|
107
|
+
if options.connecting_through_proxy?
|
108
|
+
Net::HTTP::Proxy(*options.proxy_settings)
|
109
|
+
else
|
110
|
+
Net::HTTP
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
def connect
|
115
|
+
extract_keys!
|
116
|
+
@http = create_connection
|
117
|
+
end
|
118
|
+
|
119
|
+
def port_string
|
120
|
+
default_port = options[:use_ssl] ? 443 : 80
|
121
|
+
http.port == default_port ? '' : ":#{http.port}"
|
122
|
+
end
|
123
|
+
|
124
|
+
def ensure_content_type!(request)
|
125
|
+
request['Content-Type'] ||= 'binary/octet-stream'
|
126
|
+
end
|
127
|
+
|
128
|
+
# Just do Header authentication for now
|
129
|
+
def authenticate!(request)
|
130
|
+
request['Authorization'] = Authentication::Header.new(request, access_key_id, secret_access_key)
|
131
|
+
end
|
132
|
+
|
133
|
+
def add_user_agent!(request)
|
134
|
+
request['User-Agent'] ||= "AWS::S3/#{Version}"
|
135
|
+
end
|
136
|
+
|
137
|
+
def query_string_authentication(request, options = {})
|
138
|
+
Authentication::QueryString.new(request, access_key_id, secret_access_key, options)
|
139
|
+
end
|
140
|
+
|
141
|
+
def request_method(verb)
|
142
|
+
Net::HTTP.const_get(verb.to_s.capitalize)
|
143
|
+
end
|
144
|
+
|
145
|
+
def method_missing(method, *args, &block)
|
146
|
+
options[method] || super
|
147
|
+
end
|
148
|
+
|
149
|
+
module Management #:nodoc:
|
150
|
+
def self.included(base)
|
151
|
+
base.cattr_accessor :connections
|
152
|
+
base.connections = {}
|
153
|
+
base.extend ClassMethods
|
154
|
+
end
|
155
|
+
|
156
|
+
# Manage the creation and destruction of connections for AWS::S3::Base and its subclasses. Connections are
|
157
|
+
# created with establish_connection!.
|
158
|
+
module ClassMethods
|
159
|
+
# Creates a new connection with which to make requests to the S3 servers for the calling class.
|
160
|
+
#
|
161
|
+
# AWS::S3::Base.establish_connection!(:access_key_id => '...', :secret_access_key => '...')
|
162
|
+
#
|
163
|
+
# You can set connections for every subclass of AWS::S3::Base. Once the initial connection is made on
|
164
|
+
# Base, all subsequent connections will inherit whatever values you don't specify explictly. This allows you to
|
165
|
+
# customize details of the connection, such as what server the requests are made to, by just specifying one
|
166
|
+
# option.
|
167
|
+
#
|
168
|
+
# AWS::S3::Bucket.established_connection!(:use_ssl => true)
|
169
|
+
#
|
170
|
+
# The Bucket connection would inherit the <tt>:access_key_id</tt> and the <tt>:secret_access_key</tt> from
|
171
|
+
# Base's connection. Unlike the Base connection, all Bucket requests would be made over SSL.
|
172
|
+
#
|
173
|
+
# == Required arguments
|
174
|
+
#
|
175
|
+
# * <tt>:access_key_id</tt> - The access key id for your S3 account. Provided by Amazon.
|
176
|
+
# * <tt>:secret_access_key</tt> - The secret access key for your S3 account. Provided by Amazon.
|
177
|
+
#
|
178
|
+
# If any of these required arguments is missing, a MissingAccessKey exception will be raised.
|
179
|
+
#
|
180
|
+
# == Optional arguments
|
181
|
+
#
|
182
|
+
# * <tt>:server</tt> - The server to make requests to. You can use this to specify your bucket in the subdomain,
|
183
|
+
# or your own domain's cname if you are using virtual hosted buckets. Defaults to <tt>s3.amazonaws.com</tt>.
|
184
|
+
# * <tt>:port</tt> - The port to the requests should be made on. Defaults to 80 or 443 if the <tt>:use_ssl</tt>
|
185
|
+
# argument is set.
|
186
|
+
# * <tt>:use_ssl</tt> - Whether requests should be made over SSL. If set to true, the <tt>:port</tt> argument
|
187
|
+
# will be implicitly set to 443, unless specified otherwise. Defaults to false.
|
188
|
+
# * <tt>:persistent</tt> - Whether to use a persistent connection to the server. Having this on provides around a two fold
|
189
|
+
# performance increase but for long running processes some firewalls may find the long lived connection suspicious and close the connection.
|
190
|
+
# If you run into connection errors, try setting <tt>:persistent</tt> to false. Defaults to false.
|
191
|
+
# * <tt>:proxy</tt> - If you need to connect through a proxy, you can specify your proxy settings by specifying a <tt>:host</tt>, <tt>:port</tt>, <tt>:user</tt>, and <tt>:password</tt>
|
192
|
+
# with the <tt>:proxy</tt> option.
|
193
|
+
# The <tt>:host</tt> setting is required if specifying a <tt>:proxy</tt>.
|
194
|
+
#
|
195
|
+
# AWS::S3::Bucket.established_connection!(:proxy => {
|
196
|
+
# :host => '...', :port => 8080, :user => 'marcel', :password => 'secret'
|
197
|
+
# })
|
198
|
+
def establish_connection!(options = {})
|
199
|
+
# After you've already established the default connection, just specify
|
200
|
+
# the difference for subsequent connections
|
201
|
+
options = default_connection.options.merge(options) if connected?
|
202
|
+
connections[connection_name] = Connection.connect(options)
|
203
|
+
end
|
204
|
+
|
205
|
+
# Returns the connection for the current class, or Base's default connection if the current class does not
|
206
|
+
# have its own connection.
|
207
|
+
#
|
208
|
+
# If not connection has been established yet, NoConnectionEstablished will be raised.
|
209
|
+
def connection
|
210
|
+
if connected?
|
211
|
+
connections[connection_name] || default_connection
|
212
|
+
else
|
213
|
+
raise NoConnectionEstablished
|
214
|
+
end
|
215
|
+
end
|
216
|
+
|
217
|
+
# Returns true if a connection has been made yet.
|
218
|
+
def connected?
|
219
|
+
!connections.empty?
|
220
|
+
end
|
221
|
+
|
222
|
+
# Removes the connection for the current class. If there is no connection for the current class, the default
|
223
|
+
# connection will be removed.
|
224
|
+
def disconnect(name = connection_name)
|
225
|
+
name = default_connection unless connections.has_key?(name)
|
226
|
+
connection = connections[name]
|
227
|
+
connection.http.finish if connection.persistent?
|
228
|
+
connections.delete(name)
|
229
|
+
end
|
230
|
+
|
231
|
+
# Clears *all* connections, from all classes, with prejudice.
|
232
|
+
def disconnect!
|
233
|
+
connections.each_key {|connection| disconnect(connection)}
|
234
|
+
end
|
235
|
+
|
236
|
+
private
|
237
|
+
def connection_name
|
238
|
+
name
|
239
|
+
end
|
240
|
+
|
241
|
+
def default_connection_name
|
242
|
+
'AWS::S3::Base'
|
243
|
+
end
|
244
|
+
|
245
|
+
def default_connection
|
246
|
+
connections[default_connection_name]
|
247
|
+
end
|
248
|
+
end
|
249
|
+
end
|
250
|
+
|
251
|
+
class Options < Hash #:nodoc:
|
252
|
+
class << self
|
253
|
+
def valid_options
|
254
|
+
[:access_key_id, :secret_access_key, :server, :port, :use_ssl, :persistent, :proxy]
|
255
|
+
end
|
256
|
+
end
|
257
|
+
|
258
|
+
attr_reader :options
|
259
|
+
def initialize(options = {})
|
260
|
+
super()
|
261
|
+
@options = options
|
262
|
+
validate!
|
263
|
+
extract_proxy_settings!
|
264
|
+
extract_persistent!
|
265
|
+
extract_server!
|
266
|
+
extract_port!
|
267
|
+
extract_remainder!
|
268
|
+
end
|
269
|
+
|
270
|
+
def connecting_through_proxy?
|
271
|
+
!self[:proxy].nil?
|
272
|
+
end
|
273
|
+
|
274
|
+
def proxy_settings
|
275
|
+
proxy_setting_keys.map do |proxy_key|
|
276
|
+
self[:proxy][proxy_key]
|
277
|
+
end
|
278
|
+
end
|
279
|
+
|
280
|
+
private
|
281
|
+
def proxy_setting_keys
|
282
|
+
[:host, :port, :user, :password]
|
283
|
+
end
|
284
|
+
|
285
|
+
def missing_proxy_settings?
|
286
|
+
!self[:proxy].keys.include?(:host)
|
287
|
+
end
|
288
|
+
|
289
|
+
def extract_persistent!
|
290
|
+
self[:persistent] = options.has_key?(:persitent) ? options[:persitent] : false
|
291
|
+
end
|
292
|
+
|
293
|
+
def extract_proxy_settings!
|
294
|
+
self[:proxy] = options.delete(:proxy) if options.include?(:proxy)
|
295
|
+
validate_proxy_settings!
|
296
|
+
end
|
297
|
+
|
298
|
+
def extract_server!
|
299
|
+
self[:server] = options.delete(:server) || DEFAULT_HOST
|
300
|
+
end
|
301
|
+
|
302
|
+
def extract_port!
|
303
|
+
self[:port] = options.delete(:port) || (options[:use_ssl] ? 443 : 80)
|
304
|
+
end
|
305
|
+
|
306
|
+
def extract_remainder!
|
307
|
+
update(options)
|
308
|
+
end
|
309
|
+
|
310
|
+
def validate!
|
311
|
+
invalid_options = options.keys.select {|key| !self.class.valid_options.include?(key)}
|
312
|
+
raise InvalidConnectionOption.new(invalid_options) unless invalid_options.empty?
|
313
|
+
end
|
314
|
+
|
315
|
+
def validate_proxy_settings!
|
316
|
+
if connecting_through_proxy? && missing_proxy_settings?
|
317
|
+
raise ArgumentError, "Missing proxy settings. Must specify at least :host."
|
318
|
+
end
|
319
|
+
end
|
320
|
+
end
|
321
|
+
end
|
322
|
+
end
|
323
|
+
end
|