Sha256: 2e8e36c665ddaf0c29682d60b65eb662cdf2b455a97b4771df6f7ee38be42fc3

Contents?: true

Size: 1.53 KB

Versions: 3

Compression:

Stored size: 1.53 KB

Contents

require 'digest/md5'
require 'openstack'

class PushmiPullyu::SwiftDepositer

  attr_reader :swift_connection

  def initialize(connection)
    @swift_connection = OpenStack::Connection.create(
      username: connection[:username],
      api_key: connection[:password],
      auth_method: 'password',
      auth_url: "#{connection[:endpoint]}/auth/#{connection[:auth_version]}",
      authtenant_name: connection[:tenant],
      service_type: 'object-store'
    )
  end

  def deposit_file(file_name, swift_container)
    file_base_name = File.basename(file_name, '.*')

    checksum = Digest::MD5.file(file_name).hexdigest

    era_container = swift_connection.container(swift_container)

    # Add swift metadata with in accordance to AIP spec:
    # https://docs.google.com/document/d/154BqhDPAdGW-I9enrqLpBYbhkF9exX9lV3kMaijuwPg/edit#
    metadata = {
      project: 'ERA',
      project_id: file_base_name,
      promise: 'bronze',
      aip_version: '1.0'
    }

    # ruby-openstack wants all keys of the metadata to be named like "X-Object-Meta-{{Key}}", so update them
    metadata.transform_keys! { |key| "X-Object-Meta-#{key}" }

    headers = { 'etag' => checksum,
                'content-type' => 'application/x-tar' }.merge(metadata)

    if era_container.object_exists?(file_base_name)
      deposited_file = era_container.object(file_base_name)
      deposited_file.write(File.open(file_name), headers)
    else
      deposited_file = era_container.create_object(file_base_name, headers, File.open(file_name))
    end

    deposited_file
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
pushmi_pullyu-0.2.3 lib/pushmi_pullyu/swift_depositer.rb
pushmi_pullyu-0.2.2 lib/pushmi_pullyu/swift_depositer.rb
pushmi_pullyu-0.2.1 lib/pushmi_pullyu/swift_depositer.rb