Module: Backblaze::B2

Defined in:
lib/backblaze/b2.rb,
lib/backblaze/b2/base.rb,
lib/backblaze/b2/file.rb,
lib/backblaze/b2/bucket.rb,
lib/backblaze/b2/file_version.rb

Defined Under Namespace

Classes: Base, Bucket, File, FileVersion

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.account_idObject (readonly)

Returns the value of attribute account_id.



8
9
10
# File 'lib/backblaze/b2.rb', line 8

def 
  @account_id
end

.api_pathObject (readonly)

Returns the value of attribute api_path.



8
9
10
# File 'lib/backblaze/b2.rb', line 8

def api_path
  @api_path
end

.api_urlObject (readonly)

Returns the value of attribute api_url.



8
9
10
# File 'lib/backblaze/b2.rb', line 8

def api_url
  @api_url
end

.download_urlObject (readonly)

Returns the value of attribute download_url.



8
9
10
# File 'lib/backblaze/b2.rb', line 8

def download_url
  @download_url
end

.tokenObject (readonly)

Returns the value of attribute token.



8
9
10
# File 'lib/backblaze/b2.rb', line 8

def token
  @token
end

Class Method Details

.credentials_file(filename, raise_errors: true, logging: false) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/backblaze/b2.rb', line 35

def credentials_file(filename, raise_errors: true, logging: false)
  opts = nil
  open(filename, 'r') do |f|
    if ::File.extname(filename) == '.json'
      require 'json'
      opts = JSON.load(f)
    else
      require 'psych'
      opts = Psych.load(f.read)
    end
  end
  parsed = {}
  [:application_key, :account_id, :api_path].each do |key|
    if opts[key.to_s].is_a? String
      parsed[key] = opts[key.to_s]
    end
  end
  if [:application_key, :account_id].inject(true) { |status, key| status && !parsed[key].nil? }
    puts "Attempting #{parsed[:account_id]}" if logging
    (parsed)
    true
  else
    puts "Missing params" if logging
    false
  end
rescue => e
  puts e if logging
  raise e if raise_errors
  false
end

.login(account_id:, application_key:, api_path: '/b2api/v1/') ⇒ void

This method returns an undefined value.

Authenticates with the server to get the authorization data. Raises an error if there is a problem

Parameters:

  • account_id (#to_s)

    the account id

  • application_key (#to_s)

    the private app key

Raises:



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/backblaze/b2.rb', line 17

def (account_id:, application_key:, api_path: '/b2api/v1/')
  options = {
    basic_auth: {username: , password: application_key}
  }
  api_path = "/#{api_path}/".gsub(/\/+/, '/')
  response = HTTParty.get("https://api.backblaze.com#{api_path}b2_authorize_account", options)
  raise Backblaze::AuthError.new(response) unless response.code == 200

  @account_id = response['accountId']
  @token = response['authorizationToken']
  @api_url = response['apiUrl']
  @download_url = response['downloadUrl']
  @api_path = api_path

  Backblaze::B2::Base.base_uri "#{@api_url}#{api_path}"
  Backblaze::B2::Base.headers 'Authorization' => @token, 'Content-Type' => 'application/json'
end