Module: S3
- Defined in:
- lib/s3-ruby.rb
Overview
this module has two big classes: AWSAuthConnection and QueryStringAuthGenerator. both use identical apis, but the first actually performs the operation, while the second simply outputs urls with the appropriate authentication query string parameters, which could be used in another tool (such as your web browser for GETs).
Defined Under Namespace
Classes: AWSAuthConnection, Bucket, CommonPrefixEntry, GetResponse, ListAllMyBucketsParser, ListAllMyBucketsResponse, ListBucketParser, ListBucketResponse, ListEntry, ListProperties, Owner, QueryStringAuthGenerator, Response, RubyBindings, S3Object
Constant Summary collapse
- DEFAULT_HOST =
's3.amazonaws.com'
- PORTS_BY_SECURITY =
{ true => 443, false => 80 }
- METADATA_PREFIX =
'x-amz-meta-'
- AMAZON_HEADER_PREFIX =
'x-amz-'
Class Method Summary collapse
-
.canonical_string(method, path, headers = {}, expires = nil) ⇒ Object
builds the canonical string for signing.
-
.encode(aws_secret_access_key, str, urlencode = false) ⇒ Object
encodes the given string with the aws_secret_access_key, by taking the hmac-sha1 sum, and then base64 encoding it.
Class Method Details
.canonical_string(method, path, headers = {}, expires = nil) ⇒ Object
builds the canonical string for signing.
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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/s3-ruby.rb', line 56 def S3.canonical_string(method, path, headers={}, expires=nil) interesting_headers = {} headers.each do |key, value| lk = key.downcase if (lk == 'content-md5' or lk == 'content-type' or lk == 'date' or lk =~ /^#{AMAZON_HEADER_PREFIX}/o) interesting_headers[lk] = value.to_s.strip end end # these fields get empty strings if they don't exist. interesting_headers['content-type'] ||= '' interesting_headers['content-md5'] ||= '' # just in case someone used this. it's not necessary in this lib. if interesting_headers.has_key? 'x-amz-date' interesting_headers['date'] = '' end # if you're using expires for query string auth, then it trumps date # (and x-amz-date) if not expires.nil? interesting_headers['date'] = expires end buf = "#{method}\n" interesting_headers.sort { |a, b| a[0] <=> b[0] }.each do |key, value| if key =~ /^#{AMAZON_HEADER_PREFIX}/o buf << "#{key}:#{value}\n" else buf << "#{value}\n" end end # ignore everything after the question mark... buf << path.gsub(/\?.*$/, '') # ...unless there is an acl or torrent parameter if path =~ /[&?]acl($|&|=)/ buf << '?acl' elsif path =~ /[&?]torrent($|&|=)/ buf << '?torrent' elsif path =~ /[&?]logging($|&|=)/ buf << '?logging' end return buf end |
.encode(aws_secret_access_key, str, urlencode = false) ⇒ Object
encodes the given string with the aws_secret_access_key, by taking the hmac-sha1 sum, and then base64 encoding it. optionally, it will also url encode the result of that to protect the string if it’s going to be used as a query string parameter.
111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/s3-ruby.rb', line 111 def S3.encode(aws_secret_access_key, str, urlencode=false) digest = OpenSSL::Digest::Digest.new('sha1') b64_hmac = Base64.encode64( OpenSSL::HMAC.digest(digest, aws_secret_access_key, str)).strip if urlencode return CGI::escape(b64_hmac) else return b64_hmac end end |