fog--fog/lib/fog/aws/s3.rb

212 lines
6.7 KiB
Ruby
Raw Normal View History

2009-05-19 06:06:49 +00:00
module Fog
module AWS
class S3 < Fog::Service
2010-06-12 22:31:17 +00:00
requires :aws_access_key_id, :aws_secret_access_key
model_path 'fog/aws/models/s3'
collection :directories
model :directory
collection :files
model :file
2010-06-12 22:31:17 +00:00
request_path 'fog/aws/requests/s3'
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 :put_bucket
request :put_bucket_acl
request :put_bucket_logging
request :put_bucket_versioning
request :put_object
request :put_object_url
request :put_request_payment
2009-08-10 15:30:28 +00:00
2010-06-12 22:31:17 +00:00
module Utils
2009-05-19 06:06:49 +00:00
2010-06-12 22:31:17 +00:00
def parse_data(data)
metadata = {
:body => nil,
:headers => {}
}
if data.is_a?(String)
metadata[:body] = data
metadata[:headers]['Content-Length'] = metadata[:body].size.to_s
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
metadata[:headers]['Content-Length'] = ::File.size(data.path).to_s
end
2010-06-12 22:31:17 +00:00
# metadata[:headers]['Content-MD5'] = Base64.encode64(Digest::MD5.digest(metadata[:body])).strip
metadata
end
2010-04-20 17:04:38 +00:00
def url(params, expires)
params[:headers]['Date'] = expires.to_i
query = [params[:query]].compact
query << "AWSAccessKeyId=#{@aws_access_key_id}"
query << "Signature=#{CGI.escape(signature(params))}"
query << "Expires=#{params[:headers]['Date']}"
"http://#{params[:host]}/#{params[:path]}?#{query.join('&')}"
end
2010-06-12 22:31:17 +00:00
2010-04-20 17:04:38 +00:00
end
2010-03-13 21:37:24 +00:00
class Mock
2010-06-12 22:31:17 +00:00
include Collections
2010-04-20 17:04:38 +00:00
include Utils
2010-03-17 23:07:25 +00:00
def self.data
@data ||= Hash.new do |hash, key|
hash[key] = {
:buckets => {}
}
end
end
def self.reset_data(keys=data.keys)
for key in [*keys]
data.delete(key)
end
2010-03-13 21:37:24 +00:00
end
2010-03-13 21:37:24 +00:00
def initialize(options={})
2010-03-17 23:07:25 +00:00
@aws_access_key_id = options[:aws_access_key_id]
@data = self.class.data[@aws_access_key_id]
2010-03-13 21:37:24 +00:00
end
2010-04-20 17:04:38 +00:00
def signature(params)
"foo"
end
end
2010-03-13 21:37:24 +00:00
class Real
2010-06-12 22:31:17 +00:00
include Collections
2010-04-20 17:04:38 +00:00
include Utils
extend Fog::Deprecation
deprecate(:reset, :reload)
2010-03-13 21:37:24 +00:00
# Initialize connection to S3
#
# ==== Notes
# options parameter must include values for :aws_access_key_id and
# :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={})
2010-06-12 22:31:17 +00:00
@aws_access_key_id = options[:aws_access_key_id]
@aws_secret_access_key = options[:aws_secret_access_key]
2010-06-16 04:04:16 +00:00
@hmac = Fog::HMAC.new('sha1', @aws_secret_access_key)
options[:region] ||= 'us-east-1'
2010-06-16 04:04:16 +00:00
@host = options[:host] || case options[:region]
when 'us-east-1'
's3.amazonaws.com'
2010-05-01 22:18:50 +00:00
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}"
2010-05-01 22:18:50 +00:00
end
2010-06-16 04:04:16 +00:00
@port = options[:port] || 443
@scheme = options[:scheme] || 'https'
@connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}", options[:persistent] || true)
end
def reload
@connection.reset
2010-03-13 21:37:24 +00:00
end
private
def request(params, &block)
2010-03-13 21:37:24 +00:00
params[:headers]['Date'] = Time.now.utc.strftime("%a, %d %b %Y %H:%M:%S +0000")
params[:headers]['Authorization'] = "AWS #{@aws_access_key_id}:#{signature(params)}"
response = @connection.request(params, &block)
2010-03-13 21:37:24 +00:00
response
end
def signature(params)
string_to_sign =
<<-DATA
#{params[:method]}
#{params[:headers]['Content-MD5']}
#{params[:headers]['Content-Type']}
#{params[:headers]['Date']}
DATA
2010-03-13 21:37:24 +00:00
amz_headers, canonical_amz_headers = {}, ''
for key, value in params[:headers]
if key[0..5] == 'x-amz-'
amz_headers[key] = value
end
2009-06-22 17:06:49 +00:00
end
2010-03-13 21:37:24 +00:00
amz_headers = amz_headers.sort {|x, y| x[0] <=> y[0]}
for key, value in amz_headers
canonical_amz_headers << "#{key}:#{value}\n"
2010-03-13 21:37:24 +00:00
end
string_to_sign << "#{canonical_amz_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]$/
2010-04-23 22:20:52 +00:00
Formatador.display_line("[yellow][WARN] fog: the specified s3 bucket name(#{subdomain}) is not a valid dns name. See: http://docs.amazonwebservices.com/AmazonS3/latest/dev/index.html?Introduction.html[/]")
2010-03-13 21:37:24 +00:00
params[:host] = params[:host].split("#{subdomain}.")[-1]
if params[:path]
params[:path] = "#{subdomain}/#{params[:path]}"
else
params[:path] = "#{subdomain}"
end
subdomain = nil
end
2010-03-13 21:37:24 +00:00
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
2010-03-13 21:37:24 +00:00
end
canonical_resource.chop!
2010-03-13 21:37:24 +00:00
string_to_sign << "#{canonical_resource}"
2010-06-16 04:04:16 +00:00
signed_string = @hmac.sign(string_to_sign)
2010-06-16 03:28:18 +00:00
signature = Base64.encode64(signed_string).chomp!
end
2009-05-19 06:06:49 +00:00
end
end
end
2009-08-17 22:11:53 +00:00
end