Class: B2::Bucket

Inherits:
Object
  • Object
show all
Defined in:
lib/b2/bucket.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs, connection) ⇒ Bucket

Returns a new instance of Bucket.



6
7
8
9
10
11
12
13
14
# File 'lib/b2/bucket.rb', line 6

def initialize(attrs, connection)
  @id = attrs['bucketId']
  @name = attrs['bucketName']
  
  @account_id = attrs['accountId']
  @revision = attrs['revision']
  
  @connection = connection
end

Instance Attribute Details

#account_idObject (readonly)

Returns the value of attribute account_id.



4
5
6
# File 'lib/b2/bucket.rb', line 4

def 
  @account_id
end

#idObject (readonly)

Returns the value of attribute id.



4
5
6
# File 'lib/b2/bucket.rb', line 4

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/b2/bucket.rb', line 4

def name
  @name
end

#revisionObject (readonly)

Returns the value of attribute revision.



4
5
6
# File 'lib/b2/bucket.rb', line 4

def revision
  @revision
end

Instance Method Details

#delete!(key) ⇒ Object



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

def delete!(key)
  file(key).delete!
end

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



84
85
86
# File 'lib/b2/bucket.rb', line 84

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

#file(key) ⇒ Object



69
70
71
72
73
74
75
76
77
78
# File 'lib/b2/bucket.rb', line 69

def file(key)
  file = @connection.post('/b2api/v2/b2_list_file_names', {
    bucketId: @id,
    startFileName: key,
    maxFileCount: 1,
    prefix: key
  })['files'].first

  file ? B2::File.new(file.merge({'bucketId' => @id}), @connection) : nil
end

#get_download_url(key, **options) ⇒ Object



80
81
82
# File 'lib/b2/bucket.rb', line 80

def get_download_url(key, **options)
  @connection.get_download_url(@name, key, **options)
end

#get_upload_tokenObject



16
17
18
# File 'lib/b2/bucket.rb', line 16

def get_upload_token
  @connection.post("/b2api/v2/b2_get_upload_url", { bucketId: @id })
end

#has_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
63
64
65
66
67
# File 'lib/b2/bucket.rb', line 60

def has_key?(key)
  !@connection.post('/b2api/v2/b2_list_file_names', {
    bucketId: @id,
    startFileName: key,
    maxFileCount: 1,
    prefix: key
  })['files'].empty?
end

#keys(prefix: nil, delimiter: nil) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/b2/bucket.rb', line 50

def keys(prefix: nil, delimiter: nil)
  #TODO: add abilty to get all names
  @connection.post('/b2api/v2/b2_list_file_names', {
    bucketId: @id,
    maxFileCount: 1000,
    prefix: prefix,
    delimiter: delimiter
  })['files'].map{ |f| f['fileName'] }
end

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



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/b2/bucket.rb', line 20

def upload_file(key, io_or_string, mime_type: nil, sha1: nil, content_disposition: nil, info: {})
  upload = get_upload_token
  
  uri = URI.parse(upload['uploadUrl'])
  conn = Net::HTTP.new(uri.host, uri.port)
  conn.use_ssl = uri.scheme == 'https'

  chunker = sha1 ? io_or_string : 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']  = sha1 ? sha1 : 'hex_digits_at_end'
  req['X-Bz-Info-b2-content-disposition'] = B2.encode(content_disposition) if content_disposition
  info.each do |key, value|
    req["X-Bz-Info-#{key}"] = B2.encode(value)
  end
  req['Content-Length']     = chunker.size
  req.body_stream           = chunker

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