Class: Backblaze::B2::Bucket
Overview
A class to represent the online buckets. Mostly used for file access
Class Method Summary collapse
-
.buckets ⇒ Array<Backblaze::Bucket>
List buckets for account.
-
.create(name:, type:) ⇒ Object
Create a bucket.
- .upload_url(bucket_id:) ⇒ Object
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Check if eqivalent.
-
#account_id ⇒ String
Account id.
-
#bucket_id ⇒ String
Bucket id.
-
#bucket_name ⇒ String
Bucket name.
-
#bucket_type ⇒ String
Bucket type.
-
#file_names(limit: 100, cache: false, convert: true, double_check_server: false) ⇒ Array<Backblaze::B2::File>, Array<Hash>
Lists all files that are in the bucket.
- #file_versions(limit: 100, cache: false, convert: true, double_check_server: false) ⇒ Object
-
#initialize(bucket_name:, bucket_id:, bucket_type:, account_id:, cache: false) ⇒ Bucket
constructor
Creates a bucket from all of the possible parameters.
-
#private? ⇒ Boolean
Is the bucket private.
-
#public? ⇒ Boolean
Is the bucket public.
- #upload_url ⇒ Object
Methods inherited from Base
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
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 = account_id end |
Class Method Details
.buckets ⇒ Array<Backblaze::Bucket>
List buckets for 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.account_id } 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
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.account_id, 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
51 52 53 |
# File 'lib/backblaze/b2/bucket.rb', line 51 def ==(other) bucket_name == other.bucket_name end |
#account_id ⇒ String
40 41 42 |
# File 'lib/backblaze/b2/bucket.rb', line 40 def account_id @account_id end |
#bucket_id ⇒ String
25 26 27 |
# File 'lib/backblaze/b2/bucket.rb', line 25 def bucket_id @bucket_id end |
#bucket_name ⇒ String
20 21 22 |
# File 'lib/backblaze/b2/bucket.rb', line 20 def bucket_name @bucket_name end |
#bucket_type ⇒ String
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.
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
35 36 37 |
# File 'lib/backblaze/b2/bucket.rb', line 35 def private? !public? end |
#public? ⇒ Boolean
30 31 32 |
# File 'lib/backblaze/b2/bucket.rb', line 30 def public? @bucket_type == 'allPublic' end |
#upload_url ⇒ Object
108 109 110 |
# File 'lib/backblaze/b2/bucket.rb', line 108 def upload_url self.class.upload_url(bucket_id: bucket_id) end |