class ActiveStorage::Service::BackblazeService
Public Class Methods
new(key_id:, key_token:, bucket_name:, bucket_id:)
click to toggle source
# 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
Public Instance Methods
delete(key)
click to toggle source
# 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)
click to toggle source
# File lib/active_storage/service/backblaze_service.rb, line 57 def delete_prefixed(prefix) delete(prefix) end
download(key, &block)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# File lib/active_storage/service/backblaze_service.rb, line 92 def headers_for_direct_upload(key, content_type:, checksum:, content_length:, **options) 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)
click to toggle source
# File lib/active_storage/service/backblaze_service.rb, line 19 def upload(key, io, checksum: nil, **options) 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)
click to toggle source
# File lib/active_storage/service/backblaze_service.rb, line 74 def url(key, expires_in:, disposition:, filename:, **options) 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:)
click to toggle source
# 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
Private Instance Methods
stream(key, options = {}) { |chunk| ... }
click to toggle source
# File lib/active_storage/service/backblaze_service.rb, line 98 def stream(key, options = {}, &block) resp = @connection.get_object(@bucket_name, key) io = StringIO.new(resp.body) io.binmode chunk_size = 5.megabytes while chunk = io.read(chunk_size) yield chunk end end