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

272 lines
8.8 KiB
Ruby
Raw Normal View History

2012-05-15 14:54:20 -04:00
require 'fog/google'
require 'fog/storage'
module Fog
module Storage
class Google < Fog::Service
requires :google_storage_access_key_id, :google_storage_secret_access_key
recognizes :host, :port, :scheme, :persistent
model_path 'fog/google/models/storage'
collection :directories
model :directory
collection :files
model :file
request_path 'fog/google/requests/storage'
request :copy_object
request :delete_bucket
request :delete_object
request :get_bucket
request :get_bucket_acl
request :get_object
request :get_object_acl
request :get_object_torrent
request :get_object_http_url
request :get_object_https_url
request :get_object_url
request :get_service
request :head_object
request :put_bucket
request :put_bucket_acl
request :put_object
request :put_object_url
module Utils
def http_url(params, expires)
"http://" << host_path_query(params, expires)
end
def https_url(params, expires)
"https://" << host_path_query(params, expires)
end
def url(params, expires)
Fog::Logger.deprecation("Fog::Storage::Google => #url is deprecated, use #https_url instead [light_black](#{caller.first})[/]")
https_url(params, expires)
end
private
def host_path_query(params, expires)
params[:headers]['Date'] = expires.to_i
params[:path] = CGI.escape(params[:path]).gsub('%2F', '/')
query = [params[:query]].compact
query << "GoogleAccessKeyId=#{@google_storage_access_key_id}"
query << "Signature=#{CGI.escape(signature(params))}"
query << "Expires=#{params[:headers]['Date']}"
"#{params[:host]}/#{params[:path]}?#{query.join('&')}"
end
end
class Mock
include Utils
def self.acls(type)
case type
when 'private'
2011-01-21 23:45:37 -05:00
{
"AccessControlList"=> [
{
"Permission" => "FULL_CONTROL",
"Scope" => {"ID" => "2744ccd10c7533bd736ad890f9dd5cab2adb27b07d500b9493f29cdc420cb2e0", "type" => "UserById"}
}
],
"Owner" => {"ID" => "2744ccd10c7533bd736ad890f9dd5cab2adb27b07d500b9493f29cdc420cb2e0"}
}
when 'public-read'
2011-01-21 23:45:37 -05:00
{
"AccessControlList"=> [
{
"Permission" => "FULL_CONTROL",
"Scope" => {"ID" => "2744ccd10c7533bd736ad890f9dd5cab2adb27b07d500b9493f29cdc420cb2e0", "type" => "UserById"}
},
{
"Permission" => "READ",
"Scope" => {"type" => "AllUsers"}
}
],
"Owner" => {"ID" => "2744ccd10c7533bd736ad890f9dd5cab2adb27b07d500b9493f29cdc420cb2e0"}
}
when 'public-read-write'
2011-01-21 23:45:37 -05:00
{
"AccessControlList"=> [
{
"Permission" => "FULL_CONTROL",
"Scope" => {"ID" => "2744ccd10c7533bd736ad890f9dd5cab2adb27b07d500b9493f29cdc420cb2e0", "type" => "UserById"}
},
{
"Permission" => "READ",
"Scope" => {"type" => "AllUsers"}
},
{
"Permission" => "WRITE",
"Scope" => {"type" => "AllUsers"}
}
],
"Owner" => {"ID" => "2744ccd10c7533bd736ad890f9dd5cab2adb27b07d500b9493f29cdc420cb2e0"}
}
when 'authenticated-read'
2011-01-21 23:45:37 -05:00
{
"AccessControlList"=> [
{
"Permission" => "FULL_CONTROL",
"Scope" => {"ID" => "2744ccd10c7533bd736ad890f9dd5cab2adb27b07d500b9493f29cdc420cb2e0", "type" => "UserById"}
},
{
"Permission" => "READ",
"Scope" => {"type" => "AllAuthenticatedUsers"}
}
],
"Owner" => {"ID" => "2744ccd10c7533bd736ad890f9dd5cab2adb27b07d500b9493f29cdc420cb2e0"}
}
end
end
def self.data
@data ||= Hash.new do |hash, key|
hash[key] = {
:acls => {
:bucket => {},
:object => {}
},
:buckets => {}
}
end
end
2011-05-19 18:35:33 -04:00
def self.reset
@data = nil
end
def initialize(options={})
require 'mime/types'
@google_storage_access_key_id = options[:google_storage_access_key_id]
end
2011-05-19 18:35:33 -04:00
def data
self.class.data[@google_storage_access_key_id]
end
def reset_data
self.class.data.delete(@google_storage_access_key_id)
end
def signature(params)
"foo"
end
end
This is a combination of 23 commits (included merges from upstream); this commit(s) include changes to enforces recognizes/requires parameters for all supported services. Comments from the included commits follow: - Added google_storage_* keys - Fixed indentations. - Factored out requires and recognizes method implementation (now relies on the requires and recognizes clause from the NamedParameters module) - Added dependency to named-parameters gem. - Added recognizes declaration to classes for all supported services to enforce parameter name checks - - passing an unrecognized key when instantiating a service object will now cause an ArgumentError to be raised. - Added NOTE - comment added - check/filter-out keys from credentials that are not required by the class being instantiated - [local|storage] properly write out file contents - Added google_storage_* keys - Fixed indentations. - added put_object_acl request (ref: https://github.com/geemus/fog/issues#issue/74) - Release 0.3.24 - remove tracker reference from README - issues is now the goto for bugs/todo - notify and gracefully skip credential-less testsa - [rackspace|storage] fixes for directory/files - [local|storage] CGI.escape file names - Release 0.3.25 - updated deps; recognized_parameters -> declared_parameters; restored options filtering if Fog.bin - Added requires/recognizes to Fog::Terremark::Ecloud - Updted to use latest named-parameters gem. - Filter out unwanted parameters when Fog.bin - Updated to latest named-parameters gem - commented out unnecessary code - fix missing "volume" parameter error when setting Fog::AWS::Volume#server to nil (in order to detach it) - documentation update for key_pairs and helper - [aws|compute] commented/documented flavors/volumes - Fixes for issue 38 and 39 Closes #96
2010-11-21 05:56:41 -05:00
class Real
include Utils
# Initialize connection to Google Storage
#
# ==== Notes
# options parameter must include values for :google_storage_access_key_id and
# :google_storage_secret_access_key in order to create a connection
#
# ==== Examples
# google_storage = Storage.new(
# :google_storage_access_key_id => your_google_storage_access_key_id,
# :google_storage_secret_access_key => your_google_storage_secret_access_key
# )
#
# ==== Parameters
# * options<~Hash> - config arguments for connection. Defaults to {}.
#
# ==== Returns
# * Storage object with connection to google.
def initialize(options={})
2011-02-16 20:25:50 -05:00
require 'fog/core/parser'
require 'mime/types'
2011-02-16 20:25:50 -05:00
@google_storage_access_key_id = options[:google_storage_access_key_id]
@google_storage_secret_access_key = options[:google_storage_secret_access_key]
@connection_options = options[:connection_options] || {}
@hmac = Fog::HMAC.new('sha1', @google_storage_secret_access_key)
@host = options[:host] || 'commondatastorage.googleapis.com'
@persistent = options.fetch(:persistent, true)
@port = options[:port] || 443
@scheme = options[:scheme] || 'https'
@connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}", @persistent, @connection_options)
end
def reload
@connection.reset
end
def signature(params)
string_to_sign =
<<-DATA
#{params[:method]}
#{params[:headers]['Content-MD5']}
#{params[:headers]['Content-Type']}
#{params[:headers]['Date']}
DATA
google_headers, canonical_google_headers = {}, ''
for key, value in params[:headers]
2010-09-18 15:57:37 -04:00
if key[0..6] == 'x-goog-'
google_headers[key] = value
end
end
2010-09-18 15:57:37 -04:00
google_headers = google_headers.sort {|x, y| x[0] <=> y[0]}
for key, value in google_headers
canonical_google_headers << "#{key}:#{value}\n"
end
string_to_sign << "#{canonical_google_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]$/
Fog::Logger.warning("fog: the specified google storage bucket name(#{subdomain}) is not a valid dns name. See: http://code.google.com/apis/storage/docs/developer-guide.html#naming")
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]}"
canonical_resource << '?'
for key in (params[:query] || {}).keys
if ['acl', 'location', 'logging', 'requestPayment', 'torrent', 'versions', 'versioning'].include?(key)
canonical_resource << "#{key}&"
end
end
canonical_resource.chop!
string_to_sign << "#{canonical_resource}"
signed_string = @hmac.sign(string_to_sign)
2012-05-14 14:58:42 -04:00
Base64.encode64(signed_string).chomp!
end
private
def request(params, &block)
params[:headers]['Date'] = Fog::Time.now.to_date_header
params[:headers]['Authorization'] = "GOOG1 #{@google_storage_access_key_id}:#{signature(params)}"
response = @connection.request(params, &block)
response
end
end
end
end
end