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

adapts aws method signatures for temp url generation

This commit is contained in:
Julian Weber 2013-10-02 15:13:47 +02:00
parent 997cec9271
commit 2e0be6bd17
3 changed files with 57 additions and 26 deletions

View file

@ -65,15 +65,24 @@ module Fog
new_public new_public
end end
# Get a url for file.
#
# required attributes: key
#
# @param expires [String] number of seconds (since 1970-01-01 00:00) before url expires
# @param options [Hash]
# @return [String] url
#
def url(expires, options = {})
requires :directory, :key
self.service.create_temp_url_with_service_scheme(self.directory.key, self.key, expires, "GET", options = {})
end
def public_url def public_url
requires :key requires :key
self.collection.get_url(self.key) self.collection.get_url(self.key)
end 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 = {}) def save(options = {})
requires :body, :directory, :key requires :body, :directory, :key

View file

@ -71,6 +71,16 @@ module Fog
end end
end end
def get_http_url(key, expires, options = {})
requires :directory
service.get_object_http_url(directory.key, key, expires, options)
end
def get_https_url(key, expires, options = {})
requires :directory
service.get_object_https_url(directory.key, key, expires, options)
end
def head(key, options = {}) def head(key, options = {})
requires :directory requires :directory
data = service.head_object(directory.key, key) data = service.head_object(directory.key, key)

View file

@ -14,21 +14,33 @@ module Fog
# ==== Returns # ==== Returns
# * response<~Excon::Response>: # * response<~Excon::Response>:
# * body<~String> - url for object # * body<~String> - url for object
#
# ==== 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 = {}) def get_object_https_url(container, object, expires, options = {})
create_temp_url(container, object, expires, "GET", "https", options) create_temp_url(container, object, expires, "GET", "https", options)
end end
# Get an expiring object url from Cloud Files # Get an expiring object http url
# The function uses the scheme used by the storage object (http or https)
# #
# ==== Parameters # ==== Parameters
# * container<~String> - Name of container containing object # * container<~String> - Name of container containing object
# * object<~String> - Name of object to get expiring url for # * object<~String> - Name of object to get expiring url for
# * expires_secs<~Time> - The duration in seconds of the validity for the generated url # * expires<~Time> - An expiry time for this 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
def get_object_http_url(container, object, expires, options = {})
create_temp_url(container, object, expires, "GET", "http", options)
end
# creates a temporary url
#
# ==== Parameters
# * container<~String> - Name of container containing object
# * object<~String> - Name of object to get expiring url for
# * expires<~Time> - An expiry time for this url
# * method<~String> - The method to use for accessing the object (GET, PUT, HEAD)
# * scheme<~String> - The scheme to use (http, https)
# * options<~Hash> - An optional options hash
# #
# ==== Returns # ==== Returns
# * response<~Excon::Response>: # * response<~Excon::Response>:
@ -36,21 +48,6 @@ module Fog
# #
# ==== See Also # ==== See Also
# http://docs.rackspace.com/files/api/v1/cf-devguide/content/Create_TempURL-d1a444.html # 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
def sig_to_hex(str)
str.unpack("C*").map { |c|
c.to_s(16)
}.map { |h|
h.size == 1 ? "0#{h}" : h
}.join
end
def create_temp_url(container, object, expires, method, scheme, options = {}) def create_temp_url(container, object, expires, method, scheme, options = {})
raise ArgumentError, "Insufficient parameters specified." unless (container && object && expires && method) 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? raise ArgumentError, "Storage must my instantiated with the :openstack_temp_url_key option" if @openstack_temp_url_key.nil?
@ -74,6 +71,21 @@ module Fog
"#{scheme}://#{@host}#{object_path_escaped}?temp_url_sig=#{sig}&temp_url_expires=#{expires}" "#{scheme}://#{@host}#{object_path_escaped}?temp_url_sig=#{sig}&temp_url_expires=#{expires}"
end end
# Creates a temporary url using the scheme specified in the service configuration
def create_temp_url_with_service_scheme(container, object, expires, method, options = {})
create_temp_url(container, object, expires, method, @scheme, options)
end
private
def sig_to_hex(str)
str.unpack("C*").map { |c|
c.to_s(16)
}.map { |h|
h.size == 1 ? "0#{h}" : h
}.join
end
end end
end end