2009-05-19 02:06:49 -04:00
module Fog
module AWS
2010-03-13 16:37:24 -05:00
module S3
2009-08-10 11:30:28 -04:00
2010-03-13 16:37:24 -05:00
def self . new ( options = { } )
2010-04-03 03:33:35 -04:00
2010-04-13 15:40:46 -04:00
unless @required
require 'fog/aws/models/s3/directories'
require 'fog/aws/models/s3/directory'
require 'fog/aws/models/s3/files'
require 'fog/aws/models/s3/file'
2010-05-09 21:50:10 -04:00
require 'fog/aws/parsers/s3/access_control_list'
2010-04-13 17:35:30 -04:00
require 'fog/aws/parsers/s3/copy_object'
require 'fog/aws/parsers/s3/get_bucket'
require 'fog/aws/parsers/s3/get_bucket_location'
2010-05-11 20:39:07 -04:00
require 'fog/aws/parsers/s3/get_bucket_logging'
2010-05-10 23:28:27 -04:00
require 'fog/aws/parsers/s3/get_bucket_versioning'
2010-04-13 17:35:30 -04:00
require 'fog/aws/parsers/s3/get_request_payment'
require 'fog/aws/parsers/s3/get_service'
2010-04-13 15:40:46 -04:00
require 'fog/aws/requests/s3/copy_object'
require 'fog/aws/requests/s3/delete_bucket'
require 'fog/aws/requests/s3/delete_object'
require 'fog/aws/requests/s3/get_bucket'
2010-05-09 21:50:10 -04:00
require 'fog/aws/requests/s3/get_bucket_acl'
2010-04-13 15:40:46 -04:00
require 'fog/aws/requests/s3/get_bucket_location'
2010-05-11 20:39:07 -04:00
require 'fog/aws/requests/s3/get_bucket_logging'
2010-05-10 23:28:27 -04:00
require 'fog/aws/requests/s3/get_bucket_versioning'
2010-04-13 15:40:46 -04:00
require 'fog/aws/requests/s3/get_object'
2010-05-09 21:50:10 -04:00
require 'fog/aws/requests/s3/get_object_acl'
require 'fog/aws/requests/s3/get_object_torrent'
require 'fog/aws/requests/s3/get_object_url'
2010-04-13 15:40:46 -04:00
require 'fog/aws/requests/s3/get_request_payment'
require 'fog/aws/requests/s3/get_service'
require 'fog/aws/requests/s3/head_object'
require 'fog/aws/requests/s3/put_bucket'
2010-05-09 21:50:10 -04:00
require 'fog/aws/requests/s3/put_bucket_acl'
2010-05-11 20:39:07 -04:00
require 'fog/aws/requests/s3/put_bucket_logging'
2010-05-10 23:28:27 -04:00
require 'fog/aws/requests/s3/put_bucket_versioning'
2010-04-13 15:40:46 -04:00
require 'fog/aws/requests/s3/put_object'
require 'fog/aws/requests/s3/put_request_payment'
@required = true
end
2010-04-03 03:33:35 -04:00
2009-08-18 01:39:44 -04:00
if Fog . mocking?
2010-03-13 16:37:24 -05:00
Fog :: AWS :: S3 :: Mock . new ( options )
else
Fog :: AWS :: S3 :: Real . new ( options )
2010-01-22 23:50:59 -05:00
end
2009-05-19 02:06:49 -04:00
end
2010-03-13 16:37:24 -05:00
def self . parse_data ( data )
2009-06-07 18:42:51 -04:00
metadata = {
:body = > nil ,
:headers = > { }
}
2009-09-09 23:44:28 -04:00
if data . is_a? ( String )
metadata [ :body ] = data
metadata [ :headers ] [ 'Content-Length' ] = metadata [ :body ] . size . to_s
else
2010-01-14 23:44:39 -05:00
filename = :: File . basename ( data . path )
2009-09-08 23:33:37 -04:00
unless ( mime_types = MIME :: Types . of ( filename ) ) . empty?
metadata [ :headers ] [ 'Content-Type' ] = mime_types . first . content_type
end
metadata [ :body ] = data . read
2010-01-14 23:44:39 -05:00
metadata [ :headers ] [ 'Content-Length' ] = :: File . size ( data . path ) . to_s
2009-09-08 23:33:37 -04:00
end
2009-09-09 23:44:28 -04:00
# metadata[:headers]['Content-MD5'] = Base64.encode64(Digest::MD5.digest(metadata[:body])).strip
2009-06-07 18:42:51 -04:00
metadata
end
2010-03-17 19:07:25 -04:00
def self . reset_data ( keys = Mock . data . keys )
Mock . reset_data ( keys )
end
2010-04-20 13:04:38 -04:00
module Utils
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
end
2010-03-13 16:37:24 -05:00
class Mock
2010-04-20 13:04:38 -04:00
include Utils
2009-06-24 12:57:21 -04:00
2010-03-17 19:07:25 -04: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 16:37:24 -05:00
end
2009-10-03 18:43:19 -04:00
2010-03-13 16:37:24 -05:00
def initialize ( options = { } )
2010-03-17 19:07:25 -04:00
@aws_access_key_id = options [ :aws_access_key_id ]
@data = self . class . data [ @aws_access_key_id ]
2010-03-13 16:37:24 -05:00
end
2009-10-03 18:43:19 -04:00
2010-04-20 13:04:38 -04:00
def signature ( params )
" foo "
end
2009-10-03 18:43:19 -04:00
end
2010-03-13 16:37:24 -05:00
class Real
2010-04-20 13:04:38 -04:00
include Utils
2010-03-13 16:37:24 -05: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 = { } )
unless @aws_access_key_id = options [ :aws_access_key_id ]
raise ArgumentError . new ( 'aws_access_key_id is required to access ec2' )
end
unless @aws_secret_access_key = options [ :aws_secret_access_key ]
raise ArgumentError . new ( 'aws_secret_access_key is required to access ec2' )
end
@hmac = HMAC :: SHA1 . new ( @aws_secret_access_key )
2010-05-01 18:18:50 -04:00
@host = options [ :host ] || case options [ :region ]
when 'ap-southeast-1'
's3-ap-southeast-1.amazonaws.com'
when 'us-west-1'
's3-us-west-1.amazonaws.com'
else
's3.amazonaws.com'
end
2010-03-13 16:37:24 -05:00
@port = options [ :port ] || 443
@scheme = options [ :scheme ] || 'https'
end
private
2010-05-09 19:23:03 -04:00
def request ( params , & block )
2010-03-13 16:37:24 -05:00
@connection = Fog :: Connection . new ( " #{ @scheme } :// #{ @host } : #{ @port } " )
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 ) } "
2010-05-09 19:23:03 -04:00
response = @connection . request ( params , & block )
2010-03-13 16:37:24 -05:00
response
end
def signature ( params )
string_to_sign =
2009-06-24 21:34:39 -04:00
<<-DATA
#{params[:method]}
#{params[:headers]['Content-MD5']}
#{params[:headers]['Content-Type']}
#{params[:headers]['Date']}
DATA
2009-06-24 12:57:21 -04:00
2010-03-13 16:37:24 -05: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 13:06:49 -04:00
end
2010-03-13 16:37:24 -05:00
amz_headers = amz_headers . sort { | x , y | x [ 0 ] < = > y [ 0 ] }
for pair in amz_headers
canonical_amz_headers << " #{ pair [ 0 ] } : #{ pair [ 1 ] } \n "
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 18:20:52 -04: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 16:37:24 -05:00
params [ :host ] = params [ :host ] . split ( " #{ subdomain } . " ) [ - 1 ]
if params [ :path ]
params [ :path ] = " #{ subdomain } / #{ params [ :path ] } "
else
params [ :path ] = " #{ subdomain } "
end
subdomain = nil
2009-10-03 01:34:40 -04:00
end
2010-03-13 16:37:24 -05:00
canonical_resource = " / "
unless subdomain . nil? || subdomain == @host
canonical_resource << " #{ CGI . escape ( subdomain ) . downcase } / "
end
canonical_resource << " #{ params [ :path ] } "
2010-05-10 23:28:27 -04:00
if params [ :query ]
2010-03-13 16:37:24 -05:00
canonical_resource << " ? #{ params [ :query ] } "
end
string_to_sign << " #{ canonical_resource } "
hmac = @hmac . update ( string_to_sign )
signature = Base64 . encode64 ( hmac . digest ) . chomp!
2009-06-24 12:57:21 -04:00
end
2009-05-19 02:06:49 -04:00
end
end
end
2009-08-17 18:11:53 -04:00
end