bkblz 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.rb +60 -47
- data/bin/bkblz +138 -0
- data/bkblz.gemspec +17 -14
- data/lib/bkblz.rb +4 -2
- data/lib/bkblz/all.rb +7 -0
- data/lib/bkblz/config.rb +1 -1
- data/lib/bkblz/task/all.rb +7 -0
- data/lib/bkblz/task/create_bucket.rb +22 -0
- data/lib/bkblz/task/list_buckets.rb +11 -0
- data/lib/bkblz/task/list_files.rb +22 -0
- data/lib/bkblz/task/task.rb +82 -0
- data/lib/bkblz/task/task_helpers.rb +19 -0
- data/lib/bkblz/task/upload_file.rb +25 -0
- data/lib/bkblz/v1/all.rb +1 -0
- data/lib/bkblz/v1/list_file_names.rb +35 -0
- data/lib/bkblz/v1/model_base.rb +6 -0
- data/lib/bkblz/v1/request.rb +13 -1
- data/lib/bkblz/version.rb +1 -1
- metadata +27 -3
- data/lib/all.rb +0 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5e5b80d7bec5fcf808b83fea5b07e508dc215e84
|
4
|
+
data.tar.gz: 50eba053e7feb3bf6ab847519ecac06d6a224d3a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 586eb75671ece61435dd0bbfa3f28ff01e18b74286e9549dd9692dc7409c7e26e49ee51bbab579e7a75a94dc268abc04871f1322af4af77426bf854af5c1316c
|
7
|
+
data.tar.gz: 7d992fc8b24164ad07da989f0c5cf36c9c8beba3135f339b7ea9833cfe13674039a4d57bbf136a9135ff5a69147195e2bad400202b0285cf388dd85912032f41
|
data/README.rb
CHANGED
@@ -1,19 +1,23 @@
|
|
1
|
+
# Run `ruby README.rb` for a working demo, but first add your
|
2
|
+
# application key and account id in the slots below.
|
3
|
+
|
4
|
+
# Or `gem install bkblz` to begin. After install try `bkblz -h` to use
|
5
|
+
# the CLI
|
6
|
+
|
1
7
|
$: << 'lib'
|
2
|
-
require '
|
8
|
+
require 'bkblz'
|
3
9
|
|
4
|
-
# Run `ruby README.rb` for a working demo, but first add your
|
5
|
-
# applicatoin key and account id in the slots below.
|
6
10
|
Bkblz.configure do |config_map|
|
7
11
|
config_map.merge!(
|
8
|
-
:application_key => "!!!
|
12
|
+
:application_key => "!!! API KEY !!!",
|
9
13
|
:account_id => "!!! ACCOUNT ID !!!",
|
10
14
|
:debug_http => false,
|
11
15
|
:log_level => :info # change this to :debug for more info
|
12
16
|
)
|
13
17
|
end
|
14
18
|
|
15
|
-
Bkblz.log.info do
|
16
|
-
#
|
19
|
+
Bkblz.log.info do <<-EOS
|
20
|
+
# The block above configures some defaults (including the
|
17
21
|
# logger, which is why this is after the configure block), see
|
18
22
|
# Bkblz::Config for details. This is where to set the account_id
|
19
23
|
# and application_key.
|
@@ -25,8 +29,8 @@ if Bkblz.config.account_id.match /!!!/
|
|
25
29
|
exit 1
|
26
30
|
end
|
27
31
|
|
28
|
-
Bkblz.log.info do
|
29
|
-
#
|
32
|
+
Bkblz.log.info do <<-EOS
|
33
|
+
# Using the config above, create an authorized session. All
|
30
34
|
# requests will run in the context of this session. See
|
31
35
|
# +Bkblz::V1::Session#authorize+.
|
32
36
|
EOS
|
@@ -34,20 +38,20 @@ end
|
|
34
38
|
Bkblz::V1::Session.authorize Bkblz.config do |api_session|
|
35
39
|
Bkblz.log.info "API session => #{api_session}"
|
36
40
|
|
37
|
-
Bkblz.log.info do
|
38
|
-
#
|
39
|
-
# we'll use that if it exists. All requests in a session are sent
|
40
|
-
# through the session so that the request object gets access to the
|
41
|
-
# auth credentials.
|
42
|
-
EOS
|
41
|
+
Bkblz.log.info do <<-EOS
|
42
|
+
# First try to find an existing bucket named my-test-bucket,
|
43
|
+
# we'll use that if it exists. All requests in a session are sent
|
44
|
+
# through the session so that the request object gets access to the
|
45
|
+
# auth credentials.
|
46
|
+
EOS
|
43
47
|
end
|
44
48
|
buckets = api_session.send(Bkblz::V1::ListBucketsRequest.new).buckets
|
45
49
|
bucket = buckets.select { |b| b.bucket_name == "my-test-bucket" }.first
|
46
50
|
Bkblz.log.info "bucket list => #{buckets}"
|
47
51
|
|
48
|
-
Bkblz.log.info do
|
49
|
-
#
|
50
|
-
EOS
|
52
|
+
Bkblz.log.info do <<-EOS
|
53
|
+
# Otherwise create a new my-test-bucket
|
54
|
+
EOS
|
51
55
|
end
|
52
56
|
unless bucket
|
53
57
|
bucket = Bkblz::V1::Model::Bucket.new \
|
@@ -55,13 +59,13 @@ EOS
|
|
55
59
|
:bucket_type => "allPrivate",
|
56
60
|
:account_id => api_session.account_id
|
57
61
|
|
58
|
-
Bkblz.log.info do
|
59
|
-
#
|
60
|
-
# models are just named wrappers with dynamic methods
|
61
|
-
# around the JSON responses provided back from the Bkblz
|
62
|
-
# API. See lib/bkblz/v1/models.rb for a list of defined API
|
63
|
-
# objects. See Bkblz::V1::Model::Base for how it work.
|
64
|
-
EOS
|
62
|
+
Bkblz.log.info do <<-EOS
|
63
|
+
# Pass a model to the CreateBucketRequest,
|
64
|
+
# models are just named wrappers with dynamic methods
|
65
|
+
# around the JSON responses provided back from the Bkblz
|
66
|
+
# API. See lib/bkblz/v1/models.rb for a list of defined API
|
67
|
+
# objects. See Bkblz::V1::Model::Base for how it work.
|
68
|
+
EOS
|
65
69
|
end
|
66
70
|
request = Bkblz::V1::CreateBucketRequest.new bucket
|
67
71
|
Bkblz.log.info "bucket model => #{bucket}"
|
@@ -73,19 +77,19 @@ EOS
|
|
73
77
|
Bkblz.log.info "created bucket => #{bucket.bucket_name}/#{bucket.bucket_id}"
|
74
78
|
end
|
75
79
|
|
76
|
-
Bkblz.log.info do
|
77
|
-
#
|
78
|
-
EOS
|
80
|
+
Bkblz.log.info do <<-EOS
|
81
|
+
# Uploading a file begins with getting a dynamic URL from the API.
|
82
|
+
EOS
|
79
83
|
end
|
80
84
|
upload_auth = api_session.send(
|
81
85
|
Bkblz::V1::GetUploadUrlRequest.new bucket.bucket_id).to_model
|
82
86
|
Bkblz.log.info "upload file URL => #{upload_auth.upload_url}"
|
83
87
|
|
84
88
|
|
85
|
-
Bkblz.log.info do
|
86
|
-
#
|
87
|
-
# Bkblz::V1::Model::UploadAuth) to upload some files.
|
88
|
-
EOS
|
89
|
+
Bkblz.log.info do <<-EOS
|
90
|
+
# Use the upload_auth model (a
|
91
|
+
# Bkblz::V1::Model::UploadAuth) to upload some files.
|
92
|
+
EOS
|
89
93
|
end
|
90
94
|
5.times do |i|
|
91
95
|
body = "some text #{i}"
|
@@ -98,21 +102,21 @@ EOS
|
|
98
102
|
Bkblz.log.info "uploaded file => #{upload_file_info.file_name}"
|
99
103
|
end
|
100
104
|
|
101
|
-
Bkblz.log.info do
|
102
|
-
#
|
103
|
-
# metadata from the first 2 files in the bucket.
|
104
|
-
EOS
|
105
|
+
Bkblz.log.info do <<-EOS
|
106
|
+
# We uploaded 5 files above, here we'll read back out
|
107
|
+
# metadata from the first 2 files in the bucket.
|
108
|
+
EOS
|
105
109
|
end
|
106
110
|
list_files_response = api_session.send(
|
107
111
|
Bkblz::V1::ListFileVersionsRequest.new bucket, 2)
|
108
112
|
bucket_files_info = list_files_response.files
|
109
113
|
Bkblz.log.info "first 2 files => #{bucket_files_info.map(&:file_name).join "\n"}"
|
110
114
|
|
111
|
-
Bkblz.log.info do
|
112
|
-
#
|
113
|
-
# Bkblz::Api::PaginatedResponse. Use its +has_more?+ and
|
114
|
-
# +next_request+ methods to page through more results.
|
115
|
-
EOS
|
115
|
+
Bkblz.log.info do <<-EOS
|
116
|
+
# The response object returned object is a
|
117
|
+
# Bkblz::Api::PaginatedResponse. Use its +has_more?+ and
|
118
|
+
# +next_request+ methods to page through more results.
|
119
|
+
EOS
|
116
120
|
end
|
117
121
|
while list_files_response.has_more?
|
118
122
|
list_files_response = api_session.send list_files_response.next_request 100
|
@@ -120,10 +124,19 @@ EOS
|
|
120
124
|
Bkblz.log.info "next N files => #{list_files_response.files.map(&:file_name).join "\n"}"
|
121
125
|
end
|
122
126
|
|
123
|
-
Bkblz.log.info do
|
124
|
-
#
|
125
|
-
|
126
|
-
|
127
|
+
Bkblz.log.info do <<-EOS
|
128
|
+
# Files can also be listed by name.
|
129
|
+
EOS
|
130
|
+
end
|
131
|
+
list_files_response = api_session.send(
|
132
|
+
Bkblz::V1::ListFileNamesRequest.new bucket, 10)
|
133
|
+
bucket_files_info = list_files_response.files
|
134
|
+
Bkblz.log.info "files by name => #{bucket_files_info.map(&:file_name).join "\n"}"
|
135
|
+
|
136
|
+
Bkblz.log.info do <<-EOS
|
137
|
+
# Delete all the files in the bucket that we added. This is
|
138
|
+
# a service requirement to deleting a bucket.
|
139
|
+
EOS
|
127
140
|
end
|
128
141
|
bucket_files_info.each do |file_info|
|
129
142
|
request = Bkblz::V1::DeleteFileVersionRequest.new file_info
|
@@ -131,9 +144,9 @@ EOS
|
|
131
144
|
Bkblz.log.info "deleted file => #{delete_file_version_response.to_model.file_name}"
|
132
145
|
end
|
133
146
|
|
134
|
-
Bkblz.log.info do
|
135
|
-
#
|
136
|
-
EOS
|
147
|
+
Bkblz.log.info do <<-EOS
|
148
|
+
# Finally, delete the bucket.
|
149
|
+
EOS
|
137
150
|
end
|
138
151
|
request = Bkblz::V1::DeleteBucketRequest.new bucket
|
139
152
|
delete_bucket_response = api_session.send request
|
data/bin/bkblz
ADDED
@@ -0,0 +1,138 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
$: << "lib"
|
3
|
+
require "bkblz"
|
4
|
+
require "thor"
|
5
|
+
require "yaml"
|
6
|
+
|
7
|
+
module Bkblz
|
8
|
+
|
9
|
+
module Helper
|
10
|
+
def parse_opts
|
11
|
+
if options[:config]
|
12
|
+
f = options[:config]
|
13
|
+
raise "unreadable config file #{f}" unless ::File.readable? f
|
14
|
+
Bkblz.configure do |map|
|
15
|
+
map.merge YAML.load(::File.read f)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def run_task(&block)
|
21
|
+
begin
|
22
|
+
yield
|
23
|
+
rescue Bkblz::Task::MissingBucketError => e
|
24
|
+
STDERR.puts "Unknown bucket: #{e.message}"
|
25
|
+
exit 1
|
26
|
+
rescue Bkblz::V1::UnauthorizedRequestError => e
|
27
|
+
msg = if options[:config]
|
28
|
+
"bad account id or application key, check your auth credentials"
|
29
|
+
else
|
30
|
+
"use '-c path/to/config.yml' to supply credentials"
|
31
|
+
end
|
32
|
+
STDERR.puts "Unauthorized request: #{msg}"
|
33
|
+
exit 1
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def print_header(header)
|
38
|
+
puts ""
|
39
|
+
puts "*** #{header} ***"
|
40
|
+
puts ""
|
41
|
+
end
|
42
|
+
|
43
|
+
def print_model(model)
|
44
|
+
puts model.to_yaml
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
class Bucket < Thor
|
49
|
+
include Helper
|
50
|
+
|
51
|
+
desc "bucket list", "lists all of your buckets"
|
52
|
+
def list
|
53
|
+
parse_opts
|
54
|
+
|
55
|
+
buckets = run_task do
|
56
|
+
Bkblz::Task::ListBuckets.run Bkblz.config
|
57
|
+
end
|
58
|
+
|
59
|
+
print_header "Buckets"
|
60
|
+
buckets.each do |bucket|
|
61
|
+
puts bucket.bucket_name
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
desc "bucket create <bucket_name>", "creates a new bucket"
|
66
|
+
def create(bucket_name)
|
67
|
+
parse_opts
|
68
|
+
|
69
|
+
bucket = run_task do
|
70
|
+
Bkblz::Task::CreateBucket.run Bkblz.config, :bucket_name => bucket_name
|
71
|
+
end
|
72
|
+
print_header "New Bucket"
|
73
|
+
print_model bucket
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
class File < Thor
|
78
|
+
include Helper
|
79
|
+
|
80
|
+
desc "file upload <bucket_name> <local_file_path>", "upload a local file to B2"
|
81
|
+
def upload(bucket_name, local_file_path)
|
82
|
+
parse_opts
|
83
|
+
|
84
|
+
file_info = run_task do
|
85
|
+
Bkblz::Task::UploadFile.run Bkblz.config, {
|
86
|
+
:bucket_handle => bucket_name,
|
87
|
+
:handle_type => :bucket_name,
|
88
|
+
:file_path => local_file_path
|
89
|
+
}
|
90
|
+
end
|
91
|
+
|
92
|
+
print_header "File Info"
|
93
|
+
print_model file_info
|
94
|
+
end
|
95
|
+
|
96
|
+
desc "file list <bucket_name>", "lists files in a bucket"
|
97
|
+
option :start_at, :desc => "file name to start listing from"
|
98
|
+
option :limit, {
|
99
|
+
:desc => "limit of files to list per request",
|
100
|
+
:default => 100,
|
101
|
+
:type => :numeric
|
102
|
+
}
|
103
|
+
option :all, :desc => "lists all file versions, cannot be used with --start-at"
|
104
|
+
def list(bucket_name)
|
105
|
+
parse_opts
|
106
|
+
|
107
|
+
task = Bkblz::Task::ListFiles.new Bkblz.config
|
108
|
+
run_task do
|
109
|
+
|
110
|
+
paged_response = task.run({
|
111
|
+
:start_at => options[:start_at],
|
112
|
+
:limit => options[:limit],
|
113
|
+
:all_versions => options[:all],
|
114
|
+
:bucket_name => bucket_name
|
115
|
+
})
|
116
|
+
|
117
|
+
print_header "Files"
|
118
|
+
paged_response.files.each do |f|
|
119
|
+
puts f.file_name
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
class Cli < Thor
|
126
|
+
include Helper
|
127
|
+
|
128
|
+
class_option :config, :desc => "path to a yml config file", :aliases => ["-c"]
|
129
|
+
|
130
|
+
desc "bucket SUBCOMMAND ...ARGS", "manage backblaze buckets"
|
131
|
+
subcommand "bucket", Bucket
|
132
|
+
|
133
|
+
desc "file SUBCOMMAND ...ARGS", "manage backblaze files"
|
134
|
+
subcommand "file", File
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
Bkblz::Cli.start ARGV
|
data/bkblz.gemspec
CHANGED
@@ -3,23 +3,26 @@ lib = File.expand_path('../lib', __FILE__)
|
|
3
3
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
4
|
require 'bkblz/version'
|
5
5
|
|
6
|
-
Gem::Specification.new do |
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = "bkblz"
|
8
|
+
s.version = Bkblz::VERSION
|
9
|
+
s.authors = ["Erick Johnson"]
|
10
|
+
s.email = ["erick@vos.io"]
|
11
11
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
12
|
+
s.summary = "Bkblz GEM for the Backblaze B2 API. https://www.backblaze.com/b2/docs/"
|
13
|
+
s.description = s.description
|
14
|
+
s.homepage = "https://github.com/erickj/bkblz"
|
15
|
+
s.license = "MIT"
|
16
16
|
|
17
|
-
|
17
|
+
s.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
18
|
f.match(%r{^(test|spec|features)/})
|
19
19
|
end
|
20
|
-
|
20
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
|
21
|
+
s.require_paths = ["lib"]
|
21
22
|
|
22
|
-
|
23
|
-
|
24
|
-
|
23
|
+
s.add_dependency "thor", "~> 0.19"
|
24
|
+
|
25
|
+
s.add_development_dependency "bundler", "~> 1.13"
|
26
|
+
s.add_development_dependency "rake", "~> 10.0"
|
27
|
+
s.add_development_dependency "rspec", "~> 3.0"
|
25
28
|
end
|
data/lib/bkblz.rb
CHANGED
data/lib/bkblz/all.rb
ADDED
data/lib/bkblz/config.rb
CHANGED
@@ -0,0 +1,22 @@
|
|
1
|
+
module Bkblz
|
2
|
+
module Task
|
3
|
+
class CreateBucket < BaseTask
|
4
|
+
|
5
|
+
DuplicateBucketError = Class.new Bkblz::BaseError
|
6
|
+
|
7
|
+
task_param :bucket_name, :required => true
|
8
|
+
task_param :bucket_type
|
9
|
+
|
10
|
+
def run_internal(session, params)
|
11
|
+
bucket_fields = {
|
12
|
+
:account_id => session.account_id,
|
13
|
+
:bucket_type => "allPrivate"
|
14
|
+
}.merge params
|
15
|
+
bucket = Bkblz::V1::Model::Bucket.new bucket_fields
|
16
|
+
|
17
|
+
session.send(Bkblz::V1::CreateBucketRequest.new bucket).to_model
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Bkblz
|
2
|
+
module Task
|
3
|
+
class ListFiles < BaseTask
|
4
|
+
|
5
|
+
task_param :start_at
|
6
|
+
task_param :limit, :default => 100
|
7
|
+
task_param :all_versions
|
8
|
+
task_param :bucket_name, :required => true
|
9
|
+
|
10
|
+
def run_internal(session, params)
|
11
|
+
bucket = find_bucket_by_name session, params[:bucket_name]
|
12
|
+
|
13
|
+
if params[:all_versions]
|
14
|
+
session.send Bkblz::V1::ListFileVersionsRequest.new bucket, params[:limit]
|
15
|
+
else
|
16
|
+
session.send Bkblz::V1::ListFileNamesRequest.new(
|
17
|
+
bucket, params[:limit], params[:start_at])
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
module Bkblz
|
2
|
+
module Task
|
3
|
+
|
4
|
+
TaskParamError = Class.new Bkblz::BaseError
|
5
|
+
|
6
|
+
module ClassMethods
|
7
|
+
PARAM_SPEC = {
|
8
|
+
:name => nil,
|
9
|
+
:required => false,
|
10
|
+
:default => nil,
|
11
|
+
:one_of => nil
|
12
|
+
}.freeze
|
13
|
+
|
14
|
+
def run(config, **params)
|
15
|
+
task = self.new config
|
16
|
+
task.run **params
|
17
|
+
end
|
18
|
+
|
19
|
+
def task_param(name, **param_spec)
|
20
|
+
if param_spec[:one_of] && !param_spec[:one_of].is_a?(Enumerable)
|
21
|
+
raise TaskParamError, "#{name}: :one_of must be enumerable"
|
22
|
+
end
|
23
|
+
task_params << PARAM_SPEC.merge(param_spec.merge :name => name)
|
24
|
+
end
|
25
|
+
|
26
|
+
def task_params
|
27
|
+
@task_params ||= []
|
28
|
+
end
|
29
|
+
|
30
|
+
def check_params(params)
|
31
|
+
task_params.each do |spec|
|
32
|
+
name spec[:name]
|
33
|
+
value = params[name]
|
34
|
+
|
35
|
+
if spec[:default] && value.nil?
|
36
|
+
params[name] = spec[:default]
|
37
|
+
end
|
38
|
+
|
39
|
+
check_required name, value, spec
|
40
|
+
check_one_of name, value, spec
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def check_required(name, value, spec)
|
45
|
+
raise TaskParamError, "[#{name}:required]" if spec[:required] && value.nil?
|
46
|
+
end
|
47
|
+
|
48
|
+
def check_one_of(name, value, spec)
|
49
|
+
return unless spec[:one_of]
|
50
|
+
unless spec[:one_of].include? value
|
51
|
+
raise TaskParamError, "[#{name}:one_of] invalid values: #{value}"
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
class BaseTask
|
57
|
+
extend ClassMethods
|
58
|
+
include TaskHelpers
|
59
|
+
|
60
|
+
attr_reader :config, :result
|
61
|
+
|
62
|
+
def initialize(config)
|
63
|
+
@config = config
|
64
|
+
@result = nil
|
65
|
+
end
|
66
|
+
|
67
|
+
def run(task_params)
|
68
|
+
BaseTask.check_params task_params
|
69
|
+
|
70
|
+
Bkblz::V1::Session.authorize config do |session|
|
71
|
+
@result = run_internal session, task_params
|
72
|
+
end
|
73
|
+
result
|
74
|
+
end
|
75
|
+
|
76
|
+
protected
|
77
|
+
def run_internal(session, task_params)
|
78
|
+
raise 'not implemented'
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Bkblz
|
2
|
+
module Task
|
3
|
+
|
4
|
+
MissingBucketError = Class.new Bkblz::BaseError
|
5
|
+
|
6
|
+
module TaskHelpers
|
7
|
+
|
8
|
+
def find_bucket_by_name(session, bucket_name)
|
9
|
+
buckets = session.send(Bkblz::V1::ListBucketsRequest.new).buckets
|
10
|
+
bucket = buckets.find do |bucket|
|
11
|
+
bucket.bucket_name == bucket_name
|
12
|
+
end
|
13
|
+
raise MissingBucketError, bucket_name unless bucket
|
14
|
+
bucket
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Bkblz
|
2
|
+
module Task
|
3
|
+
class UploadFile < BaseTask
|
4
|
+
|
5
|
+
task_param :bucket_name, :required => true
|
6
|
+
task_param :file_path, :required => true
|
7
|
+
|
8
|
+
def run_internal(session, params)
|
9
|
+
f = ::File.new(params[:file_path], "r")
|
10
|
+
|
11
|
+
bucket = find_bucket_by_name session, params[:bucket_name]
|
12
|
+
upload_auth = session.send(
|
13
|
+
Bkblz::V1::GetUploadUrlRequest.new bucket.bucket_id).to_model
|
14
|
+
|
15
|
+
file_name = ::File.basename f.path
|
16
|
+
file_body = f.read
|
17
|
+
mtime_millis = f.mtime.to_i * 1000
|
18
|
+
|
19
|
+
upload_file_info = session.send(
|
20
|
+
Bkblz::V1::UploadFileRequest.new upload_auth, file_body, file_name,
|
21
|
+
nil, mtime_millis).to_model
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
data/lib/bkblz/v1/all.rb
CHANGED
@@ -0,0 +1,35 @@
|
|
1
|
+
module Bkblz
|
2
|
+
module V1
|
3
|
+
|
4
|
+
class ListFileNamesResponse < PaginatedResponse
|
5
|
+
response_accessor :files, Model::FileInfo
|
6
|
+
pagination_accessors :next_file_name
|
7
|
+
|
8
|
+
def build_next_request(limit)
|
9
|
+
bucket = original_request.bucket
|
10
|
+
limit ||= files.size
|
11
|
+
ListFileNamesRequest.new bucket, limit, next_file_name
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
class ListFileNamesRequest < Request
|
16
|
+
|
17
|
+
response_class ListFileNamesResponse
|
18
|
+
url_suffix "/b2api/v1/b2_list_file_names"
|
19
|
+
|
20
|
+
attr_reader :bucket
|
21
|
+
|
22
|
+
def initialize(bucket, max_file_count=1000, start_file_name=nil)
|
23
|
+
@bucket = bucket
|
24
|
+
@body = {}
|
25
|
+
@body[:bucket_id] = bucket.bucket_id
|
26
|
+
@body[:max_file_count] = max_file_count
|
27
|
+
@body[:start_file_name] = start_file_name if start_file_name
|
28
|
+
end
|
29
|
+
|
30
|
+
def build_request(session)
|
31
|
+
session.create_post url(session), @body
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
data/lib/bkblz/v1/model_base.rb
CHANGED
data/lib/bkblz/v1/request.rb
CHANGED
@@ -6,11 +6,23 @@ module Bkblz
|
|
6
6
|
TooManyRedirectError = Class.new Bkblz::BaseError
|
7
7
|
|
8
8
|
class RequestError < Bkblz::BaseError
|
9
|
+
|
10
|
+
def self.create(error_response)
|
11
|
+
case error_response.to_model.status
|
12
|
+
when 401
|
13
|
+
UnauthorizedRequestError.new error_response
|
14
|
+
else
|
15
|
+
RequestError.new error_response
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
9
19
|
def initialize(error_response)
|
10
20
|
super error_response.message
|
11
21
|
end
|
12
22
|
end
|
13
23
|
|
24
|
+
UnauthorizedRequestError = Class.new RequestError
|
25
|
+
|
14
26
|
class Request
|
15
27
|
|
16
28
|
class << self
|
@@ -44,7 +56,7 @@ module Bkblz
|
|
44
56
|
def build_response(response)
|
45
57
|
unless response.kind_of? Net::HTTPSuccess
|
46
58
|
error_response = ErrorResponse.new response, self
|
47
|
-
raise RequestError.
|
59
|
+
raise RequestError.create error_response
|
48
60
|
end
|
49
61
|
Bkblz.log.debug { "#build_response => #{response}" }
|
50
62
|
|
data/lib/bkblz/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bkblz
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Erick Johnson
|
@@ -10,6 +10,20 @@ bindir: bin
|
|
10
10
|
cert_chain: []
|
11
11
|
date: 2016-11-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: thor
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0.19'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0.19'
|
13
27
|
- !ruby/object:Gem::Dependency
|
14
28
|
name: bundler
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -55,7 +69,8 @@ dependencies:
|
|
55
69
|
description: ''
|
56
70
|
email:
|
57
71
|
- erick@vos.io
|
58
|
-
executables:
|
72
|
+
executables:
|
73
|
+
- bkblz
|
59
74
|
extensions: []
|
60
75
|
extra_rdoc_files: []
|
61
76
|
files:
|
@@ -66,13 +81,21 @@ files:
|
|
66
81
|
- LICENSE.txt
|
67
82
|
- README.rb
|
68
83
|
- Rakefile
|
84
|
+
- bin/bkblz
|
69
85
|
- bkblz.gemspec
|
70
|
-
- lib/all.rb
|
71
86
|
- lib/bkblz.rb
|
87
|
+
- lib/bkblz/all.rb
|
72
88
|
- lib/bkblz/config.rb
|
73
89
|
- lib/bkblz/core_ext.rb
|
74
90
|
- lib/bkblz/logger.rb
|
75
91
|
- lib/bkblz/map_key_formatter.rb
|
92
|
+
- lib/bkblz/task/all.rb
|
93
|
+
- lib/bkblz/task/create_bucket.rb
|
94
|
+
- lib/bkblz/task/list_buckets.rb
|
95
|
+
- lib/bkblz/task/list_files.rb
|
96
|
+
- lib/bkblz/task/task.rb
|
97
|
+
- lib/bkblz/task/task_helpers.rb
|
98
|
+
- lib/bkblz/task/upload_file.rb
|
76
99
|
- lib/bkblz/v1/all.rb
|
77
100
|
- lib/bkblz/v1/authorize_account.rb
|
78
101
|
- lib/bkblz/v1/create_bucket.rb
|
@@ -81,6 +104,7 @@ files:
|
|
81
104
|
- lib/bkblz/v1/error_response.rb
|
82
105
|
- lib/bkblz/v1/get_upload_url.rb
|
83
106
|
- lib/bkblz/v1/list_buckets.rb
|
107
|
+
- lib/bkblz/v1/list_file_names.rb
|
84
108
|
- lib/bkblz/v1/list_file_versions.rb
|
85
109
|
- lib/bkblz/v1/model_base.rb
|
86
110
|
- lib/bkblz/v1/models.rb
|