Class: AWS::S3::Request

Inherits:
Core::Http::Request show all
Includes:
Core::UriEscape
Defined in:
lib/aws/s3/request.rb

Instance Attribute Summary collapse

Attributes inherited from Core::Http::Request

#access_key_id, #headers, #http_method, #params, #proxy_uri, #read_timeout, #region, #service_ruby_name

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Core::UriEscape

#escape, #escape_path

Methods inherited from Core::Http::Request

#add_param, #get_param, #initialize, #param_value_for, #port, #port=, #ssl_ca_file, #ssl_ca_file=, #ssl_ca_path, #ssl_ca_path=, #ssl_verify_peer=, #ssl_verify_peer?, #uri, #url_encoded_params, #use_ssl=, #use_ssl?

Constructor Details

This class inherits a constructor from AWS::Core::Http::Request

Instance Attribute Details

#body_streamObject



32
33
34
# File 'lib/aws/s3/request.rb', line 32

def body_stream
  @body_stream
end

#bucketbucket

Returns S3 bucket name.

Returns:



26
27
28
# File 'lib/aws/s3/request.rb', line 26

def bucket
  @bucket
end

#keyString

Returns S3 object key.

Returns:

  • (String)

    S3 object key



29
30
31
# File 'lib/aws/s3/request.rb', line 29

def key
  @key
end

Class Method Details

.query_parametersObject



191
192
193
194
195
# File 'lib/aws/s3/request.rb', line 191

def query_parameters
  %w(response-content-type response-content-language
     response-expires response-cache-control
     response-content-disposition response-content-encoding)
end

.sub_resourcesObject



185
186
187
188
189
# File 'lib/aws/s3/request.rb', line 185

def sub_resources
  %w(acl location logging notification partNumber policy 
     requestPayment torrent uploadId uploads versionId 
     versioning versions delete lifecycle)
end

Instance Method Details

#add_authorization!(signer) ⇒ Object



174
175
176
177
178
179
180
181
# File 'lib/aws/s3/request.rb', line 174

def add_authorization!(signer)
  if signer.respond_to?(:session_token) and
      token = signer.session_token
    headers["x-amz-security-token"] = token
  end
  signature = URI.escape(signer.sign(string_to_sign, 'sha1'))
  headers["authorization"] = "AWS #{signer.access_key_id}:#{signature}"
end

#bodyString?

Returns The http request body.

Returns:

  • (String, nil)

    The http request body.



78
79
80
81
82
83
84
85
86
# File 'lib/aws/s3/request.rb', line 78

def body
  if @body_stream
    string = @body_stream.read
    @body_stream.rewind
    string
  else
    nil
  end
end

#body=(body) ⇒ Object

Parameters:

  • body (String, IO)

    The http request body. This can be a string or any object that responds to #read and #eof? (like an IO object).



73
74
75
# File 'lib/aws/s3/request.rb', line 73

def body= body
  @body_stream = StringIO.new(body)
end

#canned_acl=(acl) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/aws/s3/request.rb', line 40

def canned_acl= acl
  if acl.kind_of?(Symbol)
    headers["x-amz-acl"] = acl.to_s.gsub("_", "-")
  elsif acl
    headers["x-amz-acl"] = acl
  end
end

#canonicalized_headersObject

CanonicalizedAmzHeaders

See the developer guide for more information on how this element is generated.



158
159
160
161
162
163
164
# File 'lib/aws/s3/request.rb', line 158

def canonicalized_headers
  x_amz = headers.select{|name, value| name.to_s =~ /^x-amz-/i }
  x_amz = x_amz.collect{|name, value| [name.downcase, value] }
  x_amz = x_amz.sort_by{|name, value| name }
  x_amz = x_amz.collect{|name, value| "#{name}:#{value}" }.join("\n")
  x_amz == '' ? nil : x_amz
end

#canonicalized_resourceObject

From the S3 developer guide

CanonicalizedResource = 
  [ "/" + Bucket ] + 
  <HTTP-Request-URI, from the protocol name up to the querystring> +
  [ sub-resource, if present. e.g. "?acl", "?location", 
  "?logging", or "?torrent"];


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
151
# File 'lib/aws/s3/request.rb', line 125

def canonicalized_resource

  parts = []

  # virtual hosted-style requests require the hostname to appear
  # in the canonicalized resource prefixed by a forward slash.
  if 
    Client.dns_compatible_bucket_name?(bucket) and
    !Client.path_style_bucket_name?(bucket)
  then
    parts << "/#{bucket}"
  end
  
  # all requests require the portion of the un-decoded uri up to
  # but not including the query string
  parts << path
 
  # lastly any sub resource querystring params need to be appened
  # in lexigraphical ordered joined by '&' and prefixed by '?'
  params = (sub_resource_params + query_parameters_for_signature)
  unless params.empty?
    parts << '?'
    parts << params.sort.collect{|p| p.to_s }.join('&')
  end

  parts.join
end

#hostObject



56
57
58
# File 'lib/aws/s3/request.rb', line 56

def host
  Client.path_style_bucket_name?(bucket) ? @host : "#{bucket}.#{@host}"
end

#metadata=(metadata) ⇒ Object



34
35
36
37
38
# File 'lib/aws/s3/request.rb', line 34

def metadata= 
  Array().each do |name, value|
    headers["x-amz-meta-#{name}"] = value
  end
end

#pathObject



60
61
62
63
64
65
# File 'lib/aws/s3/request.rb', line 60

def path
  parts = []
  parts << bucket if bucket and Client.path_style_bucket_name?(bucket)
  parts << escape_path(key) if key
  "/#{parts.join('/')}"
end

#query_parameters_for_signatureObject



170
171
172
# File 'lib/aws/s3/request.rb', line 170

def query_parameters_for_signature
  params.select { |p| self.class.query_parameters.include?(p.name) }
end

#querystringObject



67
68
69
# File 'lib/aws/s3/request.rb', line 67

def querystring
  url_encoded_params
end

#signing_string_dateObject



107
108
109
110
111
112
113
114
115
# File 'lib/aws/s3/request.rb', line 107

def signing_string_date
  # if a date is provided via x-amz-date then we should omit the
  # Date header from the signing string (should appear as a blank line)
  if headers.detect{|k,v| k.to_s =~ /^x-amz-date$/i }
    ''
  else
    headers['date'] ||= Time.now.rfc822
  end
end

#storage_class=(storage_class) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/aws/s3/request.rb', line 48

def storage_class= storage_class
  if storage_class.kind_of?(Symbol)
    headers["x-amz-storage-class"] = storage_class.to_s.upcase
  elsif storage_class
    headers["x-amz-storage-class"] = storage_class
  end
end

#string_to_signObject

From the S3 developer guide:

StringToSign =

HTTP-Verb + "\n" + 
content-md5 + "\n" + 
content-type + "\n" + 
date + "\n" + 
CanonicalizedAmzHeaders + CanonicalizedResource;


97
98
99
100
101
102
103
104
105
# File 'lib/aws/s3/request.rb', line 97

def string_to_sign
  [
    http_method,
    headers.values_at('content-md5', 'content-type').join("\n"),
    signing_string_date,
    canonicalized_headers,
    canonicalized_resource,
  ].flatten.compact.join("\n")
end

#sub_resource_paramsObject



166
167
168
# File 'lib/aws/s3/request.rb', line 166

def sub_resource_params
  params.select{|p| self.class.sub_resources.include?(p.name) }
end