2009-05-19 02:06:49 -04:00
module Fog
module AWS
class S3
2009-08-18 01:39:44 -04:00
if Fog . mocking?
def self . reset_data
@data = { :buckets = > { } }
end
def self . data
@data
end
end
2009-08-10 11:30:28 -04:00
def self . reload
2009-09-14 00:24:22 -04:00
load " fog/collection.rb "
load " fog/connection.rb "
load " fog/model.rb "
load " fog/parser.rb "
load " fog/response.rb "
2009-08-10 11:30:28 -04:00
2009-09-14 00:24:22 -04:00
load " fog/aws/models/s3/bucket.rb "
load " fog/aws/models/s3/buckets.rb "
load " fog/aws/models/s3/object.rb "
load " fog/aws/models/s3/objects.rb "
load " fog/aws/models/s3/owner.rb "
2009-08-17 18:58:36 -04:00
2009-09-14 00:24:22 -04:00
load " fog/aws/parsers/s3/copy_object.rb "
load " fog/aws/parsers/s3/get_bucket.rb "
load " fog/aws/parsers/s3/get_bucket_location.rb "
load " fog/aws/parsers/s3/get_request_payment.rb "
load " fog/aws/parsers/s3/get_service.rb "
2009-08-10 11:30:28 -04:00
2009-09-14 00:24:22 -04:00
load " fog/aws/requests/s3/copy_object.rb "
load " fog/aws/requests/s3/delete_bucket.rb "
load " fog/aws/requests/s3/delete_object.rb "
load " fog/aws/requests/s3/get_bucket.rb "
load " fog/aws/requests/s3/get_bucket_location.rb "
load " fog/aws/requests/s3/get_object.rb "
load " fog/aws/requests/s3/get_request_payment.rb "
load " fog/aws/requests/s3/get_service.rb "
load " fog/aws/requests/s3/head_object.rb "
load " fog/aws/requests/s3/put_bucket.rb "
load " fog/aws/requests/s3/put_object.rb "
load " fog/aws/requests/s3/put_request_payment.rb "
2009-08-10 11:30:28 -04:00
2009-08-18 01:39:44 -04:00
if Fog . mocking?
reset_data
end
2009-08-09 01:40:42 -04:00
end
2009-05-19 02:06:49 -04: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
2009-10-04 18:30:44 -04:00
# s3 = S3.new(
2009-07-07 22:20:54 -04:00
# :aws_access_key_id => your_aws_access_key_id,
# :aws_secret_access_key => your_aws_secret_access_key
# )
2009-05-19 02:06:49 -04:00
#
# ==== Parameters
2009-07-07 22:20:54 -04:00
# * options<~Hash> - config arguments for connection. Defaults to {}.
2009-05-19 02:06:49 -04:00
#
# ==== Returns
2009-07-07 22:20:54 -04:00
# * S3 object with connection to aws.
2009-05-19 02:06:49 -04:00
def initialize ( options = { } )
@aws_access_key_id = options [ :aws_access_key_id ]
@aws_secret_access_key = options [ :aws_secret_access_key ]
@hmac = HMAC :: SHA1 . new ( @aws_secret_access_key )
@host = options [ :host ] || 's3.amazonaws.com'
@port = options [ :port ] || 443
@scheme = options [ :scheme ] || 'https'
2009-07-13 21:22:42 -04:00
@connection = Fog :: Connection . new ( " #{ @scheme } :// #{ @host } : #{ @port } " )
2009-05-19 02:06:49 -04:00
end
private
2009-09-08 23:33:37 -04:00
def 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
2009-09-08 23:33:37 -04:00
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 . read
2009-09-10 13:08:29 -04: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
2009-06-25 21:02:00 -04:00
def request ( params )
2009-06-23 23:55:57 -04:00
params [ :headers ] [ 'Date' ] = Time . now . utc . strftime ( " %a, %d %b %Y %H:%M:%S +0000 " )
2009-10-03 18:43:19 -04:00
params [ :headers ] [ 'Authorization' ] = " AWS #{ @aws_access_key_id } : #{ signature ( params ) } "
2009-06-24 12:57:21 -04:00
2009-10-03 18:43:19 -04:00
response = @connection . request ( {
:block = > params [ :block ] ,
:body = > params [ :body ] ,
2009-10-10 22:05:17 -04:00
:error_parser = > Fog :: Errors :: Parser . new ,
2009-10-03 18:43:19 -04:00
:expects = > params [ :expects ] ,
:headers = > params [ :headers ] ,
:host = > params [ :host ] ,
:method = > params [ :method ] ,
:parser = > params [ :parser ] ,
:path = > params [ :path ] ,
:query = > params [ :query ]
} )
response
end
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
def signature ( params )
2009-06-24 21:34:39 -04:00
string_to_sign =
<<-DATA
#{params[:method]}
#{params[:headers]['Content-MD5']}
#{params[:headers]['Content-Type']}
#{params[:headers]['Date']}
DATA
2009-06-24 12:57:21 -04:00
amz_headers , canonical_amz_headers = { } , ''
2009-06-25 03:32:27 -04:00
for key , value in params [ :headers ]
2009-06-24 12:57:21 -04:00
if key [ 0 .. 5 ] == 'x-amz-'
amz_headers [ key ] = value
2009-06-22 13:06:49 -04:00
end
end
2009-06-24 12:57:21 -04:00
amz_headers = amz_headers . sort { | x , y | x [ 0 ] < = > y [ 0 ] }
for pair in amz_headers
2009-06-25 03:32:27 -04:00
canonical_amz_headers << " #{ pair [ 0 ] } : #{ pair [ 1 ] } \n "
2009-06-24 12:57:21 -04:00
end
2009-06-25 03:32:27 -04:00
string_to_sign << " #{ canonical_amz_headers } "
2009-06-24 12:57:21 -04:00
2009-07-22 22:28:53 -04:00
subdomain = params [ :host ] . split ( " . #{ @host } " ) . first
2009-10-03 01:34:40 -04:00
unless subdomain =~ / ^(?:[a-z]| \ d(?! \ d{0,2}(?: \ . \ d{1,3}){3}$))(?:[a-z0-9]| \ .(?![ \ . \ -])| \ -(?![ \ .])){1,61}[a-z0-9]$ /
puts ( " [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 " )
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
2009-10-01 02:32:24 -04:00
canonical_resource << " #{ CGI . escape ( subdomain ) . downcase } / "
2009-06-24 12:57:21 -04:00
end
2009-06-24 23:48:51 -04:00
canonical_resource << " #{ params [ :path ] } "
2009-08-17 21:34:44 -04:00
if [ 'acl' , 'location' , 'logging' , 'requestPayment' , 'torrent' ] . include? ( params [ :query ] )
2009-06-25 03:32:27 -04:00
canonical_resource << " ? #{ params [ :query ] } "
end
2009-10-01 02:32:24 -04:00
string_to_sign << " #{ canonical_resource } "
2009-06-24 12:57:21 -04:00
hmac = @hmac . update ( string_to_sign )
signature = Base64 . encode64 ( hmac . digest ) . chomp!
2009-05-19 02:06:49 -04:00
end
2009-06-05 19:51:17 -04:00
2009-05-19 02:06:49 -04:00
end
end
2009-08-17 18:11:53 -04:00
end
Fog :: AWS :: S3 . reload