Class: B2

Inherits:
Object
  • Object
show all
Defined in:
lib/b2.rb,
lib/b2/file.rb,
lib/b2/bucket.rb,
lib/b2/errors.rb,
lib/b2/version.rb,
lib/b2/connection.rb,
lib/b2/upload_chunker.rb

Defined Under Namespace

Classes: Bucket, Connection, Error, File, NotFound, UploadChunker

Constant Summary collapse

VERSION =
'1.0.4'

Instance Method Summary collapse

Constructor Details

#initialize(account_id:, application_key:) ⇒ B2

Returns a new instance of B2.



13
14
15
16
# File 'lib/b2.rb', line 13

def initialize(account_id:, application_key:)
  @account_id = 
  @connection = B2::Connection.new(, application_key)
end

Instance Method Details

#bucketsObject



18
19
20
# File 'lib/b2.rb', line 18

def buckets
  @connection.buckets
end

#delete(bucket, key) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/b2.rb', line 33

def delete(bucket, key)
  object = file(bucket, key)
  if object
    @connection.post('/b2api/v1/b2_delete_file_version', {
      fileName: file.name,
      fileId: file.id
    })
  else
    false
  end
end

#download(bucket, key, to = nil, &block) ⇒ Object



82
83
84
# File 'lib/b2.rb', line 82

def download(bucket, key, to=nil, &block)
  @connection.download(bucket, key, to, &block)
end

#download_to_file(bucket, key, filename) ⇒ Object



87
88
89
90
91
92
93
# File 'lib/b2.rb', line 87

def download_to_file(bucket, key, filename)
  file = File.open(filename, 'w')
  download(bucket, key) do |chunk|
    file << chunk
  end
  file.close
end

#file(bucket, key) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/b2.rb', line 22

def file(bucket, key)
  bucket_id = @connection.lookup_bucket_id(bucket)
  
  file = @connection.post('/b2api/v1/b2_list_file_names', {
    bucketId: bucket_id,
    startFileName: key
  })['files'].find {|f| f['fileName'] == key }

  file ? B2::File.new(file.merge({'bucketId' => bucket_id})) : nil
end

#get_download_url(bucket, filename, **options) ⇒ Object



78
79
80
# File 'lib/b2.rb', line 78

def get_download_url(bucket, filename, **options)
  @connection.get_download_url(bucket, filename, **options)
end

#get_upload_token(bucket) ⇒ Object



45
46
47
48
49
# File 'lib/b2.rb', line 45

def get_upload_token(bucket)
  @connection.post("/b2api/v1/b2_get_upload_url", {
    bucketId: @connection.lookup_bucket_id(bucket)
  })
end

#upload_file(bucket, key, io_or_string, mime_type: nil, info: {}) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/b2.rb', line 51

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::File.encode_filename(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