class Backup::Backblaze::UploadLargeFile

Upload a large file in several parts.

Constants

MAX_PARTS

10000 is backblaze specified max number of parts

Attributes

account[R]
bucket_id[R]
content_type[R]
dst[R]
part_size[R]
src[R]
url[R]

Public Class Methods

new(account:, src:, bucket_id:, dst:, url_token: nil, part_size:, content_type: nil) click to toggle source

src is a Pathname dst is a String

# File lib/backup/backblaze/upload_large_file.rb, line 12
def initialize account:, src:, bucket_id:, dst:, url_token: nil, part_size:, content_type: nil
  @account = account
  @src = src
  @dst = dst
  @bucket_id = bucket_id
  @content_type = content_type
  @url_token = url_token
  @part_size = part_size
end

Public Instance Methods

auth_headers() click to toggle source

same as account

# File lib/backup/backblaze/upload_large_file.rb, line 25
def auth_headers
  # only cos the double {{}} is a quite ugly :-p
  Hash headers: {
    'Authorization' => account.authorization_token,
  }.merge(TEST_HEADERS)
end
b2_authorize_account(retries:, backoff:) click to toggle source

needed for retry logic

# File lib/backup/backblaze/upload_large_file.rb, line 54
def b2_authorize_account(retries:, backoff:)
  account.b2_authorize_account retries: retries, backoff: backoff
end
call() click to toggle source
# File lib/backup/backblaze/upload_large_file.rb, line 133
def call
  if src.size > part_size * MAX_PARTS
    raise Error, "File #{src.to_s} has size #{src.size} which is larger than part_size * MAX_PARTS #{part_size * MAX_PARTS}. Try increasing part_size in model."
  end

  Logger.info "uploading '#{src}' to #{dst}' of #{src.size} in #{part_count} parts"

  b2_start_large_file # not really necessary, but makes the flow clearer
  url_token # try to re-use existing url token if there is one
  shas = upload_parts

  # finish up, log and return the response
  hash_wrap = b2_finish_large_file shas
  Backup::Logger.info "#{src} finished"
  hash_wrap
end
content_disposition() click to toggle source

No idea what has to be in here

# File lib/backup/backblaze/upload_large_file.rb, line 37
def content_disposition
end
content_length() click to toggle source
# File lib/backup/backblaze/upload_large_file.rb, line 40
def content_length
  src.size
end
file_id() click to toggle source
# File lib/backup/backblaze/upload_large_file.rb, line 75
def file_id
  @file_id or b2_start_large_file
end
last_modified_millis() click to toggle source
# File lib/backup/backblaze/upload_large_file.rb, line 44
def last_modified_millis
  @last_modified_millis ||= begin
    time = File.lstat(src).mtime
    time.tv_sec * 1000 + time.tv_usec / 1000
  end
end
part_count() click to toggle source
# File lib/backup/backblaze/upload_large_file.rb, line 88
def part_count
  @part_count ||= (src.size / part_size.to_r).ceil
end
upload_parts() click to toggle source
# File lib/backup/backblaze/upload_large_file.rb, line 116
def upload_parts
  (0...MAX_PARTS).each_with_object [] do |sequence, shas|
    # read length, offset
    bytes = src.read part_size, part_size * sequence

    if bytes.nil? || bytes.empty?
      # no more file to send
      break shas
    else
      sha = Digest::SHA1.hexdigest bytes
      b2_upload_part sequence, bytes, sha
      Backup::Logger.info "#{src} stored part #{sequence + 1} with #{sha}"
      shas << sha
    end
  end
end
url_token() click to toggle source
# File lib/backup/backblaze/upload_large_file.rb, line 79
def url_token
  @url_token or b2_get_upload_part_url
end