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/storage/aws.rb

364 lines
13 KiB
Ruby
Raw Normal View History

2011-01-07 20:11:05 -05:00
module Fog
module AWS
class S3
def self.new(attributes = {})
location = caller.first
warning = "[yellow][WARN] Fog::AWS::S3#new is deprecated, use Fog::AWS::Storage#new instead[/]"
warning << " [light_black](" << location << ")[/] "
Formatador.display_line(warning)
Fog::AWS::Storage.new(attributes)
end
end
end
end
2010-09-08 17:40:02 -04:00
module Fog
module AWS
class Storage < Fog::Service
requires :aws_access_key_id, :aws_secret_access_key
recognizes :endpoint, :region, :host, :path, :port, :scheme, :persistent
2011-01-07 21:14:39 -05:00
recognizes :provider # remove post deprecation
2011-01-21 23:45:37 -05:00
2011-01-07 18:34:20 -05:00
model_path 'fog/storage/models/aws'
2010-09-08 17:40:02 -04:00
collection :directories
model :directory
collection :files
model :file
2011-01-07 18:34:20 -05:00
request_path 'fog/storage/requests/aws'
request :abort_multipart_upload
request :complete_multipart_upload
2010-09-08 17:40:02 -04:00
request :copy_object
request :delete_bucket
request :delete_object
request :get_bucket
request :get_bucket_acl
request :get_bucket_location
request :get_bucket_logging
request :get_bucket_object_versions
request :get_bucket_versioning
request :get_object
request :get_object_acl
request :get_object_torrent
request :get_object_url
request :get_request_payment
request :get_service
request :head_object
request :initiate_multipart_upload
request :list_multipart_uploads
request :list_parts
request :post_object_hidden_fields
2010-09-08 17:40:02 -04:00
request :put_bucket
request :put_bucket_acl
request :put_bucket_logging
request :put_bucket_versioning
request :put_object
request :put_object_acl
2010-09-08 17:40:02 -04:00
request :put_object_url
request :put_request_payment
request :sync_clock
request :upload_part
2010-09-08 17:40:02 -04:00
module Utils
def cdn
@cdn ||= Fog::AWS::CDN.new(
:aws_access_key_id => @aws_access_key_id,
:aws_secret_access_key => @aws_secret_access_key
)
end
2010-09-08 17:40:02 -04:00
def parse_data(data)
metadata = {
:body => nil,
:headers => {}
}
if data.is_a?(String)
metadata[:body] = data
2011-01-21 23:45:37 -05:00
metadata[:headers]['Content-Length'] = metadata[:body].size
2010-09-08 17:40:02 -04:00
else
filename = ::File.basename(data.path)
unless (mime_types = MIME::Types.of(filename)).empty?
metadata[:headers]['Content-Type'] = mime_types.first.content_type
end
metadata[:body] = data
2011-01-21 23:45:37 -05:00
metadata[:headers]['Content-Length'] = ::File.size(data.path)
2010-09-08 17:40:02 -04:00
end
# metadata[:headers]['Content-MD5'] = Base64.encode64(Digest::MD5.digest(metadata[:body])).strip
metadata
end
def url(params, expires)
params[:headers]['Date'] = expires.to_i
params[:path] = CGI.escape(params[:path]).gsub('%2F', '/')
2010-09-08 17:40:02 -04:00
query = [params[:query]].compact
query << "AWSAccessKeyId=#{@aws_access_key_id}"
query << "Signature=#{CGI.escape(signature(params))}"
query << "Expires=#{params[:headers]['Date']}"
bucket = params[:host].split('.').first
"https://#{@host}/#{params[:path]}?#{query.join('&')}"
2010-09-08 17:40:02 -04:00
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",
"Grantee" => {"DisplayName" => "me", "ID" => "2744ccd10c7533bd736ad890f9dd5cab2adb27b07d500b9493f29cdc420cb2e0"}
}
],
"Owner" => {"DisplayName" => "me", "ID" => "2744ccd10c7533bd736ad890f9dd5cab2adb27b07d500b9493f29cdc420cb2e0"}
}
when 'public-read'
2011-01-21 23:45:37 -05:00
{
"AccessControlList" => [
{
"Permission" => "FULL_CONTROL",
"Grantee" => {"DisplayName" => "me", "ID" => "2744ccd10c7533bd736ad890f9dd5cab2adb27b07d500b9493f29cdc420cb2e0"}
},
{
"Permission" => "READ",
"Grantee" => {"URI" => "http://acs.amazonaws.com/groups/global/AllUsers"}
}
],
"Owner" => {"DisplayName" => "me", "ID" => "2744ccd10c7533bd736ad890f9dd5cab2adb27b07d500b9493f29cdc420cb2e0"}
}
when 'public-read-write'
2011-01-21 23:45:37 -05:00
{
"AccessControlList" => [
{
"Permission" => "FULL_CONTROL",
"Grantee" => {"DisplayName" => "me", "ID" => "2744ccd10c7533bd736ad890f9dd5cab2adb27b07d500b9493f29cdc420cb2e0"}
},
{
"Permission" => "READ",
"Grantee" => {"URI" => "http://acs.amazonaws.com/groups/global/AllUsers"}
},
{
"Permission" => "WRITE",
"Grantee" => {"URI" => "http://acs.amazonaws.com/groups/global/AllUsers"}
}
],
"Owner" => {"DisplayName" => "me", "ID" => "2744ccd10c7533bd736ad890f9dd5cab2adb27b07d500b9493f29cdc420cb2e0"}
}
when 'authenticated-read'
2011-01-21 23:45:37 -05:00
{
"AccessControlList" => [
{
"Permission" => "FULL_CONTROL",
"Grantee" => {"DisplayName" => "me", "ID" => "2744ccd10c7533bd736ad890f9dd5cab2adb27b07d500b9493f29cdc420cb2e0"}
},
{
"Permission" => "READ",
"Grantee" => {"URI" => "http://acs.amazonaws.com/groups/global/AuthenticatedUsers"}
}
],
"Owner" => {"DisplayName" => "me", "ID" => "2744ccd10c7533bd736ad890f9dd5cab2adb27b07d500b9493f29cdc420cb2e0"}
}
end
end
2010-09-08 17:40:02 -04:00
def self.data
2010-09-24 16:50:44 -04:00
@data ||= Hash.new do |hash, region|
2010-10-13 16:20:18 -04:00
hash[region] = Hash.new do |region_hash, key|
region_hash[key] = {
:acls => {
:bucket => {},
:object => {}
},
2010-09-24 16:50:44 -04:00
:buckets => {}
}
end
2010-09-08 17:40:02 -04:00
end
end
def self.reset_data(keys=data.keys)
for key in [*keys]
data.delete(key)
end
end
def initialize(options={})
2011-01-07 21:14:39 -05:00
unless options.delete(:provider)
location = caller.first
warning = "[yellow][WARN] Fog::AWS::Storage.new is deprecated, use Fog::Storage.new(:provider => 'AWS') instead[/]"
warning << " [light_black](" << location << ")[/] "
Formatador.display_line(warning)
end
require 'mime/types'
2010-09-08 17:40:02 -04:00
@aws_access_key_id = options[:aws_access_key_id]
@aws_secret_access_key = options[:aws_secret_access_key]
options[:region] ||= 'us-east-1'
@host = options[:host] || case options[:region]
when 'eu-west-1'
's3-eu-west-1.amazonaws.com'
when 'us-east-1'
's3.amazonaws.com'
when 'ap-southeast-1'
's3-ap-southeast-1.amazonaws.com'
when 'us-west-1'
's3-us-west-1.amazonaws.com'
else
raise ArgumentError, "Unknown region: #{options[:region].inspect}"
end
@data = self.class.data[options[:region]][@aws_access_key_id]
2010-09-08 17:40:02 -04:00
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
2010-09-08 17:40:02 -04:00
class Real
include Utils
extend Fog::Deprecation
deprecate(:reset, :reload)
# Initialize connection to S3
#
# ==== Notes
# options parameter must include values for :aws_access_key_id and
2010-09-08 17:40:02 -04:00
# :aws_secret_access_key in order to create a connection
#
# ==== Examples
# s3 = S3.new(
# :aws_access_key_id => your_aws_access_key_id,
# :aws_secret_access_key => your_aws_secret_access_key
# )
#
# ==== Parameters
# * options<~Hash> - config arguments for connection. Defaults to {}.
#
# ==== Returns
# * S3 object with connection to aws.
def initialize(options={})
2011-01-07 21:14:39 -05:00
unless options.delete(:provider)
location = caller.first
warning = "[yellow][WARN] Fog::AWS::Storage.new is deprecated, use Fog::Storage.new(:provider => 'AWS') instead[/]"
warning << " [light_black](" << location << ")[/] "
Formatador.display_line(warning)
end
require 'mime/types'
2010-09-08 17:40:02 -04:00
@aws_access_key_id = options[:aws_access_key_id]
@aws_secret_access_key = options[:aws_secret_access_key]
@hmac = Fog::HMAC.new('sha1', @aws_secret_access_key)
if @endpoint = options[:endpoint]
endpoint = URI.parse(@endpoint)
@host = endpoint.host
@path = endpoint.path
@port = endpoint.port
@scheme = endpoint.scheme
else
options[:region] ||= 'us-east-1'
@host = options[:host] || case options[:region]
2010-09-23 13:48:52 -04:00
when 'eu-west-1'
's3-eu-west-1.amazonaws.com'
2010-09-08 17:40:02 -04:00
when 'us-east-1'
's3.amazonaws.com'
when 'ap-southeast-1'
's3-ap-southeast-1.amazonaws.com'
when 'us-west-1'
's3-us-west-1.amazonaws.com'
else
raise ArgumentError, "Unknown region: #{options[:region].inspect}"
end
@path = options[:path] || '/'
@port = options[:port] || 443
@scheme = options[:scheme] || 'https'
end
@connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}#{@path}", options[:persistent] || true)
2010-09-08 17:40:02 -04:00
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
amz_headers, canonical_amz_headers = {}, ''
for key, value in params[:headers]
if key[0..5] == 'x-amz-'
amz_headers[key] = value
end
end
amz_headers = amz_headers.sort {|x, y| x[0] <=> y[0]}
for key, value in amz_headers
canonical_amz_headers << "#{key}:#{value}\n"
end
string_to_sign << canonical_amz_headers
2010-09-08 17:40:02 -04:00
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]$/
Formatador.display_line("[yellow][WARN] fog: the specified s3 bucket name(#{subdomain}) is not a valid dns name, which will negatively impact performance. For details see: http://docs.amazonwebservices.com/AmazonS3/latest/dev/BucketRestrictions.html[/]")
2010-09-08 17:40:02 -04:00
params[:host] = params[:host].split("#{subdomain}.")[-1]
if params[:path]
params[:path] = "#{subdomain}/#{params[:path]}"
else
params[:path] = subdomain
2010-09-08 17:40:02 -04:00
end
subdomain = nil
end
canonical_resource = @path.dup
2010-09-08 17:40:02 -04:00
unless subdomain.nil? || subdomain == @host
canonical_resource << "#{CGI.escape(subdomain).downcase}/"
end
canonical_resource << params[:path].to_s
2010-09-08 17:40:02 -04:00
canonical_resource << '?'
for key in (params[:query] || {}).keys.sort
if %w{acl location logging notification partNumber policy requestPayment torrent uploadId uploads versionId versioning versions}.include?(key)
canonical_resource << "#{key}#{"=#{params[:query][key]}" unless params[:query][key].nil?}&"
2010-09-08 17:40:02 -04:00
end
end
canonical_resource.chop!
string_to_sign << canonical_resource
2010-09-08 17:40:02 -04:00
signed_string = @hmac.sign(string_to_sign)
signature = Base64.encode64(signed_string).chomp!
end
private
def request(params, &block)
params[:headers]['Date'] = Fog::Time.now.to_date_header
params[:headers]['Authorization'] = "AWS #{@aws_access_key_id}:#{signature(params)}"
params[:expects] = [307, *params[:expects]].flatten
# FIXME: ToHashParser should make this not needed
original_params = params.dup
response = @connection.request(params, &block)
if response.status == 307
uri = URI.parse(response.headers['Location'])
Formatador.display_line("[yellow][WARN] fog: followed redirect to #{uri.host}, connecting to the matching region will be more performant[/]")
response = Fog::Connection.new("#{@scheme}://#{uri.host}:#{@port}", false).request(original_params, &block)
end
response
end
2010-09-08 17:40:02 -04:00
end
end
end
end