Class: ActiveStorage::Service::OpenstackService

Inherits:
ActiveStorage::Service show all
Defined in:
lib/active_storage/service/openstack_service.rb

Overview

Wraps OpenStack Object Storage Service as an Active Storage service.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(credentials:, container:, region:, **config) ⇒ OpenstackService



9
10
11
12
13
14
15
16
# 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

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



7
8
9
# File 'lib/active_storage/service/openstack_service.rb', line 7

def client
  @client
end

#configObject (readonly)

Returns the value of attribute config.



7
8
9
# File 'lib/active_storage/service/openstack_service.rb', line 7

def config
  @config
end

#credentialsObject (readonly)

Returns the value of attribute credentials.



7
8
9
# File 'lib/active_storage/service/openstack_service.rb', line 7

def credentials
  @credentials
end

#storageObject (readonly)

Returns the value of attribute storage.



7
8
9
# File 'lib/active_storage/service/openstack_service.rb', line 7

def storage
  @storage
end

Instance Method Details

#delete(key) ⇒ Object



52
53
54
55
56
# 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) ⇒ Object



58
59
60
61
62
63
64
65
66
# 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) ⇒ Object

Raises:

  • (ActiveStorage::FileNotFoundError)


30
31
32
33
34
35
36
37
38
39
40
41
42
# 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) ⇒ Object

Raises:

  • (ActiveStorage::FileNotFoundError)


44
45
46
47
48
49
50
# 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) ⇒ Boolean



68
69
70
71
72
73
# File 'lib/active_storage/service/openstack_service.rb', line 68

def exist?(key)
  instrument :exist, key: key do |payload|
    payload[:exist] = storage.(key).is_a?(Net::HTTPOK)
    payload.fetch(:exist)
  end
end

#headers_for_direct_upload(key, filename:, content_type:, content_length:, checksum:) ⇒ Object

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



107
108
109
# 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) ⇒ Object

:reek:UnusedParameters



28
# File 'lib/active_storage/service/openstack_service.rb', line 28

def (key, **); end

#upload(key, io, checksum: nil, **_options) ⇒ Object

:reek:LongParameterList



19
20
21
22
23
24
25
# 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:) ⇒ Object

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



78
79
80
81
82
83
84
85
86
87
88
89
# 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) ⇒ Object

:reek:LongParameterList



92
93
94
95
96
97
98
99
100
101
102
# 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