Class: AWS::S3::Connection
- Inherits:
-
Object
- Object
- AWS::S3::Connection
- Defined in:
- lib/aws/s3/connection.rb
Overview
:nodoc:
Defined Under Namespace
Modules: Management Classes: Options
Instance Attribute Summary collapse
-
#access_key_id ⇒ Object
readonly
Returns the value of attribute access_key_id.
-
#http ⇒ Object
readonly
Returns the value of attribute http.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#secret_access_key ⇒ Object
readonly
Returns the value of attribute secret_access_key.
Class Method Summary collapse
- .connect(options = {}) ⇒ Object
-
.prepare_path(path) ⇒ Object
Added the ability to remove the bucket from the path.
Instance Method Summary collapse
- #has_subdomain? ⇒ Boolean
-
#initialize(options = {}) ⇒ Connection
constructor
Creates a new connection.
- #is_for_bucket?(bucket) ⇒ Boolean
- #is_using_default_host? ⇒ Boolean
- #persistent? ⇒ Boolean
- #protocol(options = {}) ⇒ Object
- #request(verb, path, headers = {}, body = nil, attempts = 0, &block) ⇒ Object
- #subdomain ⇒ Object
- #url_for(path, options = {}) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Connection
Creates a new connection. Connections make the actual requests to S3, though these requests are usually called from subclasses of Base.
For details on establishing connections, check the Connection::Management::ClassMethods.
22 23 24 25 |
# File 'lib/aws/s3/connection.rb', line 22 def initialize( = {}) = Options.new() connect end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object (private)
170 171 172 |
# File 'lib/aws/s3/connection.rb', line 170 def method_missing(method, *args, &block) [method] || super end |
Instance Attribute Details
#access_key_id ⇒ Object (readonly)
Returns the value of attribute access_key_id.
16 17 18 |
# File 'lib/aws/s3/connection.rb', line 16 def access_key_id @access_key_id end |
#http ⇒ Object (readonly)
Returns the value of attribute http.
16 17 18 |
# File 'lib/aws/s3/connection.rb', line 16 def http @http end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
16 17 18 |
# File 'lib/aws/s3/connection.rb', line 16 def end |
#secret_access_key ⇒ Object (readonly)
Returns the value of attribute secret_access_key.
16 17 18 |
# File 'lib/aws/s3/connection.rb', line 16 def secret_access_key @secret_access_key end |
Class Method Details
.connect(options = {}) ⇒ Object
5 6 7 |
# File 'lib/aws/s3/connection.rb', line 5 def connect( = {}) new() end |
.prepare_path(path) ⇒ Object
Added the ability to remove the bucket from the path.
10 11 12 13 |
# File 'lib/aws/s3/connection.rb', line 10 def prepare_path(path) path = path.remove_extended unless path.utf8? URI.escape(path) end |
Instance Method Details
#has_subdomain? ⇒ Boolean
105 106 107 |
# File 'lib/aws/s3/connection.rb', line 105 def has_subdomain? !subdomain.nil? end |
#is_for_bucket?(bucket) ⇒ Boolean
101 102 103 |
# File 'lib/aws/s3/connection.rb', line 101 def is_for_bucket?(bucket) bucket == subdomain end |
#is_using_default_host? ⇒ Boolean
109 110 111 |
# File 'lib/aws/s3/connection.rb', line 109 def is_using_default_host? http.address == DEFAULT_HOST || self.has_subdomain? end |
#persistent? ⇒ Boolean
85 86 87 |
# File 'lib/aws/s3/connection.rb', line 85 def persistent? [:persistent] end |
#protocol(options = {}) ⇒ Object
89 90 91 92 93 94 95 96 97 98 |
# File 'lib/aws/s3/connection.rb', line 89 def protocol( = {}) # This always trumps http.use_ssl? if [:use_ssl] == false 'http://' elsif [:use_ssl] || http.use_ssl? 'https://' else 'http://' end end |
#request(verb, path, headers = {}, body = nil, attempts = 0, &block) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/aws/s3/connection.rb', line 27 def request(verb, path, headers = {}, body = nil, attempts = 0, &block) body.rewind if body.respond_to?(:rewind) unless attempts.zero? requester = Proc.new do path = self.class.prepare_path(path) if attempts.zero? # Only escape the path once request = request_method(verb).new(path, headers) ensure_content_type!(request) add_user_agent!(request) authenticate!(request, { :bucket_address => self.subdomain }) if body if body.respond_to?(:read) request.body_stream = body else request.body = body end request.content_length = body.respond_to?(:lstat) ? body.stat.size : body.size else request.content_length = 0 end http.request(request, &block) end if persistent? http.start unless http.started? requester.call else http.start(&requester) end rescue Errno::EPIPE, Timeout::Error, Errno::EINVAL, EOFError @http = create_connection attempts == 3 ? raise : (attempts += 1; retry) end |
#subdomain ⇒ Object
81 82 83 |
# File 'lib/aws/s3/connection.rb', line 81 def subdomain http.address[/^([^.]+).#{DEFAULT_HOST}$/, 1] end |
#url_for(path, options = {}) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/aws/s3/connection.rb', line 62 def url_for(path, = {}) authenticate = .delete(:authenticated) # Default to true unless explicitly false authenticate = true if authenticate.nil? if self.is_using_default_host? bucket_name = AWS::S3.remove_or_extract_bucket_from_path(path, true) address = "#{bucket_name}.#{DEFAULT_HOST}" path = self.class.prepare_path(AWS::S3.remove_or_extract_bucket_from_path(path)) else address = http.address path = self.class.prepare_path(path) end request = request_method(:get).new(path, {}) query_string = query_string_authentication(request, .merge({ :bucket_address => bucket_name })) returning "#{protocol(options)}#{address}#{port_string}#{path}" do |url| url << "?#{query_string}" if authenticate end end |