mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
Merge pull request #871 from halorgium/put_object_http_url
Add support for ports in AWS storage URLs DRY up get/put_object_url
This commit is contained in:
commit
b98f3a1ffe
7 changed files with 72 additions and 59 deletions
|
@ -141,7 +141,7 @@ module Fog
|
|||
|
||||
def url(expires, options = {})
|
||||
requires :key
|
||||
collection.get_https_url(key, expires, options)
|
||||
collection.get_url(key, expires, options)
|
||||
end
|
||||
|
||||
def versions
|
||||
|
|
|
@ -78,6 +78,11 @@ module Fog
|
|||
end
|
||||
end
|
||||
|
||||
def get_url(key, expires, options = {})
|
||||
requires :directory
|
||||
connection.get_object_url(directory.key, key, expires, options)
|
||||
end
|
||||
|
||||
def get_http_url(key, expires, options = {})
|
||||
requires :directory
|
||||
connection.get_object_http_url(directory.key, key, expires, options)
|
||||
|
|
|
@ -19,6 +19,7 @@ module Fog
|
|||
http_url({
|
||||
:headers => {},
|
||||
:host => host,
|
||||
:port => @port,
|
||||
:method => 'GET',
|
||||
:path => path,
|
||||
:query => options[:query]
|
||||
|
|
|
@ -5,24 +5,7 @@ module Fog
|
|||
module GetObjectHttpsUrl
|
||||
|
||||
def get_object_https_url(bucket_name, object_name, expires, options = {})
|
||||
unless bucket_name
|
||||
raise ArgumentError.new('bucket_name is required')
|
||||
end
|
||||
unless object_name
|
||||
raise ArgumentError.new('object_name is required')
|
||||
end
|
||||
host, path = if bucket_name =~ /^(?:[a-z]|\d(?!\d{0,2}(?:\.\d{1,3}){3}$))(?:[a-z0-9]|\-(?![\.])){1,61}[a-z0-9]$/
|
||||
["#{bucket_name}.#{@host}", object_name]
|
||||
else
|
||||
[@host, "#{bucket_name}/#{object_name}"]
|
||||
end
|
||||
https_url({
|
||||
:headers => {},
|
||||
:host => host,
|
||||
:method => 'GET',
|
||||
:path => path,
|
||||
:query => options[:query]
|
||||
}, expires)
|
||||
get_object_url(bucket_name, object_name, expires, options.merge(:scheme => 'https'))
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -1,6 +1,33 @@
|
|||
module Fog
|
||||
module Storage
|
||||
class AWS
|
||||
|
||||
module GetObjectUrl
|
||||
|
||||
def get_object_url(bucket_name, object_name, expires, options = {})
|
||||
unless bucket_name
|
||||
raise ArgumentError.new('bucket_name is required')
|
||||
end
|
||||
unless object_name
|
||||
raise ArgumentError.new('object_name is required')
|
||||
end
|
||||
host, path = if bucket_name =~ /^(?:[a-z]|\d(?!\d{0,2}(?:\.\d{1,3}){3}$))(?:[a-z0-9]|\.(?![\.\-])|\-(?![\.])){1,61}[a-z0-9]$/
|
||||
["#{bucket_name}.#{@host}", object_name]
|
||||
else
|
||||
[@host, "#{bucket_name}/#{object_name}"]
|
||||
end
|
||||
scheme_host_path_query({
|
||||
:scheme => options[:scheme],
|
||||
:headers => {},
|
||||
:host => host,
|
||||
:port => @port,
|
||||
:method => 'GET',
|
||||
:path => path,
|
||||
:query => options[:query]
|
||||
}, expires)
|
||||
end
|
||||
end
|
||||
|
||||
class Real
|
||||
|
||||
# Get an expiring object url from S3
|
||||
|
@ -17,19 +44,13 @@ module Fog
|
|||
# ==== See Also
|
||||
# http://docs.amazonwebservices.com/AmazonS3/latest/dev/S3_QSAuth.html
|
||||
|
||||
def get_object_url(bucket_name, object_name, expires)
|
||||
Fog::Logger.deprecation("Fog::Storage::AWS => #get_object_url is deprecated, use #get_object_https_url instead [light_black](#{caller.first})[/]")
|
||||
get_object_https_url(bucket_name, object_name, expires)
|
||||
end
|
||||
include GetObjectUrl
|
||||
|
||||
end
|
||||
|
||||
class Mock # :nodoc:all
|
||||
|
||||
def get_object_url(bucket_name, object_name, expires)
|
||||
Fog::Logger.deprecation("Fog::Storage::AWS => #get_object_url is deprecated, use #get_object_https_url instead [light_black](#{caller.first})[/]")
|
||||
get_object_https_url(bucket_name, object_name, expires)
|
||||
end
|
||||
include GetObjectUrl
|
||||
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,6 +1,26 @@
|
|||
module Fog
|
||||
module Storage
|
||||
class AWS
|
||||
module PutObjectUrl
|
||||
|
||||
def put_object_url(bucket_name, object_name, expires, headers = {}, options = {})
|
||||
unless bucket_name
|
||||
raise ArgumentError.new('bucket_name is required')
|
||||
end
|
||||
unless object_name
|
||||
raise ArgumentError.new('object_name is required')
|
||||
end
|
||||
scheme_host_path_query({
|
||||
:scheme => options[:scheme],
|
||||
:headers => headers,
|
||||
:host => @host,
|
||||
:port => @port,
|
||||
:method => 'PUT',
|
||||
:path => "#{bucket_name}/#{object_name}"
|
||||
}, expires)
|
||||
end
|
||||
end
|
||||
|
||||
class Real
|
||||
|
||||
# Get an expiring object url from S3 for putting an object
|
||||
|
@ -17,39 +37,13 @@ module Fog
|
|||
# ==== See Also
|
||||
# http://docs.amazonwebservices.com/AmazonS3/latest/dev/S3_QSAuth.html
|
||||
|
||||
def put_object_url(bucket_name, object_name, expires, headers = {})
|
||||
unless bucket_name
|
||||
raise ArgumentError.new('bucket_name is required')
|
||||
end
|
||||
unless object_name
|
||||
raise ArgumentError.new('object_name is required')
|
||||
end
|
||||
https_url({
|
||||
:headers => headers,
|
||||
:host => @host,
|
||||
:method => 'PUT',
|
||||
:path => "#{bucket_name}/#{object_name}"
|
||||
}, expires)
|
||||
end
|
||||
include PutObjectUrl
|
||||
|
||||
end
|
||||
|
||||
class Mock # :nodoc:all
|
||||
|
||||
def put_object_url(bucket_name, object_name, expires, headers = {})
|
||||
unless bucket_name
|
||||
raise ArgumentError.new('bucket_name is required')
|
||||
end
|
||||
unless object_name
|
||||
raise ArgumentError.new('object_name is required')
|
||||
end
|
||||
https_url({
|
||||
:headers => headers,
|
||||
:host => @host,
|
||||
:method => 'PUT',
|
||||
:path => "#{bucket_name}/#{object_name}"
|
||||
}, expires)
|
||||
end
|
||||
include PutObjectUrl
|
||||
|
||||
end
|
||||
end
|
||||
|
|
|
@ -71,11 +71,11 @@ module Fog
|
|||
end
|
||||
|
||||
def http_url(params, expires)
|
||||
"http://" << host_path_query(params, expires)
|
||||
scheme_host_path_query(params.merge(:scheme => 'http', :port => 80), expires)
|
||||
end
|
||||
|
||||
def https_url(params, expires)
|
||||
"https://" << host_path_query(params, expires)
|
||||
scheme_host_path_query(params.merge(:scheme => 'https', :port => 443), expires)
|
||||
end
|
||||
|
||||
def url(params, expires)
|
||||
|
@ -85,7 +85,14 @@ module Fog
|
|||
|
||||
private
|
||||
|
||||
def host_path_query(params, expires)
|
||||
def scheme_host_path_query(params, expires)
|
||||
params[:scheme] ||= @scheme
|
||||
if params[:port] == 80 && params[:scheme] == 'http'
|
||||
params.delete(:port)
|
||||
end
|
||||
if params[:port] == 443 && params[:scheme] == 'https'
|
||||
params.delete(:port)
|
||||
end
|
||||
params[:headers] ||= {}
|
||||
params[:headers]['Date'] = expires.to_i
|
||||
params[:path] = Fog::AWS.escape(params[:path]).gsub('%2F', '/')
|
||||
|
@ -98,7 +105,8 @@ module Fog
|
|||
query << "AWSAccessKeyId=#{@aws_access_key_id}"
|
||||
query << "Signature=#{Fog::AWS.escape(signature(params))}"
|
||||
query << "Expires=#{params[:headers]['Date']}"
|
||||
"#{params[:host]}/#{params[:path]}?#{query.join('&')}"
|
||||
port_part = params[:port] && ":#{params[:port]}"
|
||||
"#{params[:scheme]}://#{params[:host]}#{port_part}/#{params[:path]}?#{query.join('&')}"
|
||||
end
|
||||
|
||||
end
|
||||
|
@ -197,6 +205,7 @@ module Fog
|
|||
else
|
||||
"s3-#{options[:region]}.amazonaws.com"
|
||||
end
|
||||
@scheme = options[:scheme] || 'https'
|
||||
@region = options[:region]
|
||||
end
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue