1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00
fog--fog/lib/fog/aws/glacier.rb

161 lines
5 KiB
Ruby
Raw Normal View History

2012-09-01 11:44:04 -04:00
require 'fog/aws'
module Fog
module AWS
class Glacier < Fog::Service
extend Fog::AWS::CredentialFetcher::ServiceMethods
requires :aws_access_key_id, :aws_secret_access_key
recognizes :region, :host, :path, :port, :scheme, :persistent, :use_iam_profile, :aws_session_token, :aws_credentials_expire_at
request_path 'fog/aws/requests/glacier'
2012-09-01 18:09:58 -04:00
request :abort_multipart_upload
request :complete_multipart_upload
2012-09-01 13:23:40 -04:00
request :create_archive
2012-09-01 11:45:01 -04:00
request :create_vault
2012-09-01 13:23:40 -04:00
request :delete_archive
2012-09-01 11:45:01 -04:00
request :delete_vault
request :delete_vault_notification_configuration
2012-09-01 18:40:51 -04:00
request :describe_job
2012-09-01 11:45:01 -04:00
request :describe_vault
2012-09-01 18:40:51 -04:00
request :get_job_output
2012-09-01 11:45:01 -04:00
request :get_vault_notification_configuration
2012-09-01 18:40:51 -04:00
request :initiate_job
2012-09-01 18:09:58 -04:00
request :initiate_multipart_upload
2012-09-01 18:40:51 -04:00
request :list_jobs
2012-09-01 18:09:58 -04:00
request :list_multipart_uploads
request :list_parts
2012-09-01 11:45:01 -04:00
request :list_vaults
request :set_vault_notification_configuration
2012-09-01 18:09:58 -04:00
request :upload_part
2012-09-01 13:23:40 -04:00
2012-09-01 19:03:24 -04:00
model_path 'fog/aws/models/glacier'
model :vault
collection :vaults
2012-09-01 13:23:40 -04:00
MEGABYTE = 1024*1024
class TreeHash
def self.digest(body)
2012-09-01 18:09:58 -04:00
new.add_part(body)
2012-09-01 13:23:40 -04:00
end
def reduce_digests(digests)
while digests.length > 1
digests = digests.each_slice(2).collect do |pair|
if pair.length == 2
Digest::SHA256.digest(pair[0]+pair[1])
else
pair.first
end
end
end
digests.first
end
def initialize
@digests = []
end
def add_part(bytes)
2012-09-01 18:09:58 -04:00
part = self.digest_for_part(bytes)
@digests << part
part.unpack('H*').first
2012-09-01 13:23:40 -04:00
end
def digest_for_part(body)
chunk_count = [body.bytesize / MEGABYTE + (body.bytesize % MEGABYTE > 0 ? 1 : 0), 1].max
digests_for_part = chunk_count.times.collect {|chunk_index| Digest::SHA256.digest(body.byteslice(chunk_index * MEGABYTE, MEGABYTE))}
reduce_digests(digests_for_part)
end
2012-09-01 18:09:58 -04:00
def hexdigest
digest.unpack('H*').first
end
2012-09-01 13:23:40 -04:00
def digest
2012-09-01 18:09:58 -04:00
reduce_digests(@digests)
2012-09-01 13:23:40 -04:00
end
end
2012-09-01 11:44:04 -04:00
class Mock
def initialize(options={})
Fog::Mock.not_implemented
end
end
class Real
include Fog::AWS::CredentialFetcher::ConnectionMethods
# Initialize connection to Glacier
#
# ==== Notes
# options parameter must include values for :aws_access_key_id and
# :aws_secret_access_key in order to create a connection
#
# ==== Examples
# ses = SES.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 {}.
# * region<~String> - optional region to use. For instance, 'us-east-1' and etc.
#
# ==== Returns
# * Glacier object with connection to AWS.
def initialize(options={})
@use_iam_profile = options[:use_iam_profile]
@region = options[:region] || 'us-east-1'
setup_credentials(options)
@connection_options = options[:connection_options] || {}
@host = options[:host] || "glacier.#{@region}.amazonaws.com"
@version = '2012-06-01'
@path = options[:path] || '/'
@persistent = options[:persistent] || false
@port = options[:port] || 443
@scheme = options[:scheme] || 'https'
@connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}#{@path}", @persistent, @connection_options)
end
private
def setup_credentials(options)
@aws_access_key_id = options[:aws_access_key_id]
@aws_secret_access_key = options[:aws_secret_access_key]
@aws_session_token = options[:aws_session_token]
@aws_credentials_expire_at = options[:aws_credentials_expire_at]
@signer = Fog::AWS::SignatureV4.new( @aws_access_key_id, @aws_secret_access_key,@region,'glacier')
end
def request(params, &block)
refresh_credentials_if_expired
date = Fog::Time.now
params[:headers]['Date'] = date.to_date_header
params[:headers]['x-amz-date'] = date.to_iso8601_basic
params[:headers]['Host'] = @host
params[:headers]['x-amz-glacier-version'] = @version
params[:headers]['x-amz-security-token'] = @aws_session_token if @aws_session_token
params[:headers]['Authorization'] = @signer.sign params, date
response = @connection.request(params, &block)
if response.headers['Content-Type'] == 'application/json'
response.body = Fog::JSON.decode(response.body)
end
response
end
end
end
end
end