mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
[s3] cleaner mocking/dependencies
This commit is contained in:
parent
51c8e69701
commit
46ea4a4a99
18 changed files with 241 additions and 310 deletions
|
@ -1,11 +1,12 @@
|
|||
require 'fog/aws/s3.rb'
|
||||
|
||||
module Fog
|
||||
module AWS
|
||||
|
||||
def self.dependencies
|
||||
[
|
||||
'fog/aws/ec2.rb',
|
||||
'fog/aws/simpledb.rb',
|
||||
'fog/aws/s3.rb'
|
||||
'fog/aws/simpledb.rb'
|
||||
]
|
||||
end
|
||||
|
||||
|
|
|
@ -1,9 +1,20 @@
|
|||
require 'fog/collection'
|
||||
require 'fog/aws/models/s3/directory'
|
||||
|
||||
module Fog
|
||||
module AWS
|
||||
class S3
|
||||
module S3
|
||||
|
||||
def directories
|
||||
Fog::AWS::S3::Directories.new(:connection => self)
|
||||
class Real
|
||||
def directories
|
||||
Fog::AWS::S3::Directories.new(:connection => self)
|
||||
end
|
||||
end
|
||||
|
||||
class Mock
|
||||
def directories
|
||||
Fog::AWS::S3::Directories.new(:connection => self)
|
||||
end
|
||||
end
|
||||
|
||||
class Directories < Fog::Collection
|
||||
|
|
|
@ -1,6 +1,9 @@
|
|||
require 'fog/model'
|
||||
require 'fog/aws/models/s3/files'
|
||||
|
||||
module Fog
|
||||
module AWS
|
||||
class S3
|
||||
module S3
|
||||
|
||||
class Directory < Fog::Model
|
||||
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
require 'fog/model'
|
||||
|
||||
module Fog
|
||||
module AWS
|
||||
class S3
|
||||
module S3
|
||||
|
||||
class File < Fog::Model
|
||||
|
||||
|
|
|
@ -1,6 +1,9 @@
|
|||
require 'fog/collection'
|
||||
require 'fog/aws/models/s3/file'
|
||||
|
||||
module Fog
|
||||
module AWS
|
||||
class S3
|
||||
module S3
|
||||
|
||||
class Files < Fog::Collection
|
||||
|
||||
|
|
|
@ -1,8 +1,9 @@
|
|||
unless Fog.mocking?
|
||||
module Fog
|
||||
module AWS
|
||||
module S3
|
||||
class Real
|
||||
|
||||
module Fog
|
||||
module AWS
|
||||
class S3
|
||||
require 'fog/aws/parsers/s3/copy_object'
|
||||
|
||||
# Copy an object from one S3 bucket to another
|
||||
#
|
||||
|
@ -38,20 +39,14 @@ unless Fog.mocking?
|
|||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
else
|
||||
|
||||
module Fog
|
||||
module AWS
|
||||
class S3
|
||||
class Mock
|
||||
|
||||
def copy_object(source_bucket_name, source_object_name, target_bucket_name, target_object_name, options = {})
|
||||
response = Excon::Response.new
|
||||
source_bucket = Fog::AWS::S3.data[:buckets][source_bucket_name]
|
||||
source_bucket = @data[:buckets][source_bucket_name]
|
||||
source_object = source_bucket && source_bucket[:objects][source_object_name]
|
||||
target_bucket = Fog::AWS::S3.data[:buckets][target_bucket_name]
|
||||
target_bucket = @data[:buckets][target_bucket_name]
|
||||
|
||||
if source_object && target_bucket
|
||||
response.status = 200
|
||||
|
@ -75,5 +70,4 @@ else
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,8 +1,7 @@
|
|||
unless Fog.mocking?
|
||||
|
||||
module Fog
|
||||
module AWS
|
||||
class S3
|
||||
module Fog
|
||||
module AWS
|
||||
module S3
|
||||
class Real
|
||||
|
||||
# Delete an S3 bucket
|
||||
#
|
||||
|
@ -22,32 +21,26 @@ unless Fog.mocking?
|
|||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
else
|
||||
|
||||
module Fog
|
||||
module AWS
|
||||
class S3
|
||||
class Mock
|
||||
|
||||
def delete_bucket(bucket_name)
|
||||
response = Excon::Response.new
|
||||
if Fog::AWS::S3.data[:buckets][bucket_name].nil?
|
||||
if @data[:buckets][bucket_name].nil?
|
||||
response.status = 404
|
||||
raise(Excon::Errors.status_error({:expects => 204}, response))
|
||||
elsif Fog::AWS::S3.data[:buckets][bucket_name] && !Fog::AWS::S3.data[:buckets][bucket_name][:objects].empty?
|
||||
elsif @data[:buckets][bucket_name] && !@data[:buckets][bucket_name][:objects].empty?
|
||||
response.status = 409
|
||||
raise(Excon::Errors.status_error({:expects => 204}, response))
|
||||
else
|
||||
Fog::AWS::S3.data[:buckets].delete(bucket_name)
|
||||
@data[:buckets].delete(bucket_name)
|
||||
response.status = 204
|
||||
end
|
||||
response
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -1,8 +1,7 @@
|
|||
unless Fog.mocking?
|
||||
|
||||
module Fog
|
||||
module AWS
|
||||
class S3
|
||||
module Fog
|
||||
module AWS
|
||||
module S3
|
||||
class Real
|
||||
|
||||
# Delete an object from S3
|
||||
#
|
||||
|
@ -25,18 +24,12 @@ unless Fog.mocking?
|
|||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
else
|
||||
|
||||
module Fog
|
||||
module AWS
|
||||
class S3
|
||||
class Mock
|
||||
|
||||
def delete_object(bucket_name, object_name)
|
||||
response = Excon::Response.new
|
||||
if bucket = Fog::AWS::S3.data[:buckets][bucket_name]
|
||||
if bucket = @data[:buckets][bucket_name]
|
||||
response.status = 204
|
||||
bucket[:objects].delete(object_name)
|
||||
else
|
||||
|
@ -49,5 +42,4 @@ else
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
|
@ -1,8 +1,9 @@
|
|||
unless Fog.mocking?
|
||||
module Fog
|
||||
module AWS
|
||||
module S3
|
||||
class Real
|
||||
|
||||
module Fog
|
||||
module AWS
|
||||
class S3
|
||||
require 'fog/aws/parsers/s3/get_bucket'
|
||||
|
||||
# List information about objects in an S3 bucket
|
||||
#
|
||||
|
@ -54,14 +55,8 @@ unless Fog.mocking?
|
|||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
else
|
||||
|
||||
module Fog
|
||||
module AWS
|
||||
class S3
|
||||
class Mock
|
||||
|
||||
# FIXME: implement delimiter
|
||||
def get_bucket(bucket_name, options = {})
|
||||
|
@ -69,16 +64,17 @@ else
|
|||
raise ArgumentError.new('bucket_name is required')
|
||||
end
|
||||
response = Excon::Response.new
|
||||
if bucket = Fog::AWS::S3.data[:buckets][bucket_name]
|
||||
if bucket = @data[:buckets][bucket_name]
|
||||
response.status = 200
|
||||
response.body = {
|
||||
'Contents' => bucket[:objects].values.sort {|x,y| x['Key'] <=> y['Key']}.reject do |object|
|
||||
(options['prefix'] && object['Key'][0...options['prefix'].length] != options['prefix']) ||
|
||||
(options['marker'] && object['Key'] <= options['marker'])
|
||||
end.map do |object|
|
||||
data = object.reject {|key, value| !['ETag', 'Key', 'LastModified', 'Owner', 'Size', 'StorageClass'].include?(key)}
|
||||
data = object.reject {|key, value| !['ETag', 'Key', 'LastModified', 'Size', 'StorageClass'].include?(key)}
|
||||
data.merge!({
|
||||
'LastModified' => Time.parse(data['LastModified']),
|
||||
'Owner' => bucket['Owner'],
|
||||
'Size' => data['Size'].to_i
|
||||
})
|
||||
data
|
||||
|
@ -103,5 +99,4 @@ else
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -1,8 +1,9 @@
|
|||
unless Fog.mocking?
|
||||
module Fog
|
||||
module AWS
|
||||
module S3
|
||||
class Real
|
||||
|
||||
module Fog
|
||||
module AWS
|
||||
class S3
|
||||
require 'fog/aws/parsers/s3/get_bucket_location'
|
||||
|
||||
# Get location constraint for an S3 bucket
|
||||
#
|
||||
|
@ -26,18 +27,12 @@ unless Fog.mocking?
|
|||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
else
|
||||
|
||||
module Fog
|
||||
module AWS
|
||||
class S3
|
||||
class Mock
|
||||
|
||||
def get_bucket_location(bucket_name)
|
||||
response = Excon::Response.new
|
||||
if bucket = Fog::AWS::S3.data[:buckets][bucket_name]
|
||||
if bucket = @data[:buckets][bucket_name]
|
||||
response.status = 200
|
||||
response.body = {'LocationConstraint' => bucket['LocationConstraint'] }
|
||||
else
|
||||
|
@ -50,5 +45,4 @@ else
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,8 +1,7 @@
|
|||
unless Fog.mocking?
|
||||
|
||||
module Fog
|
||||
module AWS
|
||||
class S3
|
||||
module Fog
|
||||
module AWS
|
||||
module S3
|
||||
class Real
|
||||
|
||||
# Get an object from S3
|
||||
#
|
||||
|
@ -56,19 +55,13 @@ unless Fog.mocking?
|
|||
:headers => {},
|
||||
:host => "#{bucket_name}.#{@host}",
|
||||
:method => 'GET',
|
||||
:path => object_name
|
||||
:path => CGI.escape(object_name)
|
||||
}, expires)
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
else
|
||||
|
||||
module Fog
|
||||
module AWS
|
||||
class S3
|
||||
class Mock
|
||||
|
||||
def get_object(bucket_name, object_name, options = {}, &block)
|
||||
unless bucket_name
|
||||
|
@ -78,7 +71,7 @@ else
|
|||
raise ArgumentError.new('object_name is required')
|
||||
end
|
||||
response = Excon::Response.new
|
||||
if (bucket = Fog::AWS::S3.data[:buckets][bucket_name]) && (object = bucket[:objects][object_name])
|
||||
if (bucket = @data[:buckets][bucket_name]) && (object = bucket[:objects][object_name])
|
||||
if options['If-Match'] && options['If-Match'] != object['ETag']
|
||||
response.status = 412
|
||||
elsif options['If-Modified-Since'] && options['If-Modified-Since'] > Time.parse(object['LastModified'])
|
||||
|
@ -121,5 +114,4 @@ else
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -1,8 +1,9 @@
|
|||
unless Fog.mocking?
|
||||
module Fog
|
||||
module AWS
|
||||
module S3
|
||||
class Real
|
||||
|
||||
module Fog
|
||||
module AWS
|
||||
class S3
|
||||
require 'fog/aws/parsers/s3/get_request_payment'
|
||||
|
||||
# Get configured payer for an S3 bucket
|
||||
#
|
||||
|
@ -26,18 +27,12 @@ unless Fog.mocking?
|
|||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
else
|
||||
|
||||
module Fog
|
||||
module AWS
|
||||
class S3
|
||||
class Mock
|
||||
|
||||
def get_request_payment(bucket_name)
|
||||
response = Excon::Response.new
|
||||
if bucket = Fog::AWS::S3.data[:buckets][bucket_name]
|
||||
if bucket = @data[:buckets][bucket_name]
|
||||
response.status = 200
|
||||
response.body = { 'Payer' => bucket['Payer'] }
|
||||
else
|
||||
|
@ -50,5 +45,4 @@ else
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -1,8 +1,9 @@
|
|||
unless Fog.mocking?
|
||||
module Fog
|
||||
module AWS
|
||||
module S3
|
||||
class Real
|
||||
|
||||
module Fog
|
||||
module AWS
|
||||
class S3
|
||||
require 'fog/aws/parsers/s3/get_service'
|
||||
|
||||
# List information about S3 buckets for authorized user
|
||||
#
|
||||
|
@ -28,19 +29,13 @@ unless Fog.mocking?
|
|||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
else
|
||||
|
||||
module Fog
|
||||
module AWS
|
||||
class S3
|
||||
class Mock
|
||||
|
||||
def get_service
|
||||
response = Excon::Response.new
|
||||
response.headers['Status'] = 200
|
||||
buckets = Fog::AWS::S3.data[:buckets].values.map do |bucket|
|
||||
buckets = @data[:buckets].values.map do |bucket|
|
||||
bucket.reject do |key, value|
|
||||
!['CreationDate', 'Name'].include?(key)
|
||||
end
|
||||
|
@ -55,5 +50,4 @@ else
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -1,8 +1,7 @@
|
|||
unless Fog.mocking?
|
||||
|
||||
module Fog
|
||||
module AWS
|
||||
class S3
|
||||
module Fog
|
||||
module AWS
|
||||
module S3
|
||||
class Real
|
||||
|
||||
# Get headers for an object from S3
|
||||
#
|
||||
|
@ -39,14 +38,8 @@ unless Fog.mocking?
|
|||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
else
|
||||
|
||||
module Fog
|
||||
module AWS
|
||||
class S3
|
||||
class Mock
|
||||
|
||||
def head_object(bucket_name, object_name, options = {})
|
||||
response = get_object(bucket_name, object_name, options)
|
||||
|
@ -57,5 +50,4 @@ else
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,8 +1,7 @@
|
|||
unless Fog.mocking?
|
||||
|
||||
module Fog
|
||||
module AWS
|
||||
class S3
|
||||
module Fog
|
||||
module AWS
|
||||
module S3
|
||||
class Real
|
||||
|
||||
# Create an S3 bucket
|
||||
#
|
||||
|
@ -17,11 +16,11 @@ unless Fog.mocking?
|
|||
def put_bucket(bucket_name, options = {})
|
||||
if options['LocationConstraint']
|
||||
data =
|
||||
<<-DATA
|
||||
<CreateBucketConfiguration>
|
||||
<LocationConstraint>#{options['LocationConstraint']}</LocationConstraint>
|
||||
</CreateBucketConfiguration>
|
||||
DATA
|
||||
<<-DATA
|
||||
<CreateBucketConfiguration>
|
||||
<LocationConstraint>#{options['LocationConstraint']}</LocationConstraint>
|
||||
</CreateBucketConfiguration>
|
||||
DATA
|
||||
else
|
||||
data = nil
|
||||
end
|
||||
|
@ -36,14 +35,8 @@ unless Fog.mocking?
|
|||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
else
|
||||
|
||||
module Fog
|
||||
module AWS
|
||||
class S3
|
||||
class Mock
|
||||
|
||||
def put_bucket(bucket_name, options = {})
|
||||
response = Excon::Response.new
|
||||
|
@ -60,8 +53,8 @@ else
|
|||
else
|
||||
bucket['LocationConstraint'] = ''
|
||||
end
|
||||
unless Fog::AWS::S3.data[:buckets][bucket_name]
|
||||
Fog::AWS::S3.data[:buckets][bucket_name] = bucket
|
||||
unless @data[:buckets][bucket_name]
|
||||
@data[:buckets][bucket_name] = bucket
|
||||
end
|
||||
response
|
||||
end
|
||||
|
@ -69,5 +62,4 @@ else
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,8 +1,7 @@
|
|||
unless Fog.mocking?
|
||||
|
||||
module Fog
|
||||
module AWS
|
||||
class S3
|
||||
module Fog
|
||||
module AWS
|
||||
module S3
|
||||
class Real
|
||||
|
||||
# Create an object in an S3 bucket
|
||||
#
|
||||
|
@ -25,7 +24,7 @@ unless Fog.mocking?
|
|||
# * headers<~Hash>:
|
||||
# * 'ETag'<~String> - etag of new object
|
||||
def put_object(bucket_name, object_name, data, options = {})
|
||||
data = parse_data(data)
|
||||
data = Fog::AWS::S3.parse_data(data)
|
||||
headers = data[:headers].merge!(options)
|
||||
request({
|
||||
:body => data[:body],
|
||||
|
@ -39,19 +38,13 @@ unless Fog.mocking?
|
|||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
else
|
||||
|
||||
module Fog
|
||||
module AWS
|
||||
class S3
|
||||
class Mock
|
||||
|
||||
def put_object(bucket_name, object_name, data, options = {})
|
||||
data = parse_data(data)
|
||||
data = Fog::AWS::S3.parse_data(data)
|
||||
response = Excon::Response.new
|
||||
if (bucket = Fog::AWS::S3.data[:buckets][bucket_name])
|
||||
if (bucket = @data[:buckets][bucket_name])
|
||||
response.status = 200
|
||||
bucket[:objects][object_name] = {
|
||||
:body => data[:body],
|
||||
|
@ -72,5 +65,4 @@ else
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -1,8 +1,7 @@
|
|||
unless Fog.mocking?
|
||||
|
||||
module Fog
|
||||
module AWS
|
||||
class S3
|
||||
module Fog
|
||||
module AWS
|
||||
module S3
|
||||
class Real
|
||||
|
||||
# Change who pays for requests to an S3 bucket
|
||||
#
|
||||
|
@ -27,18 +26,12 @@ DATA
|
|||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
else
|
||||
|
||||
module Fog
|
||||
module AWS
|
||||
class S3
|
||||
class Mock
|
||||
|
||||
def put_request_payment(bucket_name, payer)
|
||||
response = Excon::Response.new
|
||||
if bucket = Fog::AWS::S3.data[:buckets][bucket_name]
|
||||
if bucket = @data[:buckets][bucket_name]
|
||||
response.status = 200
|
||||
bucket['Payer'] = payer
|
||||
else
|
||||
|
@ -51,6 +44,4 @@ else
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,82 +1,33 @@
|
|||
require 'fog/aws/models/s3/directories'
|
||||
require 'fog/aws/models/s3/directory'
|
||||
require 'fog/aws/models/s3/files'
|
||||
require 'fog/aws/models/s3/file'
|
||||
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'
|
||||
require 'fog/aws/requests/s3/get_bucket_location'
|
||||
require 'fog/aws/requests/s3/get_object'
|
||||
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'
|
||||
require 'fog/aws/requests/s3/put_object'
|
||||
require 'fog/aws/requests/s3/put_request_payment'
|
||||
|
||||
module Fog
|
||||
module AWS
|
||||
class S3
|
||||
module S3
|
||||
|
||||
if Fog.mocking?
|
||||
def self.reset_data
|
||||
@data = { :buckets => {} }
|
||||
end
|
||||
def self.data
|
||||
@data
|
||||
end
|
||||
end
|
||||
|
||||
def self.dependencies
|
||||
[
|
||||
"fog/aws/models/s3/directory.rb",
|
||||
"fog/aws/models/s3/directories.rb",
|
||||
"fog/aws/models/s3/file.rb",
|
||||
"fog/aws/models/s3/files.rb",
|
||||
"fog/aws/parsers/s3/copy_object.rb",
|
||||
"fog/aws/parsers/s3/get_bucket.rb",
|
||||
"fog/aws/parsers/s3/get_bucket_location.rb",
|
||||
"fog/aws/parsers/s3/get_request_payment.rb",
|
||||
"fog/aws/parsers/s3/get_service.rb",
|
||||
"fog/aws/requests/s3/copy_object.rb",
|
||||
"fog/aws/requests/s3/delete_bucket.rb",
|
||||
"fog/aws/requests/s3/delete_object.rb",
|
||||
"fog/aws/requests/s3/get_bucket.rb",
|
||||
"fog/aws/requests/s3/get_bucket_location.rb",
|
||||
"fog/aws/requests/s3/get_object.rb",
|
||||
"fog/aws/requests/s3/get_request_payment.rb",
|
||||
"fog/aws/requests/s3/get_service.rb",
|
||||
"fog/aws/requests/s3/head_object.rb",
|
||||
"fog/aws/requests/s3/put_bucket.rb",
|
||||
"fog/aws/requests/s3/put_object.rb",
|
||||
"fog/aws/requests/s3/put_request_payment.rb"
|
||||
]
|
||||
end
|
||||
|
||||
def self.reload
|
||||
self.dependencies.each {|dependency| load(dependency)}
|
||||
def self.new(options={})
|
||||
if Fog.mocking?
|
||||
reset_data
|
||||
Fog::AWS::S3::Mock.new(options)
|
||||
else
|
||||
Fog::AWS::S3::Real.new(options)
|
||||
end
|
||||
end
|
||||
|
||||
# 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)
|
||||
@host = options[:host] || 's3.amazonaws.com'
|
||||
@port = options[:port] || 443
|
||||
@scheme = options[:scheme] || 'https'
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def parse_data(data)
|
||||
def self.parse_data(data)
|
||||
metadata = {
|
||||
:body => nil,
|
||||
:headers => {}
|
||||
|
@ -97,27 +48,64 @@ module Fog
|
|||
metadata
|
||||
end
|
||||
|
||||
def request(params)
|
||||
@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)}"
|
||||
class Mock
|
||||
|
||||
response = @connection.request(params)
|
||||
def reset_data
|
||||
@data = { :buckets => {} }
|
||||
end
|
||||
|
||||
def initialize(options={})
|
||||
reset_data
|
||||
end
|
||||
|
||||
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
|
||||
class Real
|
||||
|
||||
def signature(params)
|
||||
string_to_sign =
|
||||
# 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)
|
||||
@host = options[:host] || 's3.amazonaws.com'
|
||||
@port = options[:port] || 443
|
||||
@scheme = options[:scheme] || 'https'
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def request(params)
|
||||
@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)}"
|
||||
|
||||
response = @connection.request(params)
|
||||
|
||||
response
|
||||
end
|
||||
|
||||
def signature(params)
|
||||
string_to_sign =
|
||||
<<-DATA
|
||||
#{params[:method]}
|
||||
#{params[:headers]['Content-MD5']}
|
||||
|
@ -125,46 +113,54 @@ module Fog
|
|||
#{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
|
||||
amz_headers, canonical_amz_headers = {}, ''
|
||||
for key, value in params[:headers]
|
||||
if key[0..5] == 'x-amz-'
|
||||
amz_headers[key] = value
|
||||
end
|
||||
end
|
||||
end
|
||||
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]$/
|
||||
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}"
|
||||
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
|
||||
subdomain = nil
|
||||
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]$/
|
||||
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
|
||||
canonical_resource << "#{CGI.escape(subdomain).downcase}/"
|
||||
end
|
||||
canonical_resource << "#{params[:path]}"
|
||||
if ['acl', 'location', 'logging', 'requestPayment', 'torrent'].include?(params[:query])
|
||||
canonical_resource << "?#{params[:query]}"
|
||||
end
|
||||
string_to_sign << "#{canonical_resource}"
|
||||
|
||||
hmac = @hmac.update(string_to_sign)
|
||||
signature = Base64.encode64(hmac.digest).chomp!
|
||||
end
|
||||
|
||||
canonical_resource = "/"
|
||||
unless subdomain.nil? || subdomain == @host
|
||||
canonical_resource << "#{CGI.escape(subdomain).downcase}/"
|
||||
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
|
||||
canonical_resource << "#{params[:path]}"
|
||||
if ['acl', 'location', 'logging', 'requestPayment', 'torrent'].include?(params[:query])
|
||||
canonical_resource << "?#{params[:query]}"
|
||||
end
|
||||
string_to_sign << "#{canonical_resource}"
|
||||
|
||||
hmac = @hmac.update(string_to_sign)
|
||||
signature = Base64.encode64(hmac.digest).chomp!
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Fog::AWS::S3.reload
|
||||
|
|
Loading…
Reference in a new issue