Class: ActiveStorage::Service::BackblazeService
- Inherits:
-
ActiveStorage::Service
- Object
- ActiveStorage::Service
- ActiveStorage::Service::BackblazeService
- Defined in:
- lib/active_storage/service/backblaze_service.rb
Instance Method Summary collapse
- #delete(key) ⇒ Object
- #delete_prefixed(prefix) ⇒ Object
- #download(key, &block) ⇒ Object
- #exist?(key) ⇒ Boolean
- #headers_for_direct_upload(key, content_type:, checksum:, content_length:, **options) ⇒ Object
-
#initialize(key_id:, key_token:, bucket_name:, bucket_id:) ⇒ BackblazeService
constructor
A new instance of BackblazeService.
- #upload(key, io, checksum: nil, **options) ⇒ Object
- #url(key, expires_in:, disposition:, filename:, **options) ⇒ Object
- #url_for_direct_upload(key, expires_in:, content_type:, content_length:, checksum:) ⇒ Object
Constructor Details
#initialize(key_id:, key_token:, bucket_name:, bucket_id:) ⇒ BackblazeService
Returns a new instance of BackblazeService.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/active_storage/service/backblaze_service.rb', line 4 def initialize(key_id:, key_token:, bucket_name:, bucket_id:) @key_id = key_id @key_token = key_token @bucket_name = bucket_name @bucket_id = bucket_id @connection = Fog::Storage.new( provider: 'backblaze', b2_key_id: @key_id, b2_key_token: @key_token, b2_bucket_name: @bucket_name, b2_bucket_id: @bucket_id, logger: Rails.logger ) end |
Instance Method Details
#delete(key) ⇒ Object
47 48 49 50 51 52 53 54 55 |
# File 'lib/active_storage/service/backblaze_service.rb', line 47 def delete(key) instrument :delete, { key: key } do begin @connection.delete_object(@bucket_name, key) rescue => e false end end end |
#delete_prefixed(prefix) ⇒ Object
57 58 59 |
# File 'lib/active_storage/service/backblaze_service.rb', line 57 def delete_prefixed(prefix) delete(prefix) end |
#download(key, &block) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/active_storage/service/backblaze_service.rb', line 33 def download(key, &block) if block_given? instrument :streaming_download, { key: key } do stream(key, &block) end else instrument :download, { key: key } do resp = @connection.get_object(@bucket_name, key) io = StringIO.new(resp.body) io end end end |
#exist?(key) ⇒ Boolean
61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/active_storage/service/backblaze_service.rb', line 61 def exist?(key) instrument :exist, { key: key } do |payload| answer = false begin @connection.head_object(@bucket_name, key) answer = true rescue => e end payload[:exist] = answer answer end end |
#headers_for_direct_upload(key, content_type:, checksum:, content_length:, **options) ⇒ Object
92 93 94 95 |
# File 'lib/active_storage/service/backblaze_service.rb', line 92 def headers_for_direct_upload(key, content_type:, checksum:, content_length:, **) result = @connection.b2_get_upload_url(@bucket_name) { 'Authorization' => result["authorizationToken"], 'X-Bz-File-Name' => key, 'Content-Type' => content_type, 'Content-Length' => content_length, 'X-Bz-Content-Sha1' => 'do_not_verify' } end |
#upload(key, io, checksum: nil, **options) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/active_storage/service/backblaze_service.rb', line 19 def upload(key, io, checksum: nil, **) instrument :upload, { key: key, checksum: checksum } do begin io.binmode io.rewind @connection.put_object(@bucket_name, key, io.read) rescue => e puts "ERROR - 101" puts e.inspect raise ActiveStorage::IntegrityError end end end |
#url(key, expires_in:, disposition:, filename:, **options) ⇒ Object
74 75 76 77 78 79 80 81 |
# File 'lib/active_storage/service/backblaze_service.rb', line 74 def url(key, expires_in:, disposition:, filename:, **) instrument :url, {key: key} do |payload| url = @connection.get_public_object_url(@bucket_name, key) payload[:url] = url url end end |
#url_for_direct_upload(key, expires_in:, content_type:, content_length:, checksum:) ⇒ Object
83 84 85 86 87 88 89 90 |
# File 'lib/active_storage/service/backblaze_service.rb', line 83 def url_for_direct_upload(key, expires_in:, content_type:, content_length:, checksum:) instrument :url, { key: key} do |payload| result = @connection.b2_get_upload_url(@bucket_name) url = result["uploadUrl"] payload[:url] = url url end end |