diff --git a/lib/fog/aws.rb b/lib/fog/aws.rb index 74135ba86..265396606 100644 --- a/lib/fog/aws.rb +++ b/lib/fog/aws.rb @@ -1,11 +1,12 @@ +require 'fog/aws/s3.rb' + module Fog module AWS def self.dependencies [ 'fog/aws/ec2.rb', - 'fog/aws/simpledb.rb', - 'fog/aws/s3.rb' + 'fog/aws/simpledb.rb' ] end diff --git a/lib/fog/aws/models/s3/directories.rb b/lib/fog/aws/models/s3/directories.rb index d949ba17c..384457ef2 100644 --- a/lib/fog/aws/models/s3/directories.rb +++ b/lib/fog/aws/models/s3/directories.rb @@ -1,9 +1,20 @@ +require 'fog/collection' +require 'fog/aws/models/s3/directory' + module Fog module AWS - class S3 + module S3 - def directories - Fog::AWS::S3::Directories.new(:connection => self) + class Real + def directories + Fog::AWS::S3::Directories.new(:connection => self) + end + end + + class Mock + def directories + Fog::AWS::S3::Directories.new(:connection => self) + end end class Directories < Fog::Collection diff --git a/lib/fog/aws/models/s3/directory.rb b/lib/fog/aws/models/s3/directory.rb index 046d4647f..3d048f83d 100644 --- a/lib/fog/aws/models/s3/directory.rb +++ b/lib/fog/aws/models/s3/directory.rb @@ -1,6 +1,9 @@ +require 'fog/model' +require 'fog/aws/models/s3/files' + module Fog module AWS - class S3 + module S3 class Directory < Fog::Model diff --git a/lib/fog/aws/models/s3/file.rb b/lib/fog/aws/models/s3/file.rb index 674a4c44a..036e39f30 100644 --- a/lib/fog/aws/models/s3/file.rb +++ b/lib/fog/aws/models/s3/file.rb @@ -1,6 +1,8 @@ +require 'fog/model' + module Fog module AWS - class S3 + module S3 class File < Fog::Model diff --git a/lib/fog/aws/models/s3/files.rb b/lib/fog/aws/models/s3/files.rb index d4d9a8763..80434fab2 100644 --- a/lib/fog/aws/models/s3/files.rb +++ b/lib/fog/aws/models/s3/files.rb @@ -1,6 +1,9 @@ +require 'fog/collection' +require 'fog/aws/models/s3/file' + module Fog module AWS - class S3 + module S3 class Files < Fog::Collection diff --git a/lib/fog/aws/requests/s3/copy_object.rb b/lib/fog/aws/requests/s3/copy_object.rb index 669c66fe5..5ab14994a 100644 --- a/lib/fog/aws/requests/s3/copy_object.rb +++ b/lib/fog/aws/requests/s3/copy_object.rb @@ -1,8 +1,9 @@ -unless Fog.mocking? +module Fog + module AWS + module S3 + class Real - module Fog - module AWS - class S3 + require 'fog/aws/parsers/s3/copy_object' # Copy an object from one S3 bucket to another # @@ -38,20 +39,14 @@ unless Fog.mocking? end end - end - end -else - - module Fog - module AWS - class S3 + class Mock def copy_object(source_bucket_name, source_object_name, target_bucket_name, target_object_name, options = {}) response = Excon::Response.new - source_bucket = Fog::AWS::S3.data[:buckets][source_bucket_name] + source_bucket = @data[:buckets][source_bucket_name] source_object = source_bucket && source_bucket[:objects][source_object_name] - target_bucket = Fog::AWS::S3.data[:buckets][target_bucket_name] + target_bucket = @data[:buckets][target_bucket_name] if source_object && target_bucket response.status = 200 @@ -75,5 +70,4 @@ else end end end - -end \ No newline at end of file +end diff --git a/lib/fog/aws/requests/s3/delete_bucket.rb b/lib/fog/aws/requests/s3/delete_bucket.rb index 4cab03163..041a4adb7 100644 --- a/lib/fog/aws/requests/s3/delete_bucket.rb +++ b/lib/fog/aws/requests/s3/delete_bucket.rb @@ -1,8 +1,7 @@ -unless Fog.mocking? - - module Fog - module AWS - class S3 +module Fog + module AWS + module S3 + class Real # Delete an S3 bucket # @@ -22,32 +21,26 @@ unless Fog.mocking? end end - end - end -else - - module Fog - module AWS - class S3 + class Mock def delete_bucket(bucket_name) response = Excon::Response.new - if Fog::AWS::S3.data[:buckets][bucket_name].nil? + if @data[:buckets][bucket_name].nil? response.status = 404 raise(Excon::Errors.status_error({:expects => 204}, response)) - elsif Fog::AWS::S3.data[:buckets][bucket_name] && !Fog::AWS::S3.data[:buckets][bucket_name][:objects].empty? + elsif @data[:buckets][bucket_name] && !@data[:buckets][bucket_name][:objects].empty? response.status = 409 raise(Excon::Errors.status_error({:expects => 204}, response)) else - Fog::AWS::S3.data[:buckets].delete(bucket_name) + @data[:buckets].delete(bucket_name) response.status = 204 end response end end + end end - end diff --git a/lib/fog/aws/requests/s3/delete_object.rb b/lib/fog/aws/requests/s3/delete_object.rb index 9dc4ddfb4..bf8eeeb30 100644 --- a/lib/fog/aws/requests/s3/delete_object.rb +++ b/lib/fog/aws/requests/s3/delete_object.rb @@ -1,8 +1,7 @@ -unless Fog.mocking? - - module Fog - module AWS - class S3 +module Fog + module AWS + module S3 + class Real # Delete an object from S3 # @@ -25,18 +24,12 @@ unless Fog.mocking? end end - end - end -else - - module Fog - module AWS - class S3 + class Mock def delete_object(bucket_name, object_name) response = Excon::Response.new - if bucket = Fog::AWS::S3.data[:buckets][bucket_name] + if bucket = @data[:buckets][bucket_name] response.status = 204 bucket[:objects].delete(object_name) else @@ -49,5 +42,4 @@ else end end end - end \ No newline at end of file diff --git a/lib/fog/aws/requests/s3/get_bucket.rb b/lib/fog/aws/requests/s3/get_bucket.rb index e49a7d1a2..78fd9db5e 100644 --- a/lib/fog/aws/requests/s3/get_bucket.rb +++ b/lib/fog/aws/requests/s3/get_bucket.rb @@ -1,8 +1,9 @@ -unless Fog.mocking? +module Fog + module AWS + module S3 + class Real - module Fog - module AWS - class S3 + require 'fog/aws/parsers/s3/get_bucket' # List information about objects in an S3 bucket # @@ -54,14 +55,8 @@ unless Fog.mocking? end end - end - end -else - - module Fog - module AWS - class S3 + class Mock # FIXME: implement delimiter def get_bucket(bucket_name, options = {}) @@ -69,16 +64,17 @@ else raise ArgumentError.new('bucket_name is required') end response = Excon::Response.new - if bucket = Fog::AWS::S3.data[:buckets][bucket_name] + if bucket = @data[:buckets][bucket_name] response.status = 200 response.body = { 'Contents' => bucket[:objects].values.sort {|x,y| x['Key'] <=> y['Key']}.reject do |object| (options['prefix'] && object['Key'][0...options['prefix'].length] != options['prefix']) || (options['marker'] && object['Key'] <= options['marker']) end.map do |object| - data = object.reject {|key, value| !['ETag', 'Key', 'LastModified', 'Owner', 'Size', 'StorageClass'].include?(key)} + data = object.reject {|key, value| !['ETag', 'Key', 'LastModified', 'Size', 'StorageClass'].include?(key)} data.merge!({ 'LastModified' => Time.parse(data['LastModified']), + 'Owner' => bucket['Owner'], 'Size' => data['Size'].to_i }) data @@ -103,5 +99,4 @@ else end end end - end diff --git a/lib/fog/aws/requests/s3/get_bucket_location.rb b/lib/fog/aws/requests/s3/get_bucket_location.rb index 7707eb8af..d6a026bc0 100644 --- a/lib/fog/aws/requests/s3/get_bucket_location.rb +++ b/lib/fog/aws/requests/s3/get_bucket_location.rb @@ -1,8 +1,9 @@ -unless Fog.mocking? +module Fog + module AWS + module S3 + class Real - module Fog - module AWS - class S3 + require 'fog/aws/parsers/s3/get_bucket_location' # Get location constraint for an S3 bucket # @@ -26,18 +27,12 @@ unless Fog.mocking? end end - end - end -else - - module Fog - module AWS - class S3 + class Mock def get_bucket_location(bucket_name) response = Excon::Response.new - if bucket = Fog::AWS::S3.data[:buckets][bucket_name] + if bucket = @data[:buckets][bucket_name] response.status = 200 response.body = {'LocationConstraint' => bucket['LocationConstraint'] } else @@ -50,5 +45,4 @@ else end end end - -end \ No newline at end of file +end diff --git a/lib/fog/aws/requests/s3/get_object.rb b/lib/fog/aws/requests/s3/get_object.rb index 8658e04fc..6b41ba141 100644 --- a/lib/fog/aws/requests/s3/get_object.rb +++ b/lib/fog/aws/requests/s3/get_object.rb @@ -1,8 +1,7 @@ -unless Fog.mocking? - - module Fog - module AWS - class S3 +module Fog + module AWS + module S3 + class Real # Get an object from S3 # @@ -56,19 +55,13 @@ unless Fog.mocking? :headers => {}, :host => "#{bucket_name}.#{@host}", :method => 'GET', - :path => object_name + :path => CGI.escape(object_name) }, expires) end end - end - end -else - - module Fog - module AWS - class S3 + class Mock def get_object(bucket_name, object_name, options = {}, &block) unless bucket_name @@ -78,7 +71,7 @@ else raise ArgumentError.new('object_name is required') end response = Excon::Response.new - if (bucket = Fog::AWS::S3.data[:buckets][bucket_name]) && (object = bucket[:objects][object_name]) + if (bucket = @data[:buckets][bucket_name]) && (object = bucket[:objects][object_name]) if options['If-Match'] && options['If-Match'] != object['ETag'] response.status = 412 elsif options['If-Modified-Since'] && options['If-Modified-Since'] > Time.parse(object['LastModified']) @@ -121,5 +114,4 @@ else end end end - end diff --git a/lib/fog/aws/requests/s3/get_request_payment.rb b/lib/fog/aws/requests/s3/get_request_payment.rb index a03f765c3..6582daf53 100644 --- a/lib/fog/aws/requests/s3/get_request_payment.rb +++ b/lib/fog/aws/requests/s3/get_request_payment.rb @@ -1,8 +1,9 @@ -unless Fog.mocking? +module Fog + module AWS + module S3 + class Real - module Fog - module AWS - class S3 + require 'fog/aws/parsers/s3/get_request_payment' # Get configured payer for an S3 bucket # @@ -26,18 +27,12 @@ unless Fog.mocking? end end - end - end -else - - module Fog - module AWS - class S3 + class Mock def get_request_payment(bucket_name) response = Excon::Response.new - if bucket = Fog::AWS::S3.data[:buckets][bucket_name] + if bucket = @data[:buckets][bucket_name] response.status = 200 response.body = { 'Payer' => bucket['Payer'] } else @@ -50,5 +45,4 @@ else end end end - end diff --git a/lib/fog/aws/requests/s3/get_service.rb b/lib/fog/aws/requests/s3/get_service.rb index 4d632fba0..171e2e970 100644 --- a/lib/fog/aws/requests/s3/get_service.rb +++ b/lib/fog/aws/requests/s3/get_service.rb @@ -1,8 +1,9 @@ -unless Fog.mocking? +module Fog + module AWS + module S3 + class Real - module Fog - module AWS - class S3 + require 'fog/aws/parsers/s3/get_service' # List information about S3 buckets for authorized user # @@ -28,19 +29,13 @@ unless Fog.mocking? end end - end - end -else - - module Fog - module AWS - class S3 + class Mock def get_service response = Excon::Response.new response.headers['Status'] = 200 - buckets = Fog::AWS::S3.data[:buckets].values.map do |bucket| + buckets = @data[:buckets].values.map do |bucket| bucket.reject do |key, value| !['CreationDate', 'Name'].include?(key) end @@ -55,5 +50,4 @@ else end end end - end diff --git a/lib/fog/aws/requests/s3/head_object.rb b/lib/fog/aws/requests/s3/head_object.rb index 6c3a92685..87cd5a9a6 100644 --- a/lib/fog/aws/requests/s3/head_object.rb +++ b/lib/fog/aws/requests/s3/head_object.rb @@ -1,8 +1,7 @@ -unless Fog.mocking? - - module Fog - module AWS - class S3 +module Fog + module AWS + module S3 + class Real # Get headers for an object from S3 # @@ -39,14 +38,8 @@ unless Fog.mocking? end end - end - end -else - - module Fog - module AWS - class S3 + class Mock def head_object(bucket_name, object_name, options = {}) response = get_object(bucket_name, object_name, options) @@ -57,5 +50,4 @@ else end end end - -end \ No newline at end of file +end diff --git a/lib/fog/aws/requests/s3/put_bucket.rb b/lib/fog/aws/requests/s3/put_bucket.rb index 7a4f7d7bb..f6f411b23 100644 --- a/lib/fog/aws/requests/s3/put_bucket.rb +++ b/lib/fog/aws/requests/s3/put_bucket.rb @@ -1,8 +1,7 @@ -unless Fog.mocking? - - module Fog - module AWS - class S3 +module Fog + module AWS + module S3 + class Real # Create an S3 bucket # @@ -17,11 +16,11 @@ unless Fog.mocking? def put_bucket(bucket_name, options = {}) if options['LocationConstraint'] data = - <<-DATA - - #{options['LocationConstraint']} - - DATA +<<-DATA + + #{options['LocationConstraint']} + +DATA else data = nil end @@ -36,14 +35,8 @@ unless Fog.mocking? end end - end - end -else - - module Fog - module AWS - class S3 + class Mock def put_bucket(bucket_name, options = {}) response = Excon::Response.new @@ -60,8 +53,8 @@ else else bucket['LocationConstraint'] = '' end - unless Fog::AWS::S3.data[:buckets][bucket_name] - Fog::AWS::S3.data[:buckets][bucket_name] = bucket + unless @data[:buckets][bucket_name] + @data[:buckets][bucket_name] = bucket end response end @@ -69,5 +62,4 @@ else end end end - -end \ No newline at end of file +end diff --git a/lib/fog/aws/requests/s3/put_object.rb b/lib/fog/aws/requests/s3/put_object.rb index 04b3e9b59..889d6b966 100644 --- a/lib/fog/aws/requests/s3/put_object.rb +++ b/lib/fog/aws/requests/s3/put_object.rb @@ -1,8 +1,7 @@ -unless Fog.mocking? - - module Fog - module AWS - class S3 +module Fog + module AWS + module S3 + class Real # Create an object in an S3 bucket # @@ -25,7 +24,7 @@ unless Fog.mocking? # * headers<~Hash>: # * 'ETag'<~String> - etag of new object def put_object(bucket_name, object_name, data, options = {}) - data = parse_data(data) + data = Fog::AWS::S3.parse_data(data) headers = data[:headers].merge!(options) request({ :body => data[:body], @@ -39,19 +38,13 @@ unless Fog.mocking? end end - end - end -else - - module Fog - module AWS - class S3 + class Mock def put_object(bucket_name, object_name, data, options = {}) - data = parse_data(data) + data = Fog::AWS::S3.parse_data(data) response = Excon::Response.new - if (bucket = Fog::AWS::S3.data[:buckets][bucket_name]) + if (bucket = @data[:buckets][bucket_name]) response.status = 200 bucket[:objects][object_name] = { :body => data[:body], @@ -72,5 +65,4 @@ else end end end - end diff --git a/lib/fog/aws/requests/s3/put_request_payment.rb b/lib/fog/aws/requests/s3/put_request_payment.rb index e2b7328ae..1778be894 100644 --- a/lib/fog/aws/requests/s3/put_request_payment.rb +++ b/lib/fog/aws/requests/s3/put_request_payment.rb @@ -1,8 +1,7 @@ -unless Fog.mocking? - - module Fog - module AWS - class S3 +module Fog + module AWS + module S3 + class Real # Change who pays for requests to an S3 bucket # @@ -27,18 +26,12 @@ DATA end end - end - end -else - - module Fog - module AWS - class S3 + class Mock def put_request_payment(bucket_name, payer) response = Excon::Response.new - if bucket = Fog::AWS::S3.data[:buckets][bucket_name] + if bucket = @data[:buckets][bucket_name] response.status = 200 bucket['Payer'] = payer else @@ -51,6 +44,4 @@ else end end end - - -end \ No newline at end of file +end diff --git a/lib/fog/aws/s3.rb b/lib/fog/aws/s3.rb index 4c3e443f0..dbcac4a29 100644 --- a/lib/fog/aws/s3.rb +++ b/lib/fog/aws/s3.rb @@ -1,82 +1,33 @@ +require 'fog/aws/models/s3/directories' +require 'fog/aws/models/s3/directory' +require 'fog/aws/models/s3/files' +require 'fog/aws/models/s3/file' +require 'fog/aws/requests/s3/copy_object' +require 'fog/aws/requests/s3/delete_bucket' +require 'fog/aws/requests/s3/delete_object' +require 'fog/aws/requests/s3/get_bucket' +require 'fog/aws/requests/s3/get_bucket_location' +require 'fog/aws/requests/s3/get_object' +require 'fog/aws/requests/s3/get_request_payment' +require 'fog/aws/requests/s3/get_service' +require 'fog/aws/requests/s3/head_object' +require 'fog/aws/requests/s3/put_bucket' +require 'fog/aws/requests/s3/put_object' +require 'fog/aws/requests/s3/put_request_payment' + module Fog module AWS - class S3 + module S3 - if Fog.mocking? - def self.reset_data - @data = { :buckets => {} } - end - def self.data - @data - end - end - - def self.dependencies - [ - "fog/aws/models/s3/directory.rb", - "fog/aws/models/s3/directories.rb", - "fog/aws/models/s3/file.rb", - "fog/aws/models/s3/files.rb", - "fog/aws/parsers/s3/copy_object.rb", - "fog/aws/parsers/s3/get_bucket.rb", - "fog/aws/parsers/s3/get_bucket_location.rb", - "fog/aws/parsers/s3/get_request_payment.rb", - "fog/aws/parsers/s3/get_service.rb", - "fog/aws/requests/s3/copy_object.rb", - "fog/aws/requests/s3/delete_bucket.rb", - "fog/aws/requests/s3/delete_object.rb", - "fog/aws/requests/s3/get_bucket.rb", - "fog/aws/requests/s3/get_bucket_location.rb", - "fog/aws/requests/s3/get_object.rb", - "fog/aws/requests/s3/get_request_payment.rb", - "fog/aws/requests/s3/get_service.rb", - "fog/aws/requests/s3/head_object.rb", - "fog/aws/requests/s3/put_bucket.rb", - "fog/aws/requests/s3/put_object.rb", - "fog/aws/requests/s3/put_request_payment.rb" - ] - end - - def self.reload - self.dependencies.each {|dependency| load(dependency)} + def self.new(options={}) if Fog.mocking? - reset_data + Fog::AWS::S3::Mock.new(options) + else + Fog::AWS::S3::Real.new(options) end end - # Initialize connection to S3 - # - # ==== Notes - # options parameter must include values for :aws_access_key_id and - # :aws_secret_access_key in order to create a connection - # - # ==== Examples - # s3 = S3.new( - # :aws_access_key_id => your_aws_access_key_id, - # :aws_secret_access_key => your_aws_secret_access_key - # ) - # - # ==== Parameters - # * options<~Hash> - config arguments for connection. Defaults to {}. - # - # ==== Returns - # * S3 object with connection to aws. - def initialize(options={}) - unless @aws_access_key_id = options[:aws_access_key_id] - raise ArgumentError.new('aws_access_key_id is required to access ec2') - end - unless @aws_secret_access_key = options[:aws_secret_access_key] - raise ArgumentError.new('aws_secret_access_key is required to access ec2') - end - @hmac = HMAC::SHA1.new(@aws_secret_access_key) - @host = options[:host] || 's3.amazonaws.com' - @port = options[:port] || 443 - @scheme = options[:scheme] || 'https' - end - - private - - def parse_data(data) + def self.parse_data(data) metadata = { :body => nil, :headers => {} @@ -97,27 +48,64 @@ module Fog metadata end - def request(params) - @connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}") - params[:headers]['Date'] = Time.now.utc.strftime("%a, %d %b %Y %H:%M:%S +0000") - params[:headers]['Authorization'] = "AWS #{@aws_access_key_id}:#{signature(params)}" + class Mock - response = @connection.request(params) + def reset_data + @data = { :buckets => {} } + end + + def initialize(options={}) + reset_data + end - response end - def url(params, expires) - params[:headers]['Date'] = expires.to_i - query = [params[:query]].compact - query << "AWSAccessKeyId=#{@aws_access_key_id}" - query << "Signature=#{CGI.escape(signature(params))}" - query << "Expires=#{params[:headers]['Date']}" - "http://#{params[:host]}/#{params[:path]}?#{query.join('&')}" - end + class Real - def signature(params) - string_to_sign = + # Initialize connection to S3 + # + # ==== Notes + # options parameter must include values for :aws_access_key_id and + # :aws_secret_access_key in order to create a connection + # + # ==== Examples + # s3 = S3.new( + # :aws_access_key_id => your_aws_access_key_id, + # :aws_secret_access_key => your_aws_secret_access_key + # ) + # + # ==== Parameters + # * options<~Hash> - config arguments for connection. Defaults to {}. + # + # ==== Returns + # * S3 object with connection to aws. + def initialize(options={}) + unless @aws_access_key_id = options[:aws_access_key_id] + raise ArgumentError.new('aws_access_key_id is required to access ec2') + end + unless @aws_secret_access_key = options[:aws_secret_access_key] + raise ArgumentError.new('aws_secret_access_key is required to access ec2') + end + @hmac = HMAC::SHA1.new(@aws_secret_access_key) + @host = options[:host] || 's3.amazonaws.com' + @port = options[:port] || 443 + @scheme = options[:scheme] || 'https' + end + + private + + def request(params) + @connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}") + params[:headers]['Date'] = Time.now.utc.strftime("%a, %d %b %Y %H:%M:%S +0000") + params[:headers]['Authorization'] = "AWS #{@aws_access_key_id}:#{signature(params)}" + + response = @connection.request(params) + + response + end + + def signature(params) + string_to_sign = <<-DATA #{params[:method]} #{params[:headers]['Content-MD5']} @@ -125,46 +113,54 @@ module Fog #{params[:headers]['Date']} DATA - amz_headers, canonical_amz_headers = {}, '' - for key, value in params[:headers] - if key[0..5] == 'x-amz-' - amz_headers[key] = value + amz_headers, canonical_amz_headers = {}, '' + for key, value in params[:headers] + if key[0..5] == 'x-amz-' + amz_headers[key] = value + end end - end - amz_headers = amz_headers.sort {|x, y| x[0] <=> y[0]} - for pair in amz_headers - canonical_amz_headers << "#{pair[0]}:#{pair[1]}\n" - end - string_to_sign << "#{canonical_amz_headers}" - - subdomain = params[:host].split(".#{@host}").first - unless subdomain =~ /^(?:[a-z]|\d(?!\d{0,2}(?:\.\d{1,3}){3}$))(?:[a-z0-9]|\.(?![\.\-])|\-(?![\.])){1,61}[a-z0-9]$/ - puts("[WARN] fog: the specified s3 bucket name(#{subdomain}) is not a valid dns name. See: http://docs.amazonwebservices.com/AmazonS3/latest/dev/index.html?Introduction.html") - params[:host] = params[:host].split("#{subdomain}.")[-1] - if params[:path] - params[:path] = "#{subdomain}/#{params[:path]}" - else - params[:path] = "#{subdomain}" + amz_headers = amz_headers.sort {|x, y| x[0] <=> y[0]} + for pair in amz_headers + canonical_amz_headers << "#{pair[0]}:#{pair[1]}\n" end - subdomain = nil + string_to_sign << "#{canonical_amz_headers}" + + subdomain = params[:host].split(".#{@host}").first + unless subdomain =~ /^(?:[a-z]|\d(?!\d{0,2}(?:\.\d{1,3}){3}$))(?:[a-z0-9]|\.(?![\.\-])|\-(?![\.])){1,61}[a-z0-9]$/ + puts("[WARN] fog: the specified s3 bucket name(#{subdomain}) is not a valid dns name. See: http://docs.amazonwebservices.com/AmazonS3/latest/dev/index.html?Introduction.html") + params[:host] = params[:host].split("#{subdomain}.")[-1] + if params[:path] + params[:path] = "#{subdomain}/#{params[:path]}" + else + params[:path] = "#{subdomain}" + end + subdomain = nil + end + + canonical_resource = "/" + unless subdomain.nil? || subdomain == @host + canonical_resource << "#{CGI.escape(subdomain).downcase}/" + end + canonical_resource << "#{params[:path]}" + if ['acl', 'location', 'logging', 'requestPayment', 'torrent'].include?(params[:query]) + canonical_resource << "?#{params[:query]}" + end + string_to_sign << "#{canonical_resource}" + + hmac = @hmac.update(string_to_sign) + signature = Base64.encode64(hmac.digest).chomp! end - canonical_resource = "/" - unless subdomain.nil? || subdomain == @host - canonical_resource << "#{CGI.escape(subdomain).downcase}/" + def url(params, expires) + params[:headers]['Date'] = expires.to_i + query = [params[:query]].compact + query << "AWSAccessKeyId=#{@aws_access_key_id}" + query << "Signature=#{CGI.escape(signature(params))}" + query << "Expires=#{params[:headers]['Date']}" + "http://#{params[:host]}/#{params[:path]}?#{query.join('&')}" end - canonical_resource << "#{params[:path]}" - if ['acl', 'location', 'logging', 'requestPayment', 'torrent'].include?(params[:query]) - canonical_resource << "?#{params[:query]}" - end - string_to_sign << "#{canonical_resource}" - hmac = @hmac.update(string_to_sign) - signature = Base64.encode64(hmac.digest).chomp! end - end end end - -Fog::AWS::S3.reload