class AWS::Glacier::ArchiveCollection

Attributes

account_id[R]

@return [String]

vault[R]

@return [Vault]

Public Class Methods

new(vault, options = {}) click to toggle source

@param [Vault] vault @param [Hash] options @option options [String] :account_id

Calls superclass method AWS::Core::Model.new
# File lib/aws/glacier/archive_collection.rb, line 26
def initialize vault, options = {}
  @vault = vault
  @account_id = options[:account_id] || '-'
  super
end

Public Instance Methods

[](archive_id) click to toggle source

@param [String] archive_id @return [Archive]

# File lib/aws/glacier/archive_collection.rb, line 68
def [] archive_id
  Archive.new(vault, archive_id, :config => config, :account_id => account_id)
end
create(data, options = {}) click to toggle source

Creates an archive by uploading a file to a vault. @param [File,Pathname,IO,String] data The data to upload.

If +data+ is a string, this is treated as a path to a file
on disk.

@param [Hash] options @option options [String] description @return [Archive]

# File lib/aws/glacier/archive_collection.rb, line 45
def create data, options = {}

  data = convert_to_io(data)

  hash, tree_hash = compute_checksums(data)

  upload_options = {}
  upload_options[:vault_name] = vault.name
  upload_options[:account_id] = account_id
  upload_options[:body] = data
  upload_options[:checksum] = tree_hash
  upload_options[:content_sha256] = hash
  upload_options[:archive_description] = options[:description] if
    options[:description]

  resp = client.upload_archive(upload_options)

  self[resp[:archive_id]]

end

Protected Instance Methods

compute_checksums(data) click to toggle source

Computes two checksums in a single pass of the data:

  • a hash of the entire payload

  • a tree hash of the entire payload

The tree hash is required by the streaming operations, the simple hash is required for generating the signature (via sigv4).

The sigv4 module will compute the hash of the payload for us, but that requires reading the data a 2nd time. :(

# File lib/aws/glacier/archive_collection.rb, line 102
def compute_checksums data

  digest = OpenSSL::Digest::Digest.new('sha256')
  tree_digest = OpenSSL::Digest::Digest.new('sha256')
  tree_parts = []

  until data.eof?

    chunk = data.read(1024 * 1024) # read 1MB
    tree_parts << tree_digest.update(chunk).digest
    tree_digest.reset

    digest.update(chunk)

  end

  data.rewind

  [digest.to_s, compute_tree_hash(tree_parts)]

end
compute_tree_hash(hashes) click to toggle source
# File lib/aws/glacier/archive_collection.rb, line 124
def compute_tree_hash hashes

  digest = OpenSSL::Digest::Digest.new('sha256')

  until hashes.count == 1
    hashes = hashes.each_slice(2).map do |h1,h2|
      digest.reset
      if h2
        digest.update(h1)
        digest.update(h2)
        digest.digest
      else
        h1
      end
    end
  end

  hashes.first.bytes.map{|x| x.to_i.to_s(16).rjust(2,'0')}.join('')
end
convert_to_io(data) click to toggle source
# File lib/aws/glacier/archive_collection.rb, line 74
def convert_to_io data
  return Core::ManagedFile.open(data) if
    data.is_a?(Pathname) or data.is_a?(String)

  return data if io_like?(data)

  msg = "expected data to be IO-like or a file path (String/Pathanme)."
  raise ArgumentError, msg
end
io_like?(data) click to toggle source

@return [Boolean] Returns tue if data acts like a file.

# File lib/aws/glacier/archive_collection.rb, line 85
def io_like? data
  data.respond_to?(:read) and
  data.respond_to?(:eof?) and
  data.respond_to?(:rewind) and
  data.respond_to?(:size)
end