mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
refactors get_object_https_url request, adds temp_signed_url method to file class
This commit is contained in:
parent
005293e69f
commit
997cec9271
2 changed files with 49 additions and 14 deletions
|
@ -70,6 +70,11 @@ module Fog
|
|||
self.collection.get_url(self.key)
|
||||
end
|
||||
|
||||
def temp_signed_url(expires_secs, method)
|
||||
requires :directory, :key
|
||||
service.generate_object_temp_url(directory.key, key, expires_secs, method)
|
||||
end
|
||||
|
||||
def save(options = {})
|
||||
requires :body, :directory, :key
|
||||
options['Content-Type'] = content_type if content_type
|
||||
|
|
|
@ -18,20 +18,27 @@ module Fog
|
|||
# ==== See Also
|
||||
# http://docs.rackspace.com/files/api/v1/cf-devguide/content/Create_TempURL-d1a444.html
|
||||
def get_object_https_url(container, object, expires, options = {})
|
||||
if @openstack_temp_url_key.nil?
|
||||
raise ArgumentError, "Storage must my instantiated with the :openstack_temp_url_key option"
|
||||
create_temp_url(container, object, expires, "GET", "https", options)
|
||||
end
|
||||
|
||||
method = 'GET'
|
||||
expires = expires.to_i
|
||||
object_path_escaped = "#{@path}/#{Fog::OpenStack.escape(container)}/#{Fog::OpenStack.escape(object,"/")}"
|
||||
object_path_unescaped = "#{@path}/#{Fog::OpenStack.escape(container)}/#{object}"
|
||||
string_to_sign = "#{method}\n#{expires}\n#{object_path_unescaped}"
|
||||
|
||||
hmac = Fog::HMAC.new('sha1', @openstack_temp_url_key)
|
||||
sig = sig_to_hex(hmac.sign(string_to_sign))
|
||||
|
||||
"https://#{@host}#{object_path_escaped}?temp_url_sig=#{sig}&temp_url_expires=#{expires}"
|
||||
# Get an expiring object url from Cloud Files
|
||||
# The function uses the scheme used by the storage object (http or https)
|
||||
#
|
||||
# ==== Parameters
|
||||
# * container<~String> - Name of container containing object
|
||||
# * object<~String> - Name of object to get expiring url for
|
||||
# * expires_secs<~Time> - The duration in seconds of the validity for the generated url
|
||||
# * method<~String> - The name of the method to be accessible via the url ("GET", "PUT" or "HEAD")
|
||||
#
|
||||
# ==== Returns
|
||||
# * response<~Excon::Response>:
|
||||
# * body<~String> - url for object
|
||||
#
|
||||
# ==== See Also
|
||||
# http://docs.rackspace.com/files/api/v1/cf-devguide/content/Create_TempURL-d1a444.html
|
||||
def generate_object_temp_url(container, object, expires_secs, method, options = {})
|
||||
expires = (Time.now + expires_secs.to_i).to_i
|
||||
create_temp_url(container, object, expires, method, @scheme, options)
|
||||
end
|
||||
|
||||
private
|
||||
|
@ -44,6 +51,29 @@ module Fog
|
|||
}.join
|
||||
end
|
||||
|
||||
def create_temp_url(container, object, expires, method, scheme, options = {})
|
||||
raise ArgumentError, "Insufficient parameters specified." unless (container && object && expires && method)
|
||||
raise ArgumentError, "Storage must my instantiated with the :openstack_temp_url_key option" if @openstack_temp_url_key.nil?
|
||||
|
||||
|
||||
# POST not allowed
|
||||
allowed_methods = %w{GET PUT HEAD}
|
||||
unless allowed_methods.include?(method)
|
||||
raise ArgumentError.new("Invalid method '#{method}' specified. Valid methods are: #{allowed_methods.join(', ')}")
|
||||
end
|
||||
|
||||
|
||||
expires = expires.to_i
|
||||
object_path_escaped = "#{@path}/#{Fog::OpenStack.escape(container)}/#{Fog::OpenStack.escape(object,"/")}"
|
||||
object_path_unescaped = "#{@path}/#{Fog::OpenStack.escape(container)}/#{object}"
|
||||
string_to_sign = "#{method}\n#{expires}\n#{object_path_unescaped}"
|
||||
|
||||
hmac = Fog::HMAC.new('sha1', @openstack_temp_url_key)
|
||||
sig = sig_to_hex(hmac.sign(string_to_sign))
|
||||
|
||||
"#{scheme}://#{@host}#{object_path_escaped}?temp_url_sig=#{sig}&temp_url_expires=#{expires}"
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue