class B2

Constants

VERSION

Public Class Methods

decode(value) click to toggle source
# File lib/b2.rb, line 18
def self.decode(value)
  URI.decode_www_form_component(value, Encoding::UTF_8)
end
encode(value) click to toggle source
# File lib/b2.rb, line 14
def self.encode(value)
  URI.encode_www_form_component(value.force_encoding(Encoding::UTF_8)).gsub("%2F", "/")
end
new(key_id: , secret: ) click to toggle source
# File lib/b2.rb, line 22
def initialize(key_id: , secret: )
  @connection = B2::Connection.new(key_id, secret)
end

Public Instance Methods

account_id() click to toggle source
# File lib/b2.rb, line 26
def account_id
  @connection.account_id
end
bucket(name) click to toggle source
# File lib/b2.rb, line 34
def bucket(name)
  bs = @connection.post('/b2api/v2/b2_list_buckets', {accountId: account_id, bucketName: name})['buckets']
  B2::Bucket.new(bs.first, @connection)
end
buckets() click to toggle source
# File lib/b2.rb, line 30
def buckets
  @connection.buckets
end
delete(bucket, key) click to toggle source
# File lib/b2.rb, line 50
def delete(bucket, key)
  object = file(bucket, key)
  if object
    @connection.post('/b2api/v2/b2_delete_file_version', {
      fileName: object.name,
      fileId: object.id
    })
  else
    false
  end
end
download(bucket, key, to=nil, &block) click to toggle source
# File lib/b2.rb, line 99
def download(bucket, key, to=nil, &block)
  @connection.download(bucket, key, to, &block)
end
download_to_file(bucket, key, filename) click to toggle source
# File lib/b2.rb, line 104
def download_to_file(bucket, key, filename)
  file = ::File.open(filename, 'wb')
  download(bucket, key) do |chunk|
    file << chunk
  end
  file.close
end
file(bucket, key) click to toggle source
# File lib/b2.rb, line 39
def file(bucket, key)
  bucket_id = @connection.lookup_bucket_id(bucket)
  
  file = @connection.post('/b2api/v2/b2_list_file_names', {
    bucketId: bucket_id,
    startFileName: key
  })['files'].find {|f| f['fileName'] == key }

  file ? B2::File.new(file.merge({'bucketId' => bucket_id}), @connection) : nil
end
get_download_url(bucket, filename, **options) click to toggle source
# File lib/b2.rb, line 95
def get_download_url(bucket, filename, **options)
  @connection.get_download_url(bucket, filename, **options)
end
get_upload_token(bucket) click to toggle source
# File lib/b2.rb, line 62
def get_upload_token(bucket)
  @connection.post("/b2api/v2/b2_get_upload_url", {
    bucketId: @connection.lookup_bucket_id(bucket)
  })
end
upload_file(bucket, key, io_or_string, mime_type: nil, info: {}) click to toggle source
# File lib/b2.rb, line 68
def upload_file(bucket, key, io_or_string, mime_type: nil, info: {})
  upload = get_upload_token(bucket)

  uri = URI.parse(upload['uploadUrl'])
  conn = Net::HTTP.new(uri.host, uri.port)
  conn.use_ssl = uri.scheme == 'https'

  chunker = B2::UploadChunker.new(io_or_string)
  req = Net::HTTP::Post.new(uri.path)
  req['Authorization']      = upload['authorizationToken']
  req['X-Bz-File-Name']     = B2.encode(key)
  req['Content-Type']       = mime_type || 'b2/x-auto'
  req['X-Bz-Content-Sha1']  = 'hex_digits_at_end'
  info.each do |key, value|
    req["X-Bz-Info-#{key}"] = value
  end
  req['Content-Length']     = chunker.size
  req.body_stream           = chunker

  resp = conn.start { |http| http.request(req) }
  if resp.is_a?(Net::HTTPSuccess)
    JSON.parse(resp.body)
  else
    raise "Error connecting to B2 API"
  end
end