Class: Service::OpenStackService
- Inherits:
-
Service
- Object
- Service
- Service::OpenStackService
- Defined in:
- lib/active_storage/service/open_stack_service.rb
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#container ⇒ Object
readonly
Returns the value of attribute container.
Instance Method Summary collapse
-
#change_content_type(key, content_type) ⇒ Object
Non-standard method to change the content type of an existing object.
- #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:, connection_options: {}) ⇒ OpenStackService
constructor
A new instance of OpenStackService.
- #upload(key, io, checksum: nil) ⇒ Object
- #url(key, expires_in:, disposition:, filename:) ⇒ Object
- #url_for_direct_upload(key, expires_in:) ⇒ Object
Constructor Details
#initialize(container:, credentials:, connection_options: {}) ⇒ OpenStackService
Returns a new instance of OpenStackService.
7 8 9 10 11 12 13 14 15 |
# File 'lib/active_storage/service/open_stack_service.rb', line 7 def initialize(container:, credentials:, connection_options: {}) settings = if .present? credentials.reverse_merge(connection_options: ) else credentials end @client = Fog::Storage::OpenStack.new(settings) @container = Fog::OpenStack.escape(container) end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
5 6 7 |
# File 'lib/active_storage/service/open_stack_service.rb', line 5 def client @client end |
#container ⇒ Object (readonly)
Returns the value of attribute container.
5 6 7 |
# File 'lib/active_storage/service/open_stack_service.rb', line 5 def container @container end |
Instance Method Details
#change_content_type(key, content_type) ⇒ Object
Non-standard method to change the content type of an existing object
120 121 122 123 124 125 126 127 |
# File 'lib/active_storage/service/open_stack_service.rb', line 120 def change_content_type(key, content_type) client.post_object(container, key, 'Content-Type' => content_type) true rescue Fog::Storage::OpenStack::NotFound false end |
#delete(key) ⇒ Object
55 56 57 58 59 60 61 |
# File 'lib/active_storage/service/open_stack_service.rb', line 55 def delete(key) instrument :delete, key: key do client.delete_object(container, key) rescue Fog::Storage::OpenStack::NotFound false end end |
#delete_prefixed(prefix) ⇒ Object
63 64 65 66 67 68 69 70 71 |
# File 'lib/active_storage/service/open_stack_service.rb', line 63 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
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/active_storage/service/open_stack_service.rb', line 31 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 end |
#download_chunk(key, range) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/active_storage/service/open_stack_service.rb', line 43 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] end end |
#exist?(key) ⇒ Boolean
73 74 75 76 77 78 79 80 |
# File 'lib/active_storage/service/open_stack_service.rb', line 73 def exist?(key) instrument :exist, key: key do |payload| answer = object_for(key) payload[:exist] = answer.present? rescue Fog::Storage::OpenStack::NotFound payload[:exist] = false end end |
#headers_for_direct_upload(_key, content_type:, checksum:) ⇒ Object
112 113 114 115 116 117 |
# File 'lib/active_storage/service/open_stack_service.rb', line 112 def headers_for_direct_upload(_key, content_type:, checksum:, **) { 'Content-Type' => content_type, 'ETag' => convert_base64digest_to_hexdigest(checksum) } end |
#upload(key, io, checksum: nil) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/active_storage/service/open_stack_service.rb', line 17 def upload(key, io, checksum: nil) instrument :upload, key: key, checksum: checksum do params = { 'Content-Type' => guess_content_type(io), 'ETag' => convert_base64digest_to_hexdigest(checksum) } begin client.put_object(container, key, io, params) rescue Excon::Error::UnprocessableEntity raise ActiveStorage::IntegrityError end end end |
#url(key, expires_in:, disposition:, filename:) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/active_storage/service/open_stack_service.rb', line 82 def url(key, expires_in:, disposition:, filename:, **) instrument :url, key: key do |payload| expire_at = (expires_in) generated_url = client.get_object_https_url(container, key, expire_at) generated_url += '&inline' if disposition.to_s != 'attachment' generated_url += "&filename=#{Fog::OpenStack.escape(filename.to_s)}" unless filename.nil? # unfortunately OpenStack Swift cannot overwrite the content type of an object via a temp url # so we just ignore the content_type argument here payload[:url] = generated_url generated_url end end |
#url_for_direct_upload(key, expires_in:) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/active_storage/service/open_stack_service.rb', line 96 def url_for_direct_upload(key, expires_in:, **) instrument :url, key: key do |payload| expire_at = (expires_in) generated_url = client.create_temp_url(container, key, expire_at, 'PUT', port: 443, scheme: 'https') payload[:url] = generated_url generated_url end end |