Class: ActiveStorage::Service::OpenStackService
- Inherits:
-
ActiveStorage::Service
- Object
- ActiveStorage::Service
- ActiveStorage::Service::OpenStackService
- Defined in:
- lib/active_storage/service/open_stack_service.rb
Overview
ActiveStorage provider for OpenStack
Instance Attribute Summary collapse
-
#container ⇒ Object
readonly
Returns the value of attribute container.
-
#settings ⇒ Object
readonly
Returns the value of attribute settings.
Instance Method Summary collapse
- #delete(key) ⇒ Object
- #delete_prefixed(prefix) ⇒ Object
- #download(key, &block) ⇒ Object
- #download_chunk(key, range) ⇒ Object
- #exist?(key) ⇒ Boolean
- #headers_for_direct_upload(_key, content_type:, checksum:) ⇒ Object
-
#initialize(container:, credentials:, public: false, connection_options: {}) ⇒ OpenStackService
constructor
A new instance of OpenStackService.
- #update_metadata(key, content_type:, disposition: nil, filename: nil) ⇒ Object
- #upload(key, io, checksum: nil, disposition: nil, content_type: nil, filename: nil) ⇒ Object
- #url(key, **options) ⇒ Object
- #url_for_direct_upload(key, expires_in:, filename: nil) ⇒ Object
Constructor Details
#initialize(container:, credentials:, public: false, connection_options: {}) ⇒ OpenStackService
Returns a new instance of OpenStackService.
11 12 13 14 15 16 17 |
# File 'lib/active_storage/service/open_stack_service.rb', line 11 def initialize(container:, credentials:, public: false, connection_options: {}) super() @settings = credentials.reverse_merge(connection_options: ) @public = public @container = Fog::OpenStack.escape(container) end |
Instance Attribute Details
#container ⇒ Object (readonly)
Returns the value of attribute container.
9 10 11 |
# File 'lib/active_storage/service/open_stack_service.rb', line 9 def container @container end |
#settings ⇒ Object (readonly)
Returns the value of attribute settings.
9 10 11 |
# File 'lib/active_storage/service/open_stack_service.rb', line 9 def settings @settings end |
Instance Method Details
#delete(key) ⇒ Object
64 65 66 67 68 69 70 |
# File 'lib/active_storage/service/open_stack_service.rb', line 64 def delete(key) instrument :delete, key: key do client.delete_object(container, key) rescue Fog::OpenStack::Storage::NotFound false end end |
#delete_prefixed(prefix) ⇒ Object
72 73 74 75 76 77 78 79 80 |
# File 'lib/active_storage/service/open_stack_service.rb', line 72 def delete_prefixed(prefix) instrument :delete, prefix: prefix do directory = client.directories.get(container) filtered_files = client.files(directory: directory, prefix: prefix) filtered_files = filtered_files.map(&:key) client.delete_multiple_objects(container, filtered_files) end end |
#download(key, &block) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/active_storage/service/open_stack_service.rb', line 36 def download(key, &block) if block_given? instrument :streaming_download, key: key do object_for(key, &block) end else instrument :download, key: key do object_for(key).body end end rescue Fog::OpenStack::Storage::NotFound raise ActiveStorage::FileNotFoundError if defined?(ActiveStorage::FileNotFoundError) end |
#download_chunk(key, range) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/active_storage/service/open_stack_service.rb', line 50 def download_chunk(key, range) instrument :download_chunk, key: key, range: range do chunk_buffer = [] object_for(key) do |chunk| chunk_buffer << chunk end chunk_buffer.join[range] rescue Fog::OpenStack::Storage::NotFound raise ActiveStorage::FileNotFoundError if defined?(ActiveStorage::FileNotFoundError) end end |
#exist?(key) ⇒ Boolean
82 83 84 85 86 87 88 89 |
# File 'lib/active_storage/service/open_stack_service.rb', line 82 def exist?(key) instrument :exist, key: key do |payload| answer = head_for(key) payload[:exist] = answer.present? rescue Fog::OpenStack::Storage::NotFound payload[:exist] = false end end |
#headers_for_direct_upload(_key, content_type:, checksum:) ⇒ Object
122 123 124 125 126 127 |
# File 'lib/active_storage/service/open_stack_service.rb', line 122 def headers_for_direct_upload(_key, content_type:, checksum:, **) { 'Content-Type' => content_type, 'ETag' => convert_base64digest_to_hexdigest(checksum) } end |
#update_metadata(key, content_type:, disposition: nil, filename: nil) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/active_storage/service/open_stack_service.rb', line 129 def (key, content_type:, disposition: nil, filename: nil, **) instrument :update_metadata, key: key, content_type: content_type, disposition: disposition do params = { 'Content-Type' => content_type } if disposition && filename params['Content-Disposition'] = content_disposition_with( type: disposition, filename: ActiveStorage::Filename.wrap(filename) ) end client.post_object(container, key, params) true rescue Fog::OpenStack::Storage::NotFound raise ActiveStorage::FileNotFoundError if defined?(ActiveStorage::FileNotFoundError) end end |
#upload(key, io, checksum: nil, disposition: nil, content_type: nil, filename: nil) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/active_storage/service/open_stack_service.rb', line 19 def upload(key, io, checksum: nil, disposition: nil, content_type: nil, filename: nil, **) instrument :upload, key: key, checksum: checksum do params = { 'Content-Type' => content_type || guess_content_type(io) } params['ETag'] = convert_base64digest_to_hexdigest(checksum) if checksum if disposition && filename params['Content-Disposition'] = content_disposition_with(type: disposition, filename: filename) end begin client.put_object(container, key, io, params) rescue Excon::Error::UnprocessableEntity raise ActiveStorage::IntegrityError end end end |
#url(key, **options) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/active_storage/service/open_stack_service.rb', line 91 def url(key, **) if ActiveStorage.version < Gem::Version.new('6.1-alpha') instrument :url, key: key do |payload| private_url(key, **).tap do |generated_url| payload[:url] = generated_url end end else super end end |
#url_for_direct_upload(key, expires_in:, filename: nil) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/active_storage/service/open_stack_service.rb', line 103 def url_for_direct_upload(key, expires_in:, filename: nil, **) instrument :url, key: key do |payload| expire_at = (expires_in) generated_url = client.create_temp_url( container, key, expire_at, 'PUT', port: 443, **(filename ? { filename: ActiveStorage::Filename.wrap(filename).to_s } : {}), scheme: 'https' ) payload[:url] = generated_url generated_url end end |