class ActiveStorage::Service::OpenstackService

Wraps OpenStack Object Storage Service as an Active Storage service.

Attributes

client[R]
config[R]
credentials[R]
storage[R]

Public Class Methods

new(credentials:, container:, region:, **config) click to toggle source
# File lib/active_storage/service/openstack_service.rb, line 9
def initialize(credentials:, container:, region:, **config)
  @config = config
  @credentials = credentials
  @client = ::Openstack::Client.new username: credentials.fetch(:username),
                                    password: credentials.fetch(:api_key)
  @storage = client.storage container: container,
                            region: region
end

Public Instance Methods

delete(key) click to toggle source
# File lib/active_storage/service/openstack_service.rb, line 52
def delete(key)
  instrument :delete, key: key do
    storage.delete_object(key)
  end
end
delete_prefixed(prefix) click to toggle source
# File lib/active_storage/service/openstack_service.rb, line 58
def delete_prefixed(prefix)
  instrument :delete_prefixed, prefix: prefix do
    keys = JSON.parse(
      storage.list_objects(prefix: prefix).body
    ).map { |object| "/#{storage.container}/#{object.fetch('name')}" }

    storage.bulk_delete_objects(keys)
  end
end
download(key, &block) click to toggle source
# File lib/active_storage/service/openstack_service.rb, line 30
def download(key, &block)
  raise ActiveStorage::FileNotFoundError unless exist?(key)

  if block_given?
    instrument :streaming_download, key: key do
      stream(key, &block)
    end
  else
    instrument :download, key: key do
      storage.get_object(key).body
    end
  end
end
download_chunk(key, range) click to toggle source
# File lib/active_storage/service/openstack_service.rb, line 44
def download_chunk(key, range)
  raise ActiveStorage::FileNotFoundError unless exist?(key)

  instrument :download_chunk, key: key, range: range do
    storage.get_object_by_range(key, range).body
  end
end
exist?(key) click to toggle source
# File lib/active_storage/service/openstack_service.rb, line 68
def exist?(key)
  instrument :exist, key: key do |payload|
    payload[:exist] = storage.show_object_metadata(key).is_a?(Net::HTTPOK)
    payload.fetch(:exist)
  end
end
headers_for_direct_upload(key, filename:, content_type:, content_length:, checksum:) click to toggle source

:reek: LongParameterList :reek: UnusedParameters rubocop:disable Metrics/LineLength

# File lib/active_storage/service/openstack_service.rb, line 107
def headers_for_direct_upload(key, filename:, content_type:, content_length:, checksum:)
  {}
end
update_metadata(key, **metadata) click to toggle source

:reek: UnusedParameters

# File lib/active_storage/service/openstack_service.rb, line 28
def update_metadata(key, **metadata); end
upload(key, io, checksum: nil, **_options) click to toggle source

:reek: LongParameterList

# File lib/active_storage/service/openstack_service.rb, line 19
def upload(key, io, checksum: nil, **_options)
  instrument :upload, key: key, checksum: checksum do
    handle_errors do
      storage.put_object(key, io, checksum: checksum)
    end
  end
end
url(key, expires_in:, disposition:, filename:, content_type:) click to toggle source

:reek: LongParameterList :reek: UnusedParameters rubocop:disable Lint/UnusedMethodArgument

# File lib/active_storage/service/openstack_service.rb, line 78
def url(key, expires_in:, disposition:, filename:, content_type:)
  instrument :url, key: key do |payload|
    payload[:url] = storage.temporary_url(
      key,
      'GET',
      expires_in: expires_in,
      disposition: disposition,
      filename: filename
    )
    payload.fetch(:url)
  end
end
url_for_direct_upload(key, expires_in:, filename:, **_options) click to toggle source

:reek: LongParameterList

# File lib/active_storage/service/openstack_service.rb, line 92
def url_for_direct_upload(key, expires_in:, filename:, **_options)
  instrument :url, key: key do |payload|
    payload[:url] = storage.temporary_url(
      key,
      'PUT',
      expires_in: expires_in,
      filename: filename
    )
    payload.fetch(:url)
  end
end

Private Instance Methods

handle_errors() { || ... } click to toggle source

rubocop:enable Metrics/LineLength rubocop:enable Lint/UnusedMethodArgument

# File lib/active_storage/service/openstack_service.rb, line 115
def handle_errors
  return unless block_given?

  yield.tap do |request|
    raise ActiveStorage::IntegrityError if request.is_a?(
      Net::HTTPUnprocessableEntity
    )
  end