2009-05-19 02:06:49 -04:00
|
|
|
require 'rubygems'
|
|
|
|
require 'base64'
|
|
|
|
require 'cgi'
|
2009-06-07 18:42:51 -04:00
|
|
|
require 'digest/md5'
|
2009-05-19 02:06:49 -04:00
|
|
|
require 'hmac-sha1'
|
2009-06-07 18:42:51 -04:00
|
|
|
require 'mime/types'
|
2009-05-19 02:06:49 -04:00
|
|
|
|
|
|
|
require File.dirname(__FILE__) + '/s3/parsers'
|
|
|
|
|
|
|
|
module Fog
|
|
|
|
module AWS
|
|
|
|
class S3
|
|
|
|
|
|
|
|
# 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
|
|
|
|
# sdb = 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={})
|
|
|
|
@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-06-22 01:13:01 -04:00
|
|
|
@connection = AWS::Connection.new("#{@scheme}://#{@host}:#{@port}")
|
2009-05-19 02:06:49 -04:00
|
|
|
end
|
|
|
|
|
2009-06-05 19:51:17 -04:00
|
|
|
# List information about S3 buckets for authorized user
|
2009-05-19 02:06:49 -04:00
|
|
|
def get_service
|
2009-06-23 23:55:57 -04:00
|
|
|
request({
|
|
|
|
:headers => {},
|
2009-06-25 03:32:27 -04:00
|
|
|
:host => @host,
|
2009-06-23 23:55:57 -04:00
|
|
|
:method => 'GET',
|
|
|
|
:parser => Fog::Parsers::AWS::S3::GetServiceParser.new,
|
2009-06-24 23:48:51 -04:00
|
|
|
:url => @host
|
2009-06-23 23:55:57 -04:00
|
|
|
})
|
2009-05-19 02:06:49 -04:00
|
|
|
end
|
|
|
|
|
2009-06-05 19:51:17 -04:00
|
|
|
# Create an S3 bucket
|
|
|
|
#
|
|
|
|
# ==== Parameters
|
|
|
|
# bucket_name<~String>:: name of bucket to create
|
2009-06-07 04:12:13 -04:00
|
|
|
# options<~Hash>:: config arguments for bucket. Defaults to {}.
|
|
|
|
# :location_constraint sets the location for the bucket
|
|
|
|
def put_bucket(bucket_name, options = {})
|
|
|
|
if options[:location_constraint]
|
2009-06-25 03:32:27 -04:00
|
|
|
data =
|
|
|
|
<<-DATA
|
|
|
|
<CreateBucketConfiguration>
|
|
|
|
<LocationConstraint>#{options[:location_constraint]}</LocationConstraint>
|
|
|
|
</CreateBucketConfiguration>
|
|
|
|
DATA
|
2009-06-07 04:12:13 -04:00
|
|
|
else
|
|
|
|
data = nil
|
|
|
|
end
|
2009-06-23 23:55:57 -04:00
|
|
|
request({
|
|
|
|
:body => data,
|
|
|
|
:headers => {},
|
2009-06-24 23:48:51 -04:00
|
|
|
:host => "#{bucket_name}.#{@host}",
|
2009-06-23 23:55:57 -04:00
|
|
|
:method => 'PUT',
|
2009-06-24 23:48:51 -04:00
|
|
|
:parser => Fog::Parsers::AWS::S3::BasicParser.new
|
2009-06-23 23:55:57 -04:00
|
|
|
})
|
2009-05-19 02:06:49 -04:00
|
|
|
end
|
2009-06-05 19:51:17 -04:00
|
|
|
|
2009-06-07 04:55:52 -04:00
|
|
|
# Change who pays for requests to an S3 bucket
|
|
|
|
#
|
|
|
|
# ==== Parameters
|
|
|
|
# bucket_name<~String>:: name of bucket to modify
|
|
|
|
# payer<~String>:: valid values are BucketOwner or Requester
|
|
|
|
def put_request_payment(bucket_name, payer)
|
2009-06-25 03:32:27 -04:00
|
|
|
data =
|
|
|
|
<<-DATA
|
|
|
|
<RequestPaymentConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
|
|
|
|
<Payer>#{payer}</Payer>
|
|
|
|
</RequestPaymentConfiguration>
|
|
|
|
DATA
|
2009-06-23 23:55:57 -04:00
|
|
|
request({
|
|
|
|
:body => data,
|
|
|
|
:headers => {},
|
2009-06-24 23:48:51 -04:00
|
|
|
:host => "#{bucket_name}.#{@host}",
|
2009-06-23 23:55:57 -04:00
|
|
|
:method => 'PUT',
|
2009-06-25 03:32:27 -04:00
|
|
|
:parser => Fog::Parsers::AWS::S3::BasicParser.new,
|
|
|
|
:query => "requestPayment"
|
2009-06-23 23:55:57 -04:00
|
|
|
})
|
2009-06-07 04:55:52 -04:00
|
|
|
end
|
|
|
|
|
2009-06-05 19:51:17 -04:00
|
|
|
# List information about objects in an S3 bucket
|
|
|
|
#
|
|
|
|
# ==== Parameters
|
|
|
|
# bucket_name<~String>:: name of bucket to list object keys from
|
|
|
|
# options<~Hash>:: config arguments for list. Defaults to {}.
|
2009-06-07 03:54:22 -04:00
|
|
|
# :prefix limits object keys to those beginning with its value.
|
|
|
|
# :marker limits object keys to only those that appear
|
2009-06-05 19:51:17 -04:00
|
|
|
# lexicographically after its value.
|
2009-06-07 03:54:22 -04:00
|
|
|
# :maxkeys limits number of object keys returned
|
|
|
|
# :delimiter causes keys with the same string between the prefix
|
2009-06-05 19:51:17 -04:00
|
|
|
# value and the first occurence of delimiter to be rolled up
|
|
|
|
def get_bucket(bucket_name, options = {})
|
2009-06-07 03:54:22 -04:00
|
|
|
options['max-keys'] = options.delete(:maxkeys) if options[:maxkeys]
|
2009-06-05 19:51:17 -04:00
|
|
|
query = '?'
|
2009-06-23 23:55:57 -04:00
|
|
|
for key, value in options
|
2009-06-05 19:51:17 -04:00
|
|
|
query << "#{key}=#{value};"
|
|
|
|
end
|
|
|
|
query.chop!
|
2009-06-23 23:55:57 -04:00
|
|
|
request({
|
|
|
|
:headers => {},
|
2009-06-24 23:48:51 -04:00
|
|
|
:host => "#{bucket_name}.#{@host}",
|
2009-06-23 23:55:57 -04:00
|
|
|
:method => 'GET',
|
|
|
|
:parser => Fog::Parsers::AWS::S3::GetBucketParser.new,
|
2009-06-25 03:32:27 -04:00
|
|
|
:query => query
|
2009-06-23 23:55:57 -04:00
|
|
|
})
|
2009-06-05 19:51:17 -04:00
|
|
|
end
|
|
|
|
|
2009-06-07 04:55:52 -04:00
|
|
|
# Get configured payer for an S3 bucket
|
|
|
|
def get_request_payment(bucket_name)
|
2009-06-23 23:55:57 -04:00
|
|
|
request({
|
|
|
|
:headers => {},
|
2009-06-24 23:48:51 -04:00
|
|
|
:host => "#{bucket_name}.#{@host}",
|
2009-06-23 23:55:57 -04:00
|
|
|
:method => 'GET',
|
|
|
|
:parser => Fog::Parsers::AWS::S3::GetRequestPayment.new,
|
2009-06-27 15:08:05 -04:00
|
|
|
:query => 'requestPayment'
|
2009-06-23 23:55:57 -04:00
|
|
|
})
|
2009-06-07 04:55:52 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Get location constraint for an S3 bucket
|
2009-06-25 03:32:27 -04:00
|
|
|
def get_bucket_location(bucket_name)
|
2009-06-23 23:55:57 -04:00
|
|
|
request({
|
|
|
|
:headers => {},
|
2009-06-24 23:48:51 -04:00
|
|
|
:host => "#{bucket_name}.#{@host}",
|
2009-06-23 23:55:57 -04:00
|
|
|
:method => 'GET',
|
2009-06-25 03:32:27 -04:00
|
|
|
:parser => Fog::Parsers::AWS::S3::GetBucketLocation.new,
|
|
|
|
:query => 'location'
|
2009-06-23 23:55:57 -04:00
|
|
|
})
|
2009-06-07 04:55:52 -04:00
|
|
|
end
|
|
|
|
|
2009-06-05 19:51:17 -04:00
|
|
|
# Delete an S3 bucket
|
|
|
|
#
|
|
|
|
# ==== Parameters
|
|
|
|
# bucket_name<~String>:: name of bucket to delete
|
|
|
|
def delete_bucket(bucket_name)
|
2009-06-23 23:55:57 -04:00
|
|
|
request({
|
|
|
|
:headers => {},
|
2009-06-24 23:48:51 -04:00
|
|
|
:host => "#{bucket_name}.#{@host}",
|
2009-06-29 13:07:29 -04:00
|
|
|
:method => 'DELETE'
|
2009-06-23 23:55:57 -04:00
|
|
|
})
|
2009-05-24 12:11:32 -04:00
|
|
|
end
|
2009-05-19 02:06:49 -04:00
|
|
|
|
2009-06-07 18:42:51 -04:00
|
|
|
# Create an object in an S3 bucket
|
|
|
|
def put_object(bucket_name, object_name, object, options = {})
|
|
|
|
file = parse_file(object)
|
2009-06-23 23:55:57 -04:00
|
|
|
request({
|
|
|
|
:body => file[:body],
|
|
|
|
:headers => options.merge!(file[:headers]),
|
2009-06-24 23:48:51 -04:00
|
|
|
:host => "#{bucket_name}.#{@host}",
|
2009-06-23 23:55:57 -04:00
|
|
|
:method => 'PUT',
|
2009-06-24 23:48:51 -04:00
|
|
|
:path => object_name
|
2009-06-23 23:55:57 -04:00
|
|
|
})
|
2009-06-07 18:42:51 -04:00
|
|
|
end
|
|
|
|
|
2009-06-07 04:37:54 -04:00
|
|
|
# Copy an object from one S3 bucket to another
|
|
|
|
def copy_object(source_bucket_name, source_object_name, destination_bucket_name, destination_object_name)
|
2009-06-23 23:55:57 -04:00
|
|
|
request({
|
|
|
|
:headers => { 'x-amz-copy-source' => "/#{source_bucket_name}/#{source_object_name}" },
|
2009-06-24 23:48:51 -04:00
|
|
|
:host => "#{destination_bucket_name}.#{@host}",
|
2009-06-23 23:55:57 -04:00
|
|
|
:method => 'PUT',
|
2009-06-29 13:07:29 -04:00
|
|
|
:parser => Fog::Parsers::AWS::S3::CopyObject.new,
|
2009-06-24 23:48:51 -04:00
|
|
|
:path => destination_object_name
|
2009-06-23 23:55:57 -04:00
|
|
|
})
|
2009-06-07 04:37:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Get an object from S3
|
|
|
|
def get_object(bucket_name, object_name)
|
2009-06-23 23:55:57 -04:00
|
|
|
request({
|
|
|
|
:headers => {},
|
2009-06-24 23:48:51 -04:00
|
|
|
:host => "#{bucket_name}.#{@host}",
|
2009-06-23 23:55:57 -04:00
|
|
|
:method => 'GET',
|
2009-06-24 23:48:51 -04:00
|
|
|
:path => object_name
|
2009-06-23 23:55:57 -04:00
|
|
|
})
|
2009-06-07 04:37:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Get headers for an object from S3
|
|
|
|
def head_object(bucket_name, object_name)
|
2009-06-23 23:55:57 -04:00
|
|
|
request({
|
|
|
|
:headers => {},
|
2009-06-24 23:48:51 -04:00
|
|
|
:host => "#{bucket_name}.#{@host}",
|
2009-06-23 23:55:57 -04:00
|
|
|
:method => 'HEAD',
|
2009-06-24 23:48:51 -04:00
|
|
|
:path => object_name
|
2009-06-23 23:55:57 -04:00
|
|
|
})
|
2009-06-07 04:37:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Delete an object from S3
|
|
|
|
def delete_object(bucket_name, object_name)
|
2009-06-23 23:55:57 -04:00
|
|
|
request({
|
|
|
|
:headers => {},
|
2009-06-24 23:48:51 -04:00
|
|
|
:host => "#{bucket_name}.#{@host}",
|
2009-06-23 23:55:57 -04:00
|
|
|
:method => 'DELETE',
|
2009-06-24 23:48:51 -04:00
|
|
|
:path => object_name
|
2009-06-23 23:55:57 -04:00
|
|
|
})
|
2009-06-07 04:37:54 -04:00
|
|
|
end
|
|
|
|
|
2009-05-19 02:06:49 -04:00
|
|
|
private
|
|
|
|
|
2009-06-07 18:42:51 -04:00
|
|
|
def parse_file(file)
|
|
|
|
metadata = {
|
|
|
|
:body => nil,
|
|
|
|
:headers => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
filename = File.basename(file.path)
|
|
|
|
unless (mime_types = MIME::Types.of(filename)).empty?
|
|
|
|
metadata[:headers]['Content-Type'] = mime_types.first.content_type
|
|
|
|
end
|
|
|
|
|
|
|
|
metadata[:body] = file.read
|
|
|
|
metadata[:headers]['Content-Length'] = metadata[:body].size.to_s
|
|
|
|
metadata[:headers]['Content-MD5'] = Base64.encode64(Digest::MD5.digest(metadata[:body])).strip
|
|
|
|
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-06-24 12:57:21 -04:00
|
|
|
|
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
|
|
|
|
|
|
|
canonical_resource = "/"
|
|
|
|
# [0..-18] is anything prior to .s3.amazonaws.com
|
2009-06-24 23:48:51 -04:00
|
|
|
subdomain = params[:host][0..-18]
|
2009-06-24 12:57:21 -04:00
|
|
|
unless subdomain.empty?
|
|
|
|
canonical_resource << "#{subdomain}/"
|
|
|
|
end
|
2009-06-24 23:48:51 -04:00
|
|
|
canonical_resource << "#{params[:path]}"
|
2009-06-25 03:32:27 -04:00
|
|
|
if params[:query] && !params[:query].empty?
|
|
|
|
canonical_resource << "?#{params[:query]}"
|
|
|
|
end
|
2009-06-24 12:57:21 -04:00
|
|
|
string_to_sign << "#{canonical_resource}"
|
|
|
|
|
|
|
|
hmac = @hmac.update(string_to_sign)
|
|
|
|
signature = Base64.encode64(hmac.digest).chomp!
|
2009-06-23 23:55:57 -04:00
|
|
|
params[:headers]['Authorization'] = "AWS #{@aws_access_key_id}:#{signature}"
|
2009-06-05 19:51:17 -04:00
|
|
|
|
2009-06-22 01:13:01 -04:00
|
|
|
response = @connection.request({
|
2009-06-23 23:55:57 -04:00
|
|
|
:body => params[:body],
|
|
|
|
:headers => params[:headers],
|
2009-06-24 23:48:51 -04:00
|
|
|
:host => params[:host],
|
2009-06-23 23:55:57 -04:00
|
|
|
:method => params[:method],
|
2009-06-25 03:32:27 -04:00
|
|
|
:path => params[:path],
|
|
|
|
:query => params[:query]
|
2009-06-22 01:13:01 -04:00
|
|
|
})
|
2009-06-11 13:25:05 -04:00
|
|
|
|
2009-06-23 23:55:57 -04:00
|
|
|
if params[:parser] && !response.body.empty?
|
|
|
|
Nokogiri::XML::SAX::Parser.new(params[:parser]).parse(response.body.split(/<\?xml.*\?>/)[1])
|
|
|
|
response.body = params[:parser].response
|
2009-06-22 01:13:01 -04:00
|
|
|
end
|
2009-06-11 13:25:05 -04:00
|
|
|
|
2009-05-20 21:49:20 -04:00
|
|
|
response
|
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
|
|
|
|
end
|