module Fog::Storage::AWS::Utils
Attributes
region[RW]
Public Instance Methods
cdn()
click to toggle source
# File lib/fog/aws/storage.rb, line 114 def cdn @cdn ||= Fog::AWS::CDN.new( :aws_access_key_id => @aws_access_key_id, :aws_secret_access_key => @aws_secret_access_key, :use_iam_profile => @use_iam_profile ) end
http_url(params, expires)
click to toggle source
# File lib/fog/aws/storage.rb, line 122 def http_url(params, expires) signed_url(params.merge(:scheme => 'http'), expires) end
https_url(params, expires)
click to toggle source
# File lib/fog/aws/storage.rb, line 126 def https_url(params, expires) signed_url(params.merge(:scheme => 'https'), expires) end
request_url(params)
click to toggle source
# File lib/fog/aws/storage.rb, line 135 def request_url(params) params = request_params(params) params_to_url(params) end
signed_url(params, expires)
click to toggle source
# File lib/fog/aws/storage.rb, line 140 def signed_url(params, expires) #convert expires from a point in time to a delta to now now = Fog::Time.now expires = expires.to_i - now.to_i params[:headers] ||= {} params[:query]||= {} params[:query]['X-Amz-Expires'] = expires params[:query]['X-Amz-Date'] = now.to_iso8601_basic if @aws_session_token params[:query]['X-Amz-Security-Token'] = @aws_session_token end params = request_params(params) params[:headers][:host] = params[:host] signature = @signer.signature_parameters(params, now, "UNSIGNED-PAYLOAD") params[:query] = (params[:query] || {}).merge(signature) params_to_url(params) end
url(params, expires)
click to toggle source
# File lib/fog/aws/storage.rb, line 130 def url(params, expires) Fog::Logger.deprecation("Fog::Storage::AWS => #url is deprecated, use #https_url instead [light_black](#{caller.first})[/]") https_url(params, expires) end
Private Instance Methods
bucket_to_path(bucket_name, path=nil)
click to toggle source
# File lib/fog/aws/storage.rb, line 180 def bucket_to_path(bucket_name, path=nil) "/#{escape(bucket_name.to_s)}#{path}" end
escape(string)
click to toggle source
NOTE: differs from Fog::AWS.escape
by NOT escaping ‘/`
# File lib/fog/aws/storage.rb, line 185 def escape(string) string.gsub(/([^a-zA-Z0-9_.\-~\/]+)/) { "%" + $1.unpack("H2" * $1.bytesize).join("%").upcase } end
object_to_path(object_name=nil)
click to toggle source
# File lib/fog/aws/storage.rb, line 176 def object_to_path(object_name=nil) '/' + escape(object_name.to_s).gsub('%2F','/') end
params_to_url(params)
click to toggle source
# File lib/fog/aws/storage.rb, line 255 def params_to_url(params) query = params[:query] && params[:query].map do |key, value| if value [key, escape(value.to_s)].join('=') else key end end.join('&') URI::Generic.build({ :scheme => params[:scheme], :host => params[:host], :port => params[:port], :path => params[:path], :query => query, }).to_s end
region_to_host(region=nil)
click to toggle source
# File lib/fog/aws/storage.rb, line 167 def region_to_host(region=nil) case region.to_s when DEFAULT_REGION, '' 's3.amazonaws.com' else "s3-#{region}.amazonaws.com" end end
request_params(params)
click to toggle source
Transforms things like bucket_name, object_name, region
Should yield the same result when called f*f
# File lib/fog/aws/storage.rb, line 194 def request_params(params) headers = params[:headers] || {} if params[:scheme] scheme = params[:scheme] port = params[:port] || DEFAULT_SCHEME_PORT[scheme] else scheme = @scheme port = @port end if DEFAULT_SCHEME_PORT[scheme] == port port = nil end if params[:region] region = params[:region] host = params[:host] || region_to_host(region) else region = @region || DEFAULT_REGION host = params[:host] || @host || region_to_host(region) end path = params[:path] || object_to_path(params[:object_name]) path = '/' + path if path[0..0] != '/' if params[:bucket_name] bucket_name = params[:bucket_name] path_style = params.fetch(:path_style, @path_style) if !path_style && COMPLIANT_BUCKET_NAMES !~ bucket_name Fog::Logger.warning("fog: the specified s3 bucket name(#{bucket_name}) is not a valid dns name, which will negatively impact performance. For details see: http://docs.amazonwebservices.com/AmazonS3/latest/dev/BucketRestrictions.html") path_style = true elsif scheme == 'https' && bucket_name =~ /\./ Fog::Logger.warning("fog: the specified s3 bucket name(#{bucket_name}) contains a '.' so is not accessible over https as a virtual hosted bucket, which will negatively impact performance. For details see: http://docs.amazonwebservices.com/AmazonS3/latest/dev/BucketRestrictions.html") path_style = true end if path_style path = bucket_to_path bucket_name, path else host = [bucket_name, host].join('.') end end ret = params.merge({ :scheme => scheme, :host => host, :port => port, :path => path, :headers => headers }) # ret.delete(:path_style) ret.delete(:bucket_name) ret.delete(:object_name) ret.delete(:region) ret end