2011-08-31 16:52:53 -04:00
require File . expand_path ( File . join ( File . dirname ( __FILE__ ) , '..' , 'aws' ) )
require 'fog/storage'
2010-09-08 17:40:02 -04:00
module Fog
2011-06-15 17:26:43 -04:00
module Storage
class AWS < Fog :: Service
2010-09-08 17:40:02 -04:00
2010-12-16 18:31:24 -05:00
requires :aws_access_key_id , :aws_secret_access_key
recognizes :endpoint , :region , :host , :path , :port , :scheme , :persistent
2011-01-21 23:45:37 -05:00
2011-08-24 14:50:42 -04:00
model_path 'fog/aws/models/storage'
2010-09-08 17:40:02 -04:00
collection :directories
model :directory
collection :files
model :file
2011-08-24 14:50:42 -04:00
request_path 'fog/aws/requests/storage'
2010-11-15 20:17:37 -05:00
request :abort_multipart_upload
request :complete_multipart_upload
2010-09-08 17:40:02 -04:00
request :copy_object
request :delete_bucket
2011-06-07 21:41:24 -04:00
request :delete_bucket_policy
2011-02-18 15:06:51 -05:00
request :delete_bucket_website
2010-09-08 17:40:02 -04:00
request :delete_object
request :get_bucket
request :get_bucket_acl
request :get_bucket_location
request :get_bucket_logging
request :get_bucket_object_versions
2011-06-07 21:41:24 -04:00
request :get_bucket_policy
2010-09-08 17:40:02 -04:00
request :get_bucket_versioning
2011-02-18 15:06:51 -05:00
request :get_bucket_website
2010-09-08 17:40:02 -04:00
request :get_object
request :get_object_acl
request :get_object_torrent
2011-06-27 17:50:32 -04:00
request :get_object_http_url
request :get_object_https_url
2010-09-08 17:40:02 -04:00
request :get_object_url
request :get_request_payment
request :get_service
request :head_object
2010-11-15 20:17:37 -05:00
request :initiate_multipart_upload
request :list_multipart_uploads
request :list_parts
2010-10-18 20:56:01 -04:00
request :post_object_hidden_fields
2010-09-08 17:40:02 -04:00
request :put_bucket
request :put_bucket_acl
request :put_bucket_logging
2011-06-07 21:41:24 -04:00
request :put_bucket_policy
2010-09-08 17:40:02 -04:00
request :put_bucket_versioning
2011-02-18 15:06:51 -05:00
request :put_bucket_website
2010-09-08 17:40:02 -04:00
request :put_object
2010-11-22 02:44:44 -05:00
request :put_object_acl
2010-09-08 17:40:02 -04:00
request :put_object_url
request :put_request_payment
2010-12-21 15:12:30 -05:00
request :sync_clock
2010-11-15 20:17:37 -05:00
request :upload_part
2010-09-08 17:40:02 -04:00
module Utils
2011-08-04 10:41:02 -04:00
attr_accessor :region
2010-11-05 14:37:12 -04:00
def cdn
@cdn || = Fog :: AWS :: CDN . new (
:aws_access_key_id = > @aws_access_key_id ,
:aws_secret_access_key = > @aws_secret_access_key
)
end
2011-06-27 17:50:32 -04:00
def http_url ( params , expires )
" http:// " << host_path_query ( params , expires )
end
def https_url ( params , expires )
" https:// " << host_path_query ( params , expires )
end
2010-09-08 17:40:02 -04:00
def url ( params , expires )
2011-10-19 15:49:34 -04:00
Fog :: Logger . deprecation ( " Fog::Storage::AWS => # url is deprecated, use # https_url instead [light_black]( #{ caller . first } )[/] " )
2011-06-27 17:50:32 -04:00
https_url ( params , expires )
end
private
def host_path_query ( params , expires )
2011-06-07 18:28:57 -04:00
params [ :headers ] || = { }
2010-09-08 17:40:02 -04:00
params [ :headers ] [ 'Date' ] = expires . to_i
2011-06-07 18:25:12 -04:00
params [ :path ] = Fog :: AWS . escape ( params [ :path ] ) . gsub ( '%2F' , '/' )
query = [ ]
2011-06-08 13:37:52 -04:00
if params [ :query ]
for key , value in params [ :query ]
query << " #{ key } = #{ Fog :: AWS . escape ( value ) } "
end
2011-06-07 18:25:12 -04:00
end
2010-09-08 17:40:02 -04:00
query << " AWSAccessKeyId= #{ @aws_access_key_id } "
2011-06-07 18:28:38 -04:00
query << " Signature= #{ Fog :: AWS . escape ( signature ( params ) ) } "
2010-09-08 17:40:02 -04:00
query << " Expires= #{ params [ :headers ] [ 'Date' ] } "
2011-06-27 17:50:32 -04:00
" #{ @host } / #{ params [ :path ] } ? #{ query . join ( '&' ) } "
2010-09-08 17:40:02 -04:00
end
2011-08-04 10:41:02 -04:00
2010-09-08 17:40:02 -04:00
end
class Mock
include Utils
2010-11-18 17:17:11 -05:00
def self . acls ( type )
case type
when 'private'
2011-01-21 23:45:37 -05:00
{
2010-11-18 17:17:11 -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 " }
}
2010-11-18 17:17:11 -05:00
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 " }
}
2010-11-18 17:17:11 -05:00
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 " }
}
2010-11-18 17:17:11 -05:00
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 ] = {
2010-11-18 17:17:11 -05:00
:acls = > {
:bucket = > { } ,
:object = > { }
} ,
2010-09-24 16:50:44 -04:00
:buckets = > { }
}
end
2010-09-08 17:40:02 -04:00
end
end
2011-05-19 10:05:33 -04:00
def self . reset
@data = nil
end
2010-09-08 17:40:02 -04:00
def initialize ( options = { } )
2010-10-29 17:58:28 -04:00
require 'mime/types'
2011-08-03 04:52:09 -04:00
require 'multi_json'
2010-09-08 17:40:02 -04:00
@aws_access_key_id = options [ :aws_access_key_id ]
2010-11-05 14:37:12 -04:00
@aws_secret_access_key = options [ :aws_secret_access_key ]
2010-09-24 17:43:29 -04:00
options [ :region ] || = 'us-east-1'
2010-09-24 17:36:42 -04:00
@host = options [ :host ] || case options [ :region ]
2011-03-02 13:57:52 -05:00
when 'ap-northeast-1'
's3-ap-northeast-1.amazonaws.com'
when 'ap-southeast-1'
's3-ap-southeast-1.amazonaws.com'
2010-09-24 17:36:42 -04:00
when 'eu-west-1'
's3-eu-west-1.amazonaws.com'
when 'us-east-1'
's3.amazonaws.com'
2011-12-15 07:33:38 -05:00
when 'sa-east-1'
's3-sa-east-1.amazonaws.com'
2010-09-24 17:36:42 -04:00
when 'us-west-1'
's3-us-west-1.amazonaws.com'
2011-11-09 12:57:48 -05:00
when 'us-west-2'
's3-us-west-2.amazonaws.com'
2011-12-15 07:37:31 -05:00
when 'sa-east-1'
's3-sa-east-1.amazonaws.com'
2010-09-24 17:36:42 -04:00
else
raise ArgumentError , " Unknown region: #{ options [ :region ] . inspect } "
end
2011-03-10 14:16:55 -05:00
@region = options [ :region ]
2011-05-19 18:35:33 -04:00
end
def data
self . class . data [ @region ] [ @aws_access_key_id ]
2011-03-10 14:16:55 -05:00
end
def reset_data
self . class . data [ @region ] . delete ( @aws_access_key_id )
2010-09-08 17:40:02 -04:00
end
def signature ( params )
" foo "
end
2011-03-10 14:16:55 -05:00
2010-09-08 17:40:02 -04:00
end
class Real
include Utils
# Initialize connection to S3
#
# ==== Notes
2010-09-27 19:08:04 -04:00
# 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
2011-10-12 09:45:34 -04:00
# s3 = Fog::Storage.new(
# :provider => "AWS",
2010-09-08 17:40:02 -04:00
# :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-02-16 20:25:50 -05:00
require 'fog/core/parser'
2010-10-29 17:58:28 -04:00
require 'mime/types'
2011-02-16 20:25:50 -05:00
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 ]
2011-09-12 11:01:48 -04:00
@connection_options = options [ :connection_options ] || { }
2010-09-08 17:40:02 -04:00
@hmac = Fog :: HMAC . new ( 'sha1' , @aws_secret_access_key )
2010-09-29 20:53:32 -04:00
if @endpoint = options [ :endpoint ]
endpoint = URI . parse ( @endpoint )
@host = endpoint . host
2011-11-04 11:28:27 -04:00
@path = if endpoint . path . empty?
'/'
else
endpoint . path
end
2010-09-29 20:53:32 -04:00
@port = endpoint . port
@scheme = endpoint . scheme
else
options [ :region ] || = 'us-east-1'
@host = options [ :host ] || case options [ :region ]
2011-03-03 20:06:58 -05:00
when 'ap-northeast-1'
's3-ap-northeast-1.amazonaws.com'
when 'ap-southeast-1'
's3-ap-southeast-1.amazonaws.com'
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'
2011-12-15 07:33:38 -05:00
when 'sa-east-1'
's3-sa-east-1.amazonaws.com'
2010-09-08 17:40:02 -04:00
when 'us-west-1'
's3-us-west-1.amazonaws.com'
2011-11-09 12:57:48 -05:00
when 'us-west-2'
's3-us-west-2.amazonaws.com'
2010-09-08 17:40:02 -04:00
else
raise ArgumentError , " Unknown region: #{ options [ :region ] . inspect } "
end
2011-09-12 11:01:48 -04:00
@path = options [ :path ] || '/'
@persistent = options [ :persistent ] || true
@port = options [ :port ] || 443
@scheme = options [ :scheme ] || 'https'
2010-09-29 20:53:32 -04:00
end
2011-09-12 11:01:48 -04:00
@connection = Fog :: Connection . new ( " #{ @scheme } :// #{ @host } : #{ @port } #{ @path } " , @persistent , @connection_options )
2010-09-08 17:40:02 -04:00
end
def reload
@connection . reset
end
def signature ( params )
string_to_sign =
<<-DATA
2011-06-07 18:28:57 -04:00
#{params[:method].to_s.upcase}
2010-09-08 17:40:02 -04:00
#{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
2010-11-15 20:17:37 -05:00
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]$ /
2011-09-02 13:30:10 -04:00
Fog :: Logger . warning ( " 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
2010-11-15 20:17:37 -05:00
params [ :path ] = subdomain
2010-09-08 17:40:02 -04:00
end
subdomain = nil
end
2010-10-01 13:38:15 -04:00
canonical_resource = @path . dup
2010-09-08 17:40:02 -04:00
unless subdomain . nil? || subdomain == @host
2011-06-07 18:25:12 -04:00
canonical_resource << " #{ Fog :: AWS . escape ( subdomain ) . downcase } / "
2010-09-08 17:40:02 -04:00
end
2011-01-26 17:38:00 -05:00
canonical_resource << params [ :path ] . to_s
2010-09-08 17:40:02 -04:00
canonical_resource << '?'
2011-02-08 07:33:53 -05:00
for key in ( params [ :query ] || { } ) . keys . sort
2011-06-07 18:25:12 -04:00
if %w{
acl
location
logging
notification
partNumber
policy
requestPayment
reponse - cache - control
response - content - disposition
response - content - encoding
response - content - language
response - content - type
response - expires
torrent
uploadId
uploads
versionId
versioning
versions
website
} . include? ( key )
2010-11-15 20:17:37 -05:00
canonical_resource << " #{ key } #{ " = #{ params [ :query ] [ key ] } " unless params [ :query ] [ key ] . nil? } & "
2010-09-08 17:40:02 -04:00
end
end
canonical_resource . chop!
2010-11-15 20:17:37 -05:00
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
2011-02-08 13:06:44 -05:00
private
def request ( params , & block )
params [ :headers ] [ 'Date' ] = Fog :: Time . now . to_date_header
params [ :headers ] [ 'Authorization' ] = " AWS #{ @aws_access_key_id } : #{ signature ( params ) } "
2011-02-09 07:36:12 -05:00
2011-02-08 13:06:44 -05:00
# FIXME: ToHashParser should make this not needed
original_params = params . dup
2011-02-09 07:36:12 -05:00
begin
response = @connection . request ( params , & block )
rescue Excon :: Errors :: TemporaryRedirect = > error
uri = URI . parse ( error . response . headers [ 'Location' ] )
2011-09-02 13:30:10 -04:00
Fog :: Logger . warning ( " fog: followed redirect to #{ uri . host } , connecting to the matching region will be more performant " )
2011-09-12 11:01:48 -04:00
response = Fog :: Connection . new ( " #{ @scheme } :// #{ uri . host } : #{ @port } " , false , @connection_options ) . request ( original_params , & block )
2011-02-08 13:06:44 -05:00
end
response
end
2010-09-08 17:40:02 -04:00
end
end
end
end