class Backup::Backblaze::Account

Attributes

account_id[R]
app_key[R]
body_wrap[R]

Public Class Methods

new(account_id:, app_key: @account_id = account_id) click to toggle source
# File lib/backup/backblaze/account.rb, line 7
def initialize account_id:, app_key:
  @account_id = account_id
  @app_key = app_key
  auth!
end

Public Instance Methods

api_url() click to toggle source
# File lib/backup/backblaze/account.rb, line 43
def api_url
  body_wrap.apiUrl or raise NotFound, 'apiUrl'
end
auth!() click to toggle source

This can be called by retry paths for various api calls. So it might end up needing synchronisation of some kind.

# File lib/backup/backblaze/account.rb, line 27
def auth!
  # first call b2_authorize_account to get an account_auth_token
  # this has to stick around because it has various important data
  b2_authorize_account

  unless body_wrap.allowed.capabilities.include? 'writeFiles'
    raise "app_key #{app_key} does not have write access to account #{account_id}"
  end
end
auth_headers() click to toggle source
# File lib/backup/backblaze/account.rb, line 37
def auth_headers
  Hash headers: {
    'Authorization' => authorization_token,
  }.merge(TEST_HEADERS)
end
authorization_token() click to toggle source
# File lib/backup/backblaze/account.rb, line 47
def authorization_token
  body_wrap.authorizationToken or raise NotFound, 'authorizationToken'
end
bucket_id(bucket_name: buckets = b2_list_buckets(bucketName: bucket_name, accountId: account_id).buckets) click to toggle source

return id for given name, or nil if no such named bucket

# File lib/backup/backblaze/account.rb, line 67
def bucket_id bucket_name:
  buckets = b2_list_buckets(bucketName: bucket_name, accountId: account_id).buckets
  found = buckets.find{|hw| hw.bucketName == bucket_name}
  found&.bucketId or raise NotFound, "no bucket named #{bucket_name}"
end
bucket_list(bucket_id: nil) click to toggle source

Hurhur

# File lib/backup/backblaze/account.rb, line 74
def bucket_list bucket_id: nil
  b2_list_buckets bucketId: bucket_id, accountId: account_id
end
delete_file(bucket_name, filename) click to toggle source
# File lib/backup/backblaze/account.rb, line 104
def delete_file bucket_name, filename
  # lookup fileId from given filename
  info = file_info bucket_name, filename
  body_wrap = b2_delete_file_version fileId: info.fileId, fileName: filename

# ignore 400 with body containing "code": "file_not_present"
rescue Excon::Errors::BadRequest => ex
  body_wrap = HashWrap.from_json ex.response.body
  raise unless body_wrap.code == 'file_not_present'
end
file_info(bucket_name, filename) click to toggle source

This is mostly used to get a fileId for a given fileName

# File lib/backup/backblaze/account.rb, line 92
def file_info bucket_name, filename
  body_wrap = b2_list_file_names bucketId: (bucket_id bucket_name: bucket_name), maxFileCount: 1, startFileName: filename
  files_hash = body_wrap.files
  raise NotFound, "#{filename} not found" unless files_hash.size == 1
  files_hash.first
end
files(bucket_name) click to toggle source

This might be dangerous because large number of file names might come back. But I'm not worrying about that now. Maybe later. Anyway, that's what nextFile and startFile are for.

# File lib/backup/backblaze/account.rb, line 85
def files bucket_name
  body_wrap = b2_list_file_names bucketId: (bucket_id bucket_name: bucket_name)
  # ignoring the top-level {files:, nextFileName:} structure
  body_wrap.files
end
minimum_part_size() click to toggle source
# File lib/backup/backblaze/account.rb, line 51
def minimum_part_size
  # why b2 has this as well as minimumPartSize ¯\_(ツ)_/¯
  body_wrap.absoluteMinimumPartSize
end