1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00

use hash for s3 request params

This commit is contained in:
Wesley Beary 2009-06-23 20:55:57 -07:00
parent 901630bfe7
commit d2ab5aa300
2 changed files with 141 additions and 88 deletions

44
benchs/params.rb Normal file
View file

@ -0,0 +1,44 @@
require 'rubygems'
require 'benchwarmer'
def hash(options)
result = "#{options.delete(:name)}"
for key, value in options
result << " #{key} => #{value} "
end
result
end
def optional(name, a = nil, b = nil, c = nil)
result = "#{name}"
options = { :a => a, :b => b, :c => c }
for key, value in options
result << " #{key} => #{value} "
end
result
end
COUNT = 100_000
data = "Content-Length: 100"
Benchmark.bmbm(25) do |bench|
bench.report('hash') do
COUNT.times do
hash({:name => 'name'})
end
end
bench.report('optional') do
COUNT.times do
optional('name')
end
end
bench.report('hash_with_option') do
COUNT.times do
hash({:name => 'name', :a => 'a', :b => 'b', :c => 'c'})
end
end
bench.report('optional_with_option') do
COUNT.times do
optional('name', :a => 'a', :b => 'b', :c => 'c')
end
end
end

View file

@ -41,11 +41,12 @@ module Fog
# List information about S3 buckets for authorized user # List information about S3 buckets for authorized user
def get_service def get_service
request( request({
'GET', :headers => {},
url, :method => 'GET',
Fog::Parsers::AWS::S3::GetServiceParser.new :parser => Fog::Parsers::AWS::S3::GetServiceParser.new,
) :url => url
})
end end
# Create an S3 bucket # Create an S3 bucket
@ -64,13 +65,13 @@ module Fog
else else
data = nil data = nil
end end
request( request({
'PUT', :body => data,
url(bucket_name), :headers => {},
Fog::Parsers::AWS::S3::BasicParser.new, :method => 'PUT',
{}, :parser => Fog::Parsers::AWS::S3::BasicParser.new,
data :url => url(bucket_name)
) })
end end
# Change who pays for requests to an S3 bucket # Change who pays for requests to an S3 bucket
@ -84,13 +85,13 @@ module Fog
<Payer>#{payer}</Payer> <Payer>#{payer}</Payer>
</RequestPaymentConfiguration> </RequestPaymentConfiguration>
DATA DATA
request( request({
'PUT', :body => data,
url(bucket_name), :headers => {},
Fog::Parsers::AWS::S3::BasicParser.new, :method => 'PUT',
{}, :parser => Fog::Parsers::AWS::S3::BasicParser.new,
data :url => url(bucket_name)
) })
end end
# List information about objects in an S3 bucket # List information about objects in an S3 bucket
@ -107,33 +108,36 @@ module Fog
def get_bucket(bucket_name, options = {}) def get_bucket(bucket_name, options = {})
options['max-keys'] = options.delete(:maxkeys) if options[:maxkeys] options['max-keys'] = options.delete(:maxkeys) if options[:maxkeys]
query = '?' query = '?'
options.each do |key, value| for key, value in options
query << "#{key}=#{value};" query << "#{key}=#{value};"
end end
query.chop! query.chop!
request( request({
'GET', :headers => {},
url(bucket_name, query), :method => 'GET',
Fog::Parsers::AWS::S3::GetBucketParser.new :parser => Fog::Parsers::AWS::S3::GetBucketParser.new,
) :url => url(bucket_name, query)
})
end end
# Get configured payer for an S3 bucket # Get configured payer for an S3 bucket
def get_request_payment(bucket_name) def get_request_payment(bucket_name)
request( request({
'GET', :headers => {},
url(bucket_name, '?requestpayment'), :method => 'GET',
Fog::Parsers::AWS::S3::GetRequestPayment.new :parser => Fog::Parsers::AWS::S3::GetRequestPayment.new,
) :url => url(bucket_name, '?requestpayment')
})
end end
# Get location constraint for an S3 bucket # Get location constraint for an S3 bucket
def get_location(bucket_name) def get_location(bucket_name)
request( request({
'GET', :headers => {},
url(bucket_name, '?location'), :method => 'GET',
Fog::Parsers::AWS::S3::GetRequestPayment.new :parser => Fog::Parsers::AWS::S3::GetRequestPayment.new,
) :url => url(bucket_name, '?location')
})
end end
# Delete an S3 bucket # Delete an S3 bucket
@ -141,60 +145,63 @@ module Fog
# ==== Parameters # ==== Parameters
# bucket_name<~String>:: name of bucket to delete # bucket_name<~String>:: name of bucket to delete
def delete_bucket(bucket_name) def delete_bucket(bucket_name)
request( request({
'DELETE', :headers => {},
url(bucket_name), :method => 'DELETE',
Fog::Parsers::AWS::S3::BasicParser.new :parser => Fog::Parsers::AWS::S3::BasicParser.new,
) :url => url(bucket_name)
})
end end
# Create an object in an S3 bucket # Create an object in an S3 bucket
def put_object(bucket_name, object_name, object, options = {}) def put_object(bucket_name, object_name, object, options = {})
file = parse_file(object) file = parse_file(object)
request( request({
'PUT', :body => file[:body],
url(bucket_name, object_name), :headers => options.merge!(file[:headers]),
Fog::Parsers::AWS::S3::BasicParser.new, :method => 'PUT',
options.merge!(file[:headers]), :parser => Fog::Parsers::AWS::S3::BasicParser.new,
file[:body] :url => url(bucket_name, object_name)
) })
end end
# Copy an object from one S3 bucket to another # Copy an object from one S3 bucket to another
def copy_object(source_bucket_name, source_object_name, destination_bucket_name, destination_object_name) def copy_object(source_bucket_name, source_object_name, destination_bucket_name, destination_object_name)
request( request({
'PUT', :headers => { 'x-amz-copy-source' => "/#{source_bucket_name}/#{source_object_name}" },
url(destination_bucket_name, destination_object_name), :method => 'PUT',
Fog::Parsers::AWS::S3::BasicParser.new, :parser => Fog::Parsers::AWS::S3::BasicParser.new,
{ 'x-amz-copy-source' => "/#{source_bucket_name}/#{source_object_name}" } :url => url(destination_bucket_name, destination_object_name)
) })
end end
# Get an object from S3 # Get an object from S3
def get_object(bucket_name, object_name) def get_object(bucket_name, object_name)
request( request({
'GET', :headers => {},
url(bucket_name, object_name), :method => 'GET',
nil :url => url(bucket_name, object_name)
) })
end end
# Get headers for an object from S3 # Get headers for an object from S3
def head_object(bucket_name, object_name) def head_object(bucket_name, object_name)
request( request({
'HEAD', :headers => {},
url(bucket_name, object_name), :method => 'HEAD',
Fog::Parsers::AWS::S3::BasicParser.new :parser => Fog::Parsers::AWS::S3::BasicParser.new,
) :url => url(bucket_name, object_name)
})
end end
# Delete an object from S3 # Delete an object from S3
def delete_object(bucket_name, object_name) def delete_object(bucket_name, object_name)
request( request({
'DELETE', :headers => {},
url(bucket_name, object_name), :method => 'DELETE',
Fog::Parsers::AWS::S3::BasicParser.new :parser => Fog::Parsers::AWS::S3::BasicParser.new,
) :url => url(bucket_name, object_name)
})
end end
private private
@ -226,8 +233,10 @@ module Fog
def canonicalize_resource(uri) def canonicalize_resource(uri)
resource = "/" resource = "/"
if match = uri.host.match(/(.*).s3.amazonaws.com/) # [0..-18] is anything prior to .s3.amazonaws.com
resource << "#{match[1]}/" subdomain = uri.host[0..-18]
unless subdomain.empty?
resource << "#{subdomain}/"
end end
resource << "#{uri.path[1..-1]}" resource << "#{uri.path[1..-1]}"
# resource << "?acl" if uri.to_s.include?('?acl') # resource << "?acl" if uri.to_s.include?('?acl')
@ -253,37 +262,37 @@ module Fog
metadata metadata
end end
def request(method, url, parser, headers = {}, body = nil) def request(params)
uri = URI.parse(url) uri = URI.parse(params[:url])
headers['Date'] = Time.now.utc.strftime("%a, %d %b %Y %H:%M:%S +0000") params[:headers]['Date'] = Time.now.utc.strftime("%a, %d %b %Y %H:%M:%S +0000")
params = [ params_to_sign = [
method, params[:method],
content_md5 = headers['Content-MD5'] || '', content_md5 = params[:headers]['Content-MD5'] || '',
content_type = headers['Content-Type'] || '', content_type = params[:headers]['Content-Type'] || '',
headers['Date'], params[:headers]['Date'],
canonicalized_amz_headers = canonicalize_amz_headers(headers), canonicalized_amz_headers = canonicalize_amz_headers(params[:headers]),
canonicalized_resource = canonicalize_resource(uri) canonicalized_resource = canonicalize_resource(uri)
] ]
string_to_sign = '' string_to_sign = ''
for value in params for value in params_to_sign
unless value.nil? unless value.nil?
string_to_sign << "#{value}\n" string_to_sign << "#{value}\n"
end end
end end
hmac = @hmac.update(string_to_sign.chomp!) hmac = @hmac.update(string_to_sign.chomp!)
signature = Base64.encode64(hmac.digest).strip! signature = Base64.encode64(hmac.digest).strip!
headers['Authorization'] = "AWS #{@aws_access_key_id}:#{signature}" params[:headers]['Authorization'] = "AWS #{@aws_access_key_id}:#{signature}"
response = @connection.request({ response = @connection.request({
:body => body, :body => params[:body],
:headers => headers, :headers => params[:headers],
:method => method, :method => params[:method],
:url => url :url => params[:url]
}) })
if parser && !response.body.empty? if params[:parser] && !response.body.empty?
Nokogiri::XML::SAX::Parser.new(parser).parse(response.body.split(/<\?xml.*\?>/)[1]) Nokogiri::XML::SAX::Parser.new(params[:parser]).parse(response.body.split(/<\?xml.*\?>/)[1])
response.body = parser.response response.body = params[:parser].response
end end
response response