Class: B2
- Inherits:
-
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, ExpiredAuthToken, File, FileIntegrityError, NotFound, UploadChunker
Constant Summary
collapse
- VERSION =
'1.0.7'
Class Method Summary
collapse
Instance Method Summary
collapse
-
#account_id ⇒ Object
-
#bucket(name) ⇒ Object
-
#buckets ⇒ Object
-
#delete(bucket, key) ⇒ Object
-
#download(bucket, key, to = nil, &block) ⇒ Object
-
#download_to_file(bucket, key, filename) ⇒ Object
-
#file(bucket, key) ⇒ Object
-
#get_download_url(bucket, filename, **options) ⇒ Object
-
#get_upload_token(bucket) ⇒ Object
-
#initialize(key_id:, secret:) ⇒ B2
constructor
-
#upload_file(bucket, key, io_or_string, mime_type: nil, info: {}) ⇒ Object
Constructor Details
#initialize(key_id:, secret:) ⇒ B2
Returns a new instance of B2.
22
23
24
|
# File 'lib/b2.rb', line 22
def initialize(key_id: , secret: )
@connection = B2::Connection.new(key_id, secret)
end
|
Class Method Details
.decode(value) ⇒ Object
18
19
20
|
# File 'lib/b2.rb', line 18
def self.decode(value)
URI.decode_www_form_component(value, Encoding::UTF_8)
end
|
.encode(value) ⇒ Object
14
15
16
|
# File 'lib/b2.rb', line 14
def self.encode(value)
URI.encode_www_form_component(value.force_encoding(Encoding::UTF_8)).gsub("%2F", "/")
end
|
Instance Method Details
#account_id ⇒ Object
26
27
28
|
# File 'lib/b2.rb', line 26
def account_id
@connection.account_id
end
|
#bucket(name) ⇒ Object
34
35
36
37
|
# File 'lib/b2.rb', line 34
def bucket(name)
bs = @connection.post('/b2api/v2/b2_list_buckets', {accountId: account_id, bucketName: name})['buckets']
B2::Bucket.new(bs.first, @connection)
end
|
#buckets ⇒ Object
30
31
32
|
# File 'lib/b2.rb', line 30
def buckets
@connection.buckets
end
|
#delete(bucket, key) ⇒ Object
50
51
52
53
54
55
56
57
58
59
60
|
# File 'lib/b2.rb', line 50
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
99
100
101
|
# File 'lib/b2.rb', line 99
def download(bucket, key, to=nil, &block)
@connection.download(bucket, key, to, &block)
end
|
#download_to_file(bucket, key, filename) ⇒ Object
104
105
106
107
108
109
110
|
# File 'lib/b2.rb', line 104
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
39
40
41
42
43
44
45
46
47
48
|
# File 'lib/b2.rb', line 39
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
95
96
97
|
# File 'lib/b2.rb', line 95
def get_download_url(bucket, filename, **options)
@connection.get_download_url(bucket, filename, **options)
end
|
#get_upload_token(bucket) ⇒ Object
62
63
64
65
66
|
# File 'lib/b2.rb', line 62
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
# File 'lib/b2.rb', line 68
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.encode(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
|