Class: S3::Connection
Overview
Class responsible for handling connections to amazon hosts
Instance Attribute Summary collapse
-
#access_key_id ⇒ Object
Returns the value of attribute access_key_id.
-
#debug ⇒ Object
Returns the value of attribute debug.
-
#proxy ⇒ Object
Returns the value of attribute proxy.
-
#secret_access_key ⇒ Object
Returns the value of attribute secret_access_key.
-
#timeout ⇒ Object
Returns the value of attribute timeout.
-
#use_ssl ⇒ Object
(also: #use_ssl?)
Returns the value of attribute use_ssl.
Class Method Summary collapse
-
.parse_headers(headers) ⇒ Object
Helper function to change headers from symbols, to in correct form (i.e. with ‘-’ instead of ‘_’).
-
.parse_params(params) ⇒ Object
Helper function to parser parameters and create single string of params added to questy string.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Connection
constructor
Creates new connection object.
-
#request(method, options) ⇒ Object
Makes request with given HTTP method, sets missing parameters, adds signature to request header and returns response object (Net::HTTPResponse).
Methods included from Parser
#parse_copy_object_result, #parse_error, #parse_list_all_my_buckets_result, #parse_list_bucket_result, #parse_location_constraint, #rexml_document
Constructor Details
#initialize(options = {}) ⇒ Connection
Creates new connection object.
Options
-
:access_key_id
- Access key id (REQUIRED) -
:secret_access_key
- Secret access key (REQUIRED) -
:use_ssl
- Use https or http protocol (false by default) -
:debug
- Display debug information on the STDOUT (false by default) -
:timeout
- Timeout to use by the Net::HTTP object (60 by default) -
:proxy
- Hash for Net::HTTP Proxy settings { :host => “proxy.mydomain.com”, :port => “80, :user => ”user_a“, :password => ”secret“ } -
:proxy
- Hash for Net::HTTP Proxy settings
24 25 26 27 28 29 30 31 |
# File 'lib/s3/connection.rb', line 24 def initialize( = {}) @access_key_id = .fetch(:access_key_id) @secret_access_key = .fetch(:secret_access_key) @use_ssl = .fetch(:use_ssl, false) @debug = .fetch(:debug, false) @timeout = .fetch(:timeout, 60) @proxy = .fetch(:proxy, nil) end |
Instance Attribute Details
#access_key_id ⇒ Object
Returns the value of attribute access_key_id.
7 8 9 |
# File 'lib/s3/connection.rb', line 7 def access_key_id @access_key_id end |
#debug ⇒ Object
Returns the value of attribute debug.
7 8 9 |
# File 'lib/s3/connection.rb', line 7 def debug @debug end |
#proxy ⇒ Object
Returns the value of attribute proxy.
7 8 9 |
# File 'lib/s3/connection.rb', line 7 def proxy @proxy end |
#secret_access_key ⇒ Object
Returns the value of attribute secret_access_key.
7 8 9 |
# File 'lib/s3/connection.rb', line 7 def secret_access_key @secret_access_key end |
#timeout ⇒ Object
Returns the value of attribute timeout.
7 8 9 |
# File 'lib/s3/connection.rb', line 7 def timeout @timeout end |
#use_ssl ⇒ Object Also known as: use_ssl?
Returns the value of attribute use_ssl.
7 8 9 |
# File 'lib/s3/connection.rb', line 7 def use_ssl @use_ssl end |
Class Method Details
.parse_headers(headers) ⇒ Object
Helper function to change headers from symbols, to in correct form (i.e. with ‘-’ instead of ‘_’)
Parameters
-
headers
- Hash of pairsheadername => value
, where value can be Range (for Range header) or any other value which can be translated to string
Returns
Hash of headers translated from symbol to string, containing only interesting headers
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/s3/connection.rb', line 124 def self.parse_headers(headers) interesting_keys = [:content_type, :x_amz_acl, :x_amz_storage_class, :range, :if_modified_since, :if_unmodified_since, :if_match, :if_none_match, :content_disposition, :content_encoding, :x_amz_copy_source, :x_amz_metadata_directive, :x_amz_copy_source_if_match, :x_amz_copy_source_if_none_match, :x_amz_copy_source_if_unmodified_since, :x_amz_copy_source_if_modified_since] parsed_headers = {} if headers headers.each do |key, value| if interesting_keys.include?(key) parsed_key = key.to_s.gsub("_", "-") parsed_value = value case value when Range parsed_value = "bytes=#{value.first}-#{value.last}" end parsed_headers[parsed_key] = parsed_value end end end parsed_headers end |
.parse_params(params) ⇒ Object
Helper function to parser parameters and create single string of params added to questy string
Parameters
-
params
- Hash of parameters
Returns
String – containing all parameters joined in one params string, i.e. param1=val¶m2¶m3=0
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/s3/connection.rb', line 95 def self.parse_params(params) interesting_keys = [:max_keys, :prefix, :marker, :delimiter, :location] result = [] params.each do |key, value| if interesting_keys.include?(key) parsed_key = key.to_s.gsub("_", "-") case value when nil result << parsed_key else result << "#{parsed_key}=#{value}" end end end result.join("&") end |
Instance Method Details
#request(method, options) ⇒ Object
Makes request with given HTTP method, sets missing parameters, adds signature to request header and returns response object (Net::HTTPResponse)
Parameters
-
method
- HTTP Method symbol, can be:get
,:put
,:delete
Options:
-
:host
- Hostname to connecto to, defaults tos3.amazonaws.com
-
:path
- path to send request to (REQUIRED) -
:body
- Request body, only meaningful for:put
request -
:params
- Parameters to add to query string for request, can be String or Hash -
:headers
- Hash of headers fields to add to request header
Returns
Net::HTTPResponse object – response from the server
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/s3/connection.rb', line 54 def request(method, ) host = .fetch(:host, HOST) path = .fetch(:path) body = .fetch(:body, nil) params = .fetch(:params, {}) headers = .fetch(:headers, {}) if params params = params.is_a?(String) ? params : self.class.parse_params(params) path << "?#{params}" end path = URI.escape(path) request = Net::HTTPGenericRequest.new(method.to_s.upcase, !!body, true, path) headers = self.class.parse_headers(headers) headers.each do |key, value| request[key] = value end 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 end send_request(host, request) end |