Sha256: ff93a3415f3060283e6ee12eff7818fae59013c7d69ef4d95c0ec95c19119623

Contents?: true

Size: 1.77 KB

Versions: 65

Compression:

Stored size: 1.77 KB

Contents

require 'fog/core/model'

module Fog
  module AWS
    class Glacier

      class Archive < Fog::Model

        identity  :id
        attribute :description
        attribute :body

        attr_accessor :multipart_chunk_size #must be a power of 2 multiple of 1MB

        def vault
          @vault
        end

        def save
          requires :body, :vault

          if multipart_chunk_size && body.respond_to?(:read)
            self.id = multipart_save
          else
            data = service.create_archive(vault.id, body, 'description' => description)
            self.id = data.headers['x-amz-archive-id']
          end
          true
        end

        def destroy
          requires :id
          service.delete_archive(vault.id,id)
        end

        private

        def vault=(new_vault)
          @vault = new_vault
        end

        def multipart_save
          # Initiate the upload
          res = service.initiate_multipart_upload vault.id, multipart_chunk_size, 'description' => description
          upload_id = res.headers["x-amz-multipart-upload-id"]

          hash = Fog::AWS::Glacier::TreeHash.new

          body.rewind if body.respond_to?(:rewind)
          offset = 0
          while (chunk = body.read(multipart_chunk_size)) do
            part_hash = hash.add_part(chunk)
            part_upload = service.upload_part(vault.id, upload_id, chunk, offset, part_hash  )
            offset += chunk.bytesize
          end

        rescue
          # Abort the upload & reraise
          service.abort_multipart_upload(vault.id, upload_id) if upload_id
          raise
        else
          # Complete the upload
          service.complete_multipart_upload(vault.id, upload_id, offset, hash.hexdigest).headers['x-amz-archive-id']
        end

      end

    end
  end
end

Version data entries

65 entries across 65 versions & 6 rubygems

Version Path
fog-1.22.0 lib/fog/aws/models/glacier/archive.rb
fog-1.21.0 lib/fog/aws/models/glacier/archive.rb
fog-maestrodev-1.20.0.20140305101839 lib/fog/aws/models/glacier/archive.rb
fog-maestrodev-1.20.0.20140305101305 lib/fog/aws/models/glacier/archive.rb
fog-maestrodev-1.19.0.20140212012611 lib/fog/aws/models/glacier/archive.rb
fog-1.20.0 lib/fog/aws/models/glacier/archive.rb
fog-maestrodev-1.19.0.20140110004459 lib/fog/aws/models/glacier/archive.rb
fog-maestrodev-1.19.0.20140110003812 lib/fog/aws/models/glacier/archive.rb
fog-maestrodev-1.19.0.20140109202555 lib/fog/aws/models/glacier/archive.rb
fog-maestrodev-1.19.0.20140107192102 lib/fog/aws/models/glacier/archive.rb
fog-maestrodev-1.19.0.20140107142106 lib/fog/aws/models/glacier/archive.rb
fog-maestrodev-1.19.0.20131219203941 lib/fog/aws/models/glacier/archive.rb
fog-maestrodev-1.18.0.20131219193542 lib/fog/aws/models/glacier/archive.rb
fog-1.19.0 lib/fog/aws/models/glacier/archive.rb
fog-maestrodev-1.18.0.20131219033443 lib/fog/aws/models/glacier/archive.rb
fog-maestrodev-1.18.0.20131219032002 lib/fog/aws/models/glacier/archive.rb
fog-maestrodev-1.18.0.20131219030716 lib/fog/aws/models/glacier/archive.rb
fog-maestrodev-1.18.0.20131219022322 lib/fog/aws/models/glacier/archive.rb
fog-maestrodev-1.18.0.20131218202447 lib/fog/aws/models/glacier/archive.rb
fog-maestrodev-1.18.0.20131209091424 lib/fog/aws/models/glacier/archive.rb