Class: Backblaze::B2::Bucket

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

Overview

A class to represent the online buckets. Mostly used for file access

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#get, #head, #post, #put

Methods included from Utils

#camelize, included, #underscore

Constructor Details

#initialize(bucket_name:, bucket_id:, bucket_type:, account_id:, cache: false) ⇒ Bucket

Creates a bucket from all of the possible parameters. This sould be rarely used and instead use a finder or creator

Parameters:

  • bucket_name (#to_s)

    the bucket name

  • bucket_id (#to_s)

    the bucket id

  • bucket_type (#to_s)

    the bucket publicity type

  • account_id (#to_s)

    the account to which this bucket belongs



12
13
14
15
16
17
# File 'lib/backblaze/b2/bucket.rb', line 12

def initialize(bucket_name:, bucket_id:, bucket_type:, account_id:, cache: false)
  @bucket_name = bucket_name
  @bucket_id = bucket_id
  @bucket_type = bucket_type
  @account_id = 
end

Class Method Details

.bucketsArray<Backblaze::Bucket>

List buckets for account

Returns:

  • (Array<Backblaze::Bucket>)

    buckets for this account



144
145
146
147
148
149
150
151
152
153
# File 'lib/backblaze/b2/bucket.rb', line 144

def buckets
  body = {
    accountId: Backblaze::B2.
  }
  response = post('/b2_list_buckets', body: body.to_json)
  response['buckets'].map do |bucket|
    params = Hash[bucket.map{|k,v| [Backblaze::Utils.underscore(k).to_sym, v]}]
    new(params)
  end
end

.create(name:, type:) ⇒ Object

Create a bucket

Parameters:

  • name (String)

    name of the new bucket must be no more than 50 character and only contain letters, digits, “-”, and “_”. must be globally unique

  • type (:public, :private)

    determines the type of bucket

Raises:



120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/backblaze/b2/bucket.rb', line 120

def create(name:, type:)
  body = {
    accountId: Backblaze::B2.,
    bucketName: name,
    bucketType: (type == :public ? 'allPublic' : 'allPrivate')
  }
  response = post('/b2_create_bucket', body: body.to_json)

  raise Backblaze::BucketError.new(response) unless response.code / 100 == 2

  params = Hash[response.map{|k,v| [Backblaze::Utils.underscore(k).to_sym, v]}]

  new(params)
end

.upload_url(bucket_id:) ⇒ Object



135
136
137
138
139
# File 'lib/backblaze/b2/bucket.rb', line 135

def upload_url(bucket_id:)
  response = post('/b2_get_upload_url', body: {bucketId: bucket_id}.to_json)
  raise Backblaze::BucketError.new(response) unless response.code / 100 == 2
  {url: response['uploadUrl'], token: response['authorizationToken']}
end

Instance Method Details

#==(other) ⇒ Boolean

Check if eqivalent. Takes advantage of globally unique names

Returns:

  • (Boolean)

    equality



51
52
53
# File 'lib/backblaze/b2/bucket.rb', line 51

def ==(other)
  bucket_name == other.bucket_name
end

#account_idString

Returns account id.

Returns:

  • (String)

    account id



40
41
42
# File 'lib/backblaze/b2/bucket.rb', line 40

def 
  @account_id
end

#bucket_idString

Returns bucket id.

Returns:

  • (String)

    bucket id



25
26
27
# File 'lib/backblaze/b2/bucket.rb', line 25

def bucket_id
  @bucket_id
end

#bucket_nameString

Returns bucket name.

Returns:

  • (String)

    bucket name



20
21
22
# File 'lib/backblaze/b2/bucket.rb', line 20

def bucket_name
  @bucket_name
end

#bucket_typeString

Returns bucket type.

Returns:

  • (String)

    bucket type



45
46
47
# File 'lib/backblaze/b2/bucket.rb', line 45

def bucket_type
  @bucket_type
end

#file_names(limit: 100, cache: false, convert: true, double_check_server: false) ⇒ Array<Backblaze::B2::File>, Array<Hash>

Note:

many of these methods are for the recusion

Lists all files that are in the bucket. This is the basic building block for the search.

Parameters:

  • limit (Integer) (defaults to: 100)

    max number of files to retreive. Set to ‘-1` to get all files. This is not exact as it mainly just throws the limit into max param on the request so it will try to grab at least `limit` files, unless there aren’t enoungh in the bucket

  • cache (Boolean) (defaults to: false)

    if there is no cache, create one. If there is a cache, use it. Will check if the previous cache had the same size limit and convert options

  • convert (Boolean) (defaults to: true)

    convert the files to Backblaze::B2::File objects

  • double_check_server (Integer) (defaults to: false)

    whether or not to assume the server returns the most files possible

Returns:

  • (Array<Backblaze::B2::File>)

    when convert is true

  • (Array<Hash>)

    when convert is false



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/backblaze/b2/bucket.rb', line 67

def file_names(limit: 100, cache: false, convert: true, double_check_server: false)
  if cache && !@file_name_cache.nil?
    if limit <= @file_name_cache[:limit] && convert == @file_name_cache[:convert]
      return @file_name_cache[:files]
    end
  end

  retreive_count = (double_check_server ? 0 : -1)
  files = file_list(bucket_id: bucket_id, limit: limit, retreived: retreive_count, first_file: nil, start_field: 'startFileName'.freeze)

  merge_params = {bucket_id: bucket_id}
  files.map! do |f|
    Backblaze::B2::File.new(f.merge(merge_params))
  end if convert
  if cache
    @file_name_cache = {limit: limit, convert: convert, files: files}
  end
  files
end

#file_versions(limit: 100, cache: false, convert: true, double_check_server: false) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/backblaze/b2/bucket.rb', line 87

def file_versions(limit: 100, cache: false, convert: true, double_check_server: false)
  if cache && !@file_versions_cache.nil?
    if limit <= @file_versions_cache[:limit] && convert == @file_versions_cache[:convert]
      return @file_versions_cache[:files]
    end
  end
  file_versions = super(limit: 100, convert: convert, double_check_server: double_check_server, bucket_id: bucket_id)
  files = file_versions.group_by {|version| convert ? version.file_name : version[:file_name]}
  if convert
    files = files.map do |name, versions|
      File.new(file_name: name, bucket_id: bucket_id, versions: versions)
    end
  end
  if cache
    @file_versions_cache = {limit: limit, convert: convert, files: files}
  else
    @file_versions_cache = {}
  end
  files
end

#private?Boolean

Returns is the bucket private.

Returns:

  • (Boolean)

    is the bucket private



35
36
37
# File 'lib/backblaze/b2/bucket.rb', line 35

def private?
  !public?
end

#public?Boolean

Returns is the bucket public.

Returns:

  • (Boolean)

    is the bucket public



30
31
32
# File 'lib/backblaze/b2/bucket.rb', line 30

def public?
  @bucket_type == 'allPublic'
end

#upload_urlObject



108
109
110
# File 'lib/backblaze/b2/bucket.rb', line 108

def upload_url
  self.class.upload_url(bucket_id: bucket_id)
end