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/api_connection.rb,
lib/b2/upload_chunker.rb

Defined Under Namespace

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

Constant Summary collapse

VERSION =
'1.0.6'

Instance Method Summary collapse

Constructor Details

#initialize(key_id:, secret:) ⇒ B2

Returns a new instance of B2.



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

def initialize(key_id: , secret: )
  @connection = B2::Connection.new(key_id, secret)
end

Instance Method Details

#account_idObject



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

def 
  @connection.
end

#bucket(name) ⇒ Object



26
27
28
29
# File 'lib/b2.rb', line 26

def bucket(name)
  bs = @connection.post('/b2api/v2/b2_list_buckets', {accountId: , bucketName: name})['buckets']
  B2::Bucket.new(bs.first, @connection)
end

#bucketsObject



22
23
24
# File 'lib/b2.rb', line 22

def buckets
  @connection.buckets
end

#delete(bucket, key) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/b2.rb', line 42

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) ⇒ Object



91
92
93
# File 'lib/b2.rb', line 91

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

#download_to_file(bucket, key, filename) ⇒ Object



96
97
98
99
100
101
102
# File 'lib/b2.rb', line 96

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) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/b2.rb', line 31

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) ⇒ Object



87
88
89
# File 'lib/b2.rb', line 87

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

#get_upload_token(bucket) ⇒ Object



54
55
56
57
58
# File 'lib/b2.rb', line 54

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: {}) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/b2.rb', line 60

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