class AWS::S3::Request

@private

Attributes

body_stream[RW]
bucket[RW]

@param [bucket] S3 bucket name

key[RW]

@param [String] S3 object key

Public Class Methods

query_parameters() click to toggle source
# File lib/aws/s3/request.rb, line 190
def query_parameters
  %w(response-content-type response-content-language
     response-expires response-cache-control
     response-content-disposition response-content-encoding)
end
sub_resources() click to toggle source
# File lib/aws/s3/request.rb, line 184
def sub_resources
  %w(acl location logging notification partNumber policy 
     requestPayment torrent uploadId uploads versionId 
     versioning versions delete lifecycle)
end

Public Instance Methods

add_authorization!(signer) click to toggle source
# File lib/aws/s3/request.rb, line 173
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
body() click to toggle source

@return [String, nil] The http request body.

# File lib/aws/s3/request.rb, line 77
def body
  if @body_stream
    string = @body_stream.read
    @body_stream.rewind
    string
  else
    nil
  end
end
body=(body) click to toggle source

@param [String, IO] The http request body. This can be a string or

any object that responds to #read and #eof? (like an IO object).
# File lib/aws/s3/request.rb, line 72
def body= body
  @body_stream = StringIO.new(body)
end
canned_acl=(acl) click to toggle source
# File lib/aws/s3/request.rb, line 39
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_headers() click to toggle source

CanonicalizedAmzHeaders

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

# File lib/aws/s3/request.rb, line 157
def canonicalized_headers
  x_amz = headers.select{|name, value| name.to_s =~ %r^x-amz-/ }
  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_resource() click to toggle source

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"];
# File lib/aws/s3/request.rb, line 124
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
host() click to toggle source
# File lib/aws/s3/request.rb, line 55
def host
  Client.path_style_bucket_name?(bucket) ? @host : "#{bucket}.#{@host}"
end
metadata=(metadata) click to toggle source
# File lib/aws/s3/request.rb, line 33
def metadata= metadata
  Array(metadata).each do |name, value|
    headers["x-amz-meta-#{name}"] = value
  end
end
path() click to toggle source
# File lib/aws/s3/request.rb, line 59
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_signature() click to toggle source
# File lib/aws/s3/request.rb, line 169
def query_parameters_for_signature
  params.select { |p| self.class.query_parameters.include?(p.name) }
end
querystring() click to toggle source
# File lib/aws/s3/request.rb, line 66
def querystring
  url_encoded_params
end
signing_string_date() click to toggle source
# File lib/aws/s3/request.rb, line 106
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 =~ %r^x-amz-date$/ }
    ''
  else
    headers['date'] ||= Time.now.rfc822
  end
end
storage_class=(storage_class) click to toggle source
# File lib/aws/s3/request.rb, line 47
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_sign() click to toggle source

From the S3 developer guide:

StringToSign =

HTTP-Verb + "\n" + 
content-md5 + "\n" + 
content-type + "\n" + 
date + "\n" + 
CanonicalizedAmzHeaders + CanonicalizedResource;
# File lib/aws/s3/request.rb, line 96
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_params() click to toggle source
# File lib/aws/s3/request.rb, line 165
def sub_resource_params
  params.select{|p| self.class.sub_resources.include?(p.name) }
end