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/upload_chunker.rb
Defined Under Namespace
Classes: Bucket, Connection, Error, File, NotFound, UploadChunker
Constant Summary
collapse
- VERSION =
'1.0.3'
Instance Method Summary
collapse
-
#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, expires_in: 3_600) ⇒ Object
-
#get_upload_token(bucket) ⇒ Object
-
#initialize(account_id:, application_key:) ⇒ B2
constructor
-
#lookup_bucket_id(name) ⇒ Object
-
#upload_file(bucket, key, io_or_string, mime_type: nil, info: {}) ⇒ Object
Constructor Details
#initialize(account_id:, application_key:) ⇒ B2
Returns a new instance of B2.
13
14
15
16
17
|
# File 'lib/b2.rb', line 13
def initialize(account_id:, application_key:)
@account_id = account_id
@connection = B2::Connection.new(account_id, application_key)
@buckets_cache = []
end
|
Instance Method Details
#buckets ⇒ Object
19
20
21
22
23
|
# File 'lib/b2.rb', line 19
def buckets
@connection.post('/b2api/v1/b2_list_buckets', {accountId: @account_id})['buckets'].map do |b|
B2::Bucket.new(b, @connection)
end
end
|
#delete(bucket, key) ⇒ Object
44
45
46
47
48
49
50
51
52
53
54
|
# File 'lib/b2.rb', line 44
def delete(bucket, key)
object = file(bucket, key)
if object
@connection.post('/b2api/v1/b2_delete_file_version', {
fileName: file.name,
fileId: file.id
})
else
false
end
end
|
#download(bucket, key, to = nil, &block) ⇒ Object
98
99
100
|
# File 'lib/b2.rb', line 98
def download(bucket, key, to=nil, &block)
@connection.download(bucket, key, to, &block)
end
|
#download_to_file(bucket, key, filename) ⇒ Object
103
104
105
106
107
108
109
|
# File 'lib/b2.rb', line 103
def download_to_file(bucket, key, filename)
file = File.open(filename, 'w')
download(bucket, key) do |chunk|
file << chunk
end
file.close
end
|
#file(bucket, key) ⇒ Object
33
34
35
36
37
38
39
40
41
42
|
# File 'lib/b2.rb', line 33
def file(bucket, key)
bucket_id = lookup_bucket_id(bucket)
file = @connection.post('/b2api/v1/b2_list_file_names', {
bucketId: bucket_id,
startFileName: key
})['files'].find {|f| f['fileName'] == key }
file ? B2::File.new(file.merge({'bucketId' => bucket_id})) : nil
end
|
#get_download_url(bucket, filename, expires_in: 3_600) ⇒ Object
89
90
91
92
93
94
95
96
|
# File 'lib/b2.rb', line 89
def get_download_url(bucket, filename, expires_in: 3_600)
response = @connection.post("/b2api/v1/b2_get_download_authorization", {
bucketId: lookup_bucket_id(bucket),
fileNamePrefix: filename,
validDurationInSeconds: expires_in
})
@connection.download_url + '/file/' + bucket + '/' + filename + "?Authorization=" + response['authorizationToken']
end
|
#get_upload_token(bucket) ⇒ Object
56
57
58
59
60
|
# File 'lib/b2.rb', line 56
def get_upload_token(bucket)
@connection.post("/b2api/v1/b2_get_upload_url", {
bucketId: lookup_bucket_id(bucket)
})
end
|
#lookup_bucket_id(name) ⇒ Object
25
26
27
28
29
30
31
|
# File 'lib/b2.rb', line 25
def lookup_bucket_id(name)
bucket = @buckets_cache.find{ |b| b.name == name }
return bucket.id if bucket
@buckets_cache = buckets
@buckets_cache.find{ |b| b.name == name }&.id
end
|
#upload_file(bucket, key, io_or_string, mime_type: nil, info: {}) ⇒ Object
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
# File 'lib/b2.rb', line 62
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
|