bos_client 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +7 -3
- data/CHANGELOG +1 -0
- data/README.md +2 -5
- data/Rakefile +1 -2
- data/bin/console +3 -3
- data/bos_client.gemspec +15 -14
- data/lib/bos_client/auth.rb +28 -25
- data/lib/bos_client/bucket.rb +22 -23
- data/lib/bos_client/config.rb +14 -19
- data/lib/bos_client/error.rb +5 -6
- data/lib/bos_client/helper.rb +5 -6
- data/lib/bos_client/object.rb +29 -20
- data/lib/bos_client/request.rb +36 -41
- data/lib/bos_client/service.rb +1 -1
- data/lib/bos_client/version.rb +1 -1
- data/lib/bos_client.rb +2 -3
- metadata +7 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a51335e0701f20bd4f64014d6be8916f1f95dddd
|
4
|
+
data.tar.gz: 97b474f581a0073d8dfcae645fb84647e258be49
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e7f08a6fce9f9c7cc7a4ecc2a4036b63b5f754aa486d60f86c7348c9d49e2b9390011ee09d04d52b491ca666fe57df20808514d08856c63cfbfd1070d341fccf
|
7
|
+
data.tar.gz: bd60fed016b190e4e00847da1dcc1829c6c3746e5c7d37b18cf4c52e5b476a2e6c04a390799694ed94b27f8fb97809145fd30a81f0cadbcdc6efa911de6981f7
|
data/.travis.yml
CHANGED
@@ -1,7 +1,11 @@
|
|
1
1
|
language: ruby
|
2
2
|
rvm:
|
3
|
-
|
3
|
+
- 2.1.2
|
4
4
|
before_script:
|
5
|
-
|
5
|
+
- bundle install
|
6
6
|
script:
|
7
|
-
|
7
|
+
- bundle exec rspec
|
8
|
+
notifications:
|
9
|
+
slack:
|
10
|
+
secure: RL3pJyyctMFuoTHleabKFXnyRXD6e4rjpj4Mx6juA5Gx+Fy1bj3gl+kmYPJlh81XNGjA3+t0FknswNe0E3eFCfHVyrQ0G+AkOFIwSJ1FNmjKnOi8YfFv6V+ACIft5oucGbe+k0MgZXj0/u+NVqrnePCxFI3rP3vBzr3Ou1n7J7r7SYGrePxpsYVCcmdpUN8TgHjKQRr5TcUPaBTPQcGHZ65hOiI7nsWM35452MTi7+NFql+dhflOCJuiajN96hojaVkzGWZi5bp9A7e4LipdjgmNvpwGkseiksIfZd2pq6Ho20DeEp7mEweyS7wmb4p675g5Aal22jTZkNbZBGFzr55+tqIYSp92iby9InKmPyniO8ivyokO2XdzJkJikzUjXlhfjWKbuKf9hGC6bLHzzeSs1ZS7Ekiyr9kXsnA0xIEPzpc2AjSgS+K6vhQeyon3tnFAEKO9MJIjvgKq/jgOSsV6wc/AkHpFK3nZrjVVBAtpkPZeuvG+q2Gmo2p3ip3i2uqWEFkpAktcWmIt/p9dzE1OR5G+Hf6CNd+t5wLd9T6BtAYT5P/tpaATQ5VrlhnqgJZbQI6umXp2nNQTTA7cgn1ENoQA6R5D+q5v9HE3EPY6cvTZJ+Rs5UjVljsb1pJTs92RbppwLJUm2kMvOpOqRng9qJS1OD/KNFqXBH2TS6o=
|
11
|
+
email: false
|
data/CHANGELOG
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
Version 0.1.1 Added save bucket method
|
data/README.md
CHANGED
@@ -1,11 +1,10 @@
|
|
1
|
-
# BosClient
|
2
|
-
|
1
|
+
# BosClient [![Gem Version](https://badge.fury.io/rb/bos_client.svg)](https://badge.fury.io/rb/bos_client) ![](https://travis-ci.org/fcce/bos.svg?branch=master)
|
3
2
|
## Installation
|
4
3
|
|
5
4
|
Add this line to your application's Gemfile:
|
6
5
|
|
7
6
|
```ruby
|
8
|
-
gem '
|
7
|
+
gem 'bos_client'
|
9
8
|
```
|
10
9
|
|
11
10
|
And then execute:
|
@@ -16,8 +15,6 @@ Or install it yourself as:
|
|
16
15
|
|
17
16
|
$ gem install bos_client
|
18
17
|
|
19
|
-
## Usage
|
20
|
-
|
21
18
|
|
22
19
|
## Development
|
23
20
|
|
data/Rakefile
CHANGED
@@ -1,2 +1 @@
|
|
1
|
-
require
|
2
|
-
|
1
|
+
require 'bundler/gem_tasks'
|
data/bin/console
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'bos_client'
|
5
5
|
|
6
6
|
# You can add fixtures and/or initialization code here to make experimenting
|
7
7
|
# with your gem easier. You can also use a different console, if you like.
|
@@ -10,5 +10,5 @@ require "bos_client"
|
|
10
10
|
# require "pry"
|
11
11
|
# Pry.start
|
12
12
|
|
13
|
-
require
|
13
|
+
require 'irb'
|
14
14
|
IRB.start
|
data/bos_client.gemspec
CHANGED
@@ -4,25 +4,26 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
4
|
require 'bos_client/version'
|
5
5
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
|
-
spec.name =
|
7
|
+
spec.name = 'bos_client'
|
8
8
|
spec.version = BosClient::VERSION
|
9
|
-
spec.authors = [
|
10
|
-
spec.email = [
|
9
|
+
spec.authors = ['Feng Ce']
|
10
|
+
spec.email = ['kalelfc@gmail.com']
|
11
11
|
|
12
|
-
spec.summary =
|
13
|
-
spec.description =
|
14
|
-
|
15
|
-
spec.
|
12
|
+
spec.summary = 'Baidu Object Storage Unofficial Ruby SDK'
|
13
|
+
spec.description = 'Library for accessing BOS objects and buckets through
|
14
|
+
REST API'
|
15
|
+
spec.homepage = 'http://github.com/fcce/bos'
|
16
|
+
spec.license = 'MIT'
|
16
17
|
|
17
18
|
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
19
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
-
spec.require_paths = [
|
20
|
+
spec.require_paths = ['lib']
|
20
21
|
|
21
|
-
spec.required_ruby_version =
|
22
|
+
spec.required_ruby_version = '>= 2.0'
|
22
23
|
|
23
|
-
spec.add_runtime_dependency
|
24
|
-
spec.add_development_dependency
|
25
|
-
spec.add_development_dependency
|
26
|
-
spec.add_development_dependency
|
27
|
-
spec.add_development_dependency
|
24
|
+
spec.add_runtime_dependency 'typhoeus', '~> 1.1'
|
25
|
+
spec.add_development_dependency 'bundler', '~> 1.6'
|
26
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
27
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
28
|
+
spec.add_development_dependency 'pry', '~> 0.1'
|
28
29
|
end
|
data/lib/bos_client/auth.rb
CHANGED
@@ -4,38 +4,34 @@ require 'typhoeus'
|
|
4
4
|
require 'uri'
|
5
5
|
module BosClient
|
6
6
|
class Authable
|
7
|
-
|
8
|
-
def self.authorize_request request
|
7
|
+
def self.authorize_request(request)
|
9
8
|
default_headers = get_default_headers request
|
10
9
|
request.options[:headers].merge! default_headers
|
11
10
|
authorization = sign(request)
|
12
|
-
request.options[:headers]
|
11
|
+
request.options[:headers]['Authorization'] = authorization
|
13
12
|
request
|
14
13
|
end
|
15
14
|
|
16
|
-
private
|
17
15
|
class << self
|
18
|
-
def get_canonical_time
|
19
|
-
Time.at(t).utc.strftime(
|
16
|
+
def get_canonical_time(t = Time.now.to_i)
|
17
|
+
Time.at(t).utc.strftime('%FT%TZ')
|
20
18
|
end
|
21
19
|
|
22
|
-
def get_default_headers
|
20
|
+
def get_default_headers(request)
|
23
21
|
{
|
24
|
-
|
25
|
-
|
26
|
-
|
22
|
+
'content-type' => 'text/plain',
|
23
|
+
'x-bce-date' => get_canonical_time,
|
24
|
+
'content-length' => (request.options[:body] || '').length
|
27
25
|
}
|
28
|
-
# todo PUT 方法计算的签名错误,content-length 的问题,暂时处理。
|
29
|
-
# headers.merge!({"content-length" => 0}) if request.options[:method] == :put
|
30
26
|
end
|
31
27
|
|
32
|
-
def get_canonical_uri
|
33
|
-
uri
|
28
|
+
def get_canonical_uri(request)
|
29
|
+
uri = URI(request.base_url)
|
34
30
|
url_path = URI.encode(uri.path)
|
35
31
|
url_path == '' ? '/' : url_path
|
36
32
|
end
|
37
33
|
|
38
|
-
def get_canonical_query_string
|
34
|
+
def get_canonical_query_string(request)
|
39
35
|
params = request.options[:params]
|
40
36
|
params = params.map do |k, v|
|
41
37
|
"#{URI.encode(k.to_s)}=#{encode_slash(URI.encode(v.to_s))}"
|
@@ -43,10 +39,13 @@ module BosClient
|
|
43
39
|
params
|
44
40
|
end
|
45
41
|
|
46
|
-
def get_canonical_headers
|
47
|
-
headers_to_sign_keys = [
|
42
|
+
def get_canonical_headers(request)
|
43
|
+
headers_to_sign_keys = ['host',
|
44
|
+
'content-md5',
|
45
|
+
'content-length',
|
46
|
+
'content-type']
|
48
47
|
headers_to_sign = []
|
49
|
-
|
48
|
+
request.options[:headers].each do |k, v|
|
50
49
|
if headers_to_sign_keys.include?(k) || k.start_with?('x-bce')
|
51
50
|
headers_to_sign << "#{encode(k.to_s.downcase)}:#{encode(v.to_s)}"
|
52
51
|
end
|
@@ -54,21 +53,22 @@ module BosClient
|
|
54
53
|
headers_to_sign.compact.sort.join("\n")
|
55
54
|
end
|
56
55
|
|
57
|
-
def get_http_method
|
56
|
+
def get_http_method(request)
|
58
57
|
(request.options[:method] || 'get').upcase
|
59
58
|
end
|
60
59
|
|
61
|
-
def encode
|
60
|
+
def encode(string)
|
62
61
|
URI.encode(string, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
|
63
62
|
end
|
64
63
|
|
65
|
-
def encode_slash
|
66
|
-
str.gsub(/\//,'%2F')
|
64
|
+
def encode_slash(str)
|
65
|
+
# str.gsub(/\//, '%2F')
|
66
|
+
str.gsub(%r{/}, '%2F')
|
67
67
|
end
|
68
68
|
|
69
|
-
def sign
|
69
|
+
def sign(request)
|
70
70
|
digest = OpenSSL::Digest.new('sha256')
|
71
|
-
sign_key_prefix = "bce-auth-v1/#{BosClient.access_key_id}/#{get_canonical_time
|
71
|
+
sign_key_prefix = "bce-auth-v1/#{BosClient.access_key_id}/#{get_canonical_time}/#{BosClient.expiration_in_seconds}"
|
72
72
|
sign_key = OpenSSL::HMAC.hexdigest digest, BosClient.secret_access_key, sign_key_prefix
|
73
73
|
|
74
74
|
http_method = get_http_method request
|
@@ -76,7 +76,10 @@ module BosClient
|
|
76
76
|
canonical_uri = get_canonical_uri request
|
77
77
|
canonical_query_string = get_canonical_query_string request
|
78
78
|
canonical_headers = get_canonical_headers request
|
79
|
-
string_to_sign = [http_method,
|
79
|
+
string_to_sign = [http_method,
|
80
|
+
canonical_uri,
|
81
|
+
canonical_query_string,
|
82
|
+
canonical_headers].join("\n")
|
80
83
|
sign_result = OpenSSL::HMAC.hexdigest digest, sign_key, string_to_sign
|
81
84
|
"#{sign_key_prefix}//#{sign_result}"
|
82
85
|
end
|
data/lib/bos_client/bucket.rb
CHANGED
@@ -16,13 +16,11 @@ module BosClient
|
|
16
16
|
response[:result]
|
17
17
|
end
|
18
18
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
# request.run
|
25
|
-
# end
|
19
|
+
def save
|
20
|
+
request = BosClient::Request.new @bucket_host, method: :put
|
21
|
+
response = request.run
|
22
|
+
response[:result]
|
23
|
+
end
|
26
24
|
|
27
25
|
def destory
|
28
26
|
request = BosClient::Request.new @bucket_host, method: :delete
|
@@ -31,41 +29,42 @@ module BosClient
|
|
31
29
|
end
|
32
30
|
|
33
31
|
## objects
|
34
|
-
def objects
|
35
|
-
response = list_objects options.merge(
|
32
|
+
def objects(options = {})
|
33
|
+
response = list_objects options.merge(delimiter: '/')
|
36
34
|
objs = response[:data][:contents]
|
37
|
-
objs.map { |obj| BosClient::Object.new ({bucket:self}.merge(obj)) }
|
35
|
+
objs.map { |obj| BosClient::Object.new ({bucket: self}.merge(obj)) }
|
38
36
|
end
|
39
37
|
|
40
|
-
def dirs
|
38
|
+
def dirs(options = {})
|
41
39
|
response = list_objects options
|
42
40
|
data = response[:data][:contents]
|
43
|
-
data = data.find_all{|e| is_dir?(e[:key]) }
|
41
|
+
data = data.find_all { |e| is_dir?(e[:key]) }
|
44
42
|
data.map { |e| e[:key] }
|
45
43
|
end
|
46
44
|
|
47
|
-
def dir_objects
|
48
|
-
response = list_objects options.merge(
|
45
|
+
def dir_objects(prefix, options = {})
|
46
|
+
response = list_objects options.merge(delimiter: '/', prefix: prefix)
|
49
47
|
data = response[:data][:contents]
|
50
|
-
objs = data.find_all{|e| !is_dir?(e[:key]) }
|
51
|
-
objs.map { |obj| BosClient::Object.new ({bucket:self}.merge(obj)) }
|
48
|
+
objs = data.find_all { |e| !is_dir?(e[:key]) }
|
49
|
+
objs.map { |obj| BosClient::Object.new ({bucket: self}.merge(obj)) }
|
52
50
|
end
|
53
51
|
|
54
52
|
private
|
55
|
-
|
53
|
+
|
54
|
+
def is_dir?(name)
|
56
55
|
name.end_with?('/')
|
57
56
|
end
|
58
57
|
|
59
|
-
def list_objects
|
58
|
+
def list_objects(options = {})
|
60
59
|
prefix = options.fetch(:prefix, nil)
|
61
60
|
max_keys = options.fetch(:max_keys, 1000)
|
62
61
|
marker = options.fetch(:marker, nil)
|
63
62
|
delimiter = options.fetch(:delimiter, nil)
|
64
|
-
params = {maxKeys: max_keys}
|
65
|
-
params
|
66
|
-
params
|
67
|
-
params
|
68
|
-
request = BosClient::Request.new @bucket_host, method: :get, params:params
|
63
|
+
params = { maxKeys: max_keys }
|
64
|
+
params[:prefix] = prefix unless prefix.nil?
|
65
|
+
params[:marker] = marker unless marker.nil?
|
66
|
+
params[:delimiter] = delimiter unless delimiter.nil?
|
67
|
+
request = BosClient::Request.new @bucket_host, method: :get, params: params
|
69
68
|
request.run
|
70
69
|
end
|
71
70
|
end
|
data/lib/bos_client/config.rb
CHANGED
@@ -1,23 +1,20 @@
|
|
1
1
|
# encoding: UTF-8
|
2
2
|
module BosClient
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
}
|
3
|
+
DEFAULTS = {
|
4
|
+
scheme: 'http',
|
5
|
+
url: 'bcebos.com',
|
6
|
+
location: 'bj',
|
7
|
+
expiration_in_seconds: 1800,
|
8
|
+
access_key_id: '*****',
|
9
|
+
secret_access_key: '*****'
|
10
|
+
}.freeze
|
12
11
|
|
13
12
|
class << self
|
14
13
|
def options
|
15
14
|
@options ||= DEFAULTS.dup
|
16
15
|
end
|
17
16
|
|
18
|
-
|
19
|
-
@options = opts
|
20
|
-
end
|
17
|
+
attr_writer :options
|
21
18
|
|
22
19
|
def configure
|
23
20
|
yield self
|
@@ -26,17 +23,15 @@ module BosClient
|
|
26
23
|
def host
|
27
24
|
"#{options[:scheme]}://#{options[:location]}.#{options[:url]}"
|
28
25
|
end
|
29
|
-
|
30
26
|
end
|
31
27
|
|
32
|
-
DEFAULTS.each do |k,
|
33
|
-
|
34
|
-
|
28
|
+
DEFAULTS.each do |k, _v|
|
29
|
+
define_singleton_method "#{k}=" do |value|
|
30
|
+
options.merge!(k => value)
|
35
31
|
end
|
36
32
|
|
37
|
-
|
38
|
-
|
33
|
+
define_singleton_method k do
|
34
|
+
options[k]
|
39
35
|
end
|
40
36
|
end
|
41
|
-
|
42
37
|
end
|
data/lib/bos_client/error.rb
CHANGED
@@ -2,17 +2,16 @@
|
|
2
2
|
module BosClient
|
3
3
|
class Error < StandardError
|
4
4
|
class << self
|
5
|
-
|
6
|
-
|
7
|
-
unless const_defined?(name)
|
8
|
-
BosClient::Error.const_set(name.to_sym, Class.new(BosClient::Error)).new(message)
|
9
|
-
else
|
5
|
+
def bos_error(name, message)
|
6
|
+
if const_defined?(name)
|
10
7
|
eval "BosClient::Error::#{name}.new \"#{message}\""
|
8
|
+
else
|
9
|
+
BosClient::Error.const_set(name.to_sym, Class.new(BosClient::Error)).new(message)
|
11
10
|
end
|
12
11
|
end
|
13
12
|
|
14
13
|
def const_missing(name)
|
15
|
-
BosClient::Error.const_set(name.to_sym,Class.new(BosClient::Error))
|
14
|
+
BosClient::Error.const_set(name.to_sym, Class.new(BosClient::Error))
|
16
15
|
end
|
17
16
|
end
|
18
17
|
end
|
data/lib/bos_client/helper.rb
CHANGED
@@ -1,8 +1,7 @@
|
|
1
1
|
# encoding: UTF-8
|
2
2
|
module BosClient
|
3
3
|
module Helper
|
4
|
-
|
5
|
-
def snake_hash_keys value
|
4
|
+
def snake_hash_keys(value)
|
6
5
|
case value
|
7
6
|
when Array
|
8
7
|
value.map { |v| snake_hash_keys(v) }
|
@@ -29,10 +28,10 @@ module BosClient
|
|
29
28
|
|
30
29
|
def underscore(string)
|
31
30
|
string.gsub(/::/, '/')
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
31
|
+
.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
|
32
|
+
.gsub(/([a-z\d])([A-Z])/, '\1_\2')
|
33
|
+
.tr('-', '_')
|
34
|
+
.downcase
|
36
35
|
end
|
37
36
|
end
|
38
37
|
end
|
data/lib/bos_client/object.rb
CHANGED
@@ -7,71 +7,80 @@ module BosClient
|
|
7
7
|
@bucket = options[:bucket]
|
8
8
|
@size = options[:size]
|
9
9
|
@file = options[:key]
|
10
|
-
@path, @name =
|
10
|
+
@path, @name = File.split(options[:key])
|
11
11
|
@last_modified = options[:last_modified]
|
12
12
|
@storage_class = options[:storage_class]
|
13
13
|
end
|
14
14
|
|
15
|
-
|
16
15
|
def save
|
17
16
|
save_to nil
|
18
17
|
end
|
19
18
|
|
20
|
-
def save_as
|
19
|
+
def save_as(name)
|
21
20
|
save_to nil, name
|
22
21
|
end
|
23
22
|
|
24
|
-
def save_to
|
23
|
+
def save_to(path, name = nil)
|
25
24
|
headers = {
|
26
|
-
|
27
|
-
|
25
|
+
'host' => @bucket.bucket_host
|
26
|
+
}
|
28
27
|
|
29
28
|
params = {}
|
30
29
|
url = URI.encode("http://#{@bucket.bucket_host}/#{@file}")
|
31
|
-
request = Typhoeus::Request.new
|
30
|
+
request = Typhoeus::Request.new url,
|
31
|
+
method: :get,
|
32
|
+
headers: headers,
|
33
|
+
params: params
|
32
34
|
request = BosClient::Authable.authorize_request(request)
|
33
35
|
|
34
36
|
request.on_complete do |response|
|
35
|
-
if path
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
37
|
+
flie = if path
|
38
|
+
"#{path}/#{name || @name}"
|
39
|
+
else
|
40
|
+
(name || @name).to_s
|
41
|
+
end
|
40
42
|
write_file flie, response.body
|
41
43
|
end
|
42
44
|
request.run
|
43
45
|
end
|
44
46
|
|
45
47
|
def destory
|
46
|
-
request = BosClient::Request.new
|
48
|
+
request = BosClient::Request.new "#{@bucket.bucket_host}/#{@file}",
|
49
|
+
method: :delete
|
47
50
|
response = request.run
|
48
51
|
response[:result]
|
49
52
|
end
|
50
53
|
|
51
|
-
def self.upload
|
54
|
+
def self.upload(options = {})
|
52
55
|
bucket = options[:bucket]
|
53
56
|
file = options[:file]
|
54
57
|
origin_file_name = File.basename(file)
|
55
58
|
filename = options[:filename] || origin_file_name
|
56
59
|
path = options[:path] || ''
|
57
|
-
storage_class = options[:storage_class] ||
|
60
|
+
storage_class = options[:storage_class] || 'STANDARD'
|
58
61
|
headers = options[:headers] || {}
|
59
62
|
|
60
63
|
params = {
|
61
|
-
append:
|
64
|
+
append: ''
|
62
65
|
}
|
63
|
-
headers
|
64
|
-
|
66
|
+
headers['x-bce-storage-class'] = storage_class
|
67
|
+
|
68
|
+
url = "#{bucket.bucket_host}/#{File.join(path, filename)}?append"
|
69
|
+
request = BosClient::Request.new url,
|
70
|
+
method: :post,
|
71
|
+
params: params,
|
72
|
+
headers: headers,
|
73
|
+
body: File.open(file, 'r').read
|
65
74
|
response = request.run
|
66
75
|
response[:result]
|
67
76
|
end
|
68
77
|
|
69
78
|
private
|
79
|
+
|
70
80
|
def write_file(filename, data)
|
71
|
-
file = File.new(filename,
|
81
|
+
file = File.new(filename, 'wb')
|
72
82
|
file.write(data)
|
73
83
|
file.close
|
74
84
|
end
|
75
|
-
|
76
85
|
end
|
77
86
|
end
|
data/lib/bos_client/request.rb
CHANGED
@@ -4,7 +4,7 @@ module BosClient
|
|
4
4
|
include BosClient::Helper
|
5
5
|
attr_accessor :uri, :options
|
6
6
|
|
7
|
-
def initialize
|
7
|
+
def initialize(url, options = {})
|
8
8
|
@uri = URI(URI.encode(url))
|
9
9
|
@options = options
|
10
10
|
end
|
@@ -20,8 +20,9 @@ module BosClient
|
|
20
20
|
end
|
21
21
|
|
22
22
|
private
|
23
|
+
|
23
24
|
def format_request
|
24
|
-
headers = {
|
25
|
+
headers = { 'host' => @uri.host }
|
25
26
|
|
26
27
|
if @options[:headers].nil?
|
27
28
|
@options[:headers] = headers
|
@@ -29,65 +30,59 @@ module BosClient
|
|
29
30
|
@options[:headers].merge! headers
|
30
31
|
end
|
31
32
|
|
32
|
-
if @options[:params].nil?
|
33
|
-
@options[:params] = {}
|
34
|
-
end
|
33
|
+
@options[:params] = {} if @options[:params].nil?
|
35
34
|
|
36
35
|
request = Typhoeus::Request.new @uri.to_s, @options
|
37
36
|
BosClient::Authable.authorize_request request
|
38
37
|
end
|
39
38
|
|
40
|
-
def resolve_response
|
39
|
+
def resolve_response(response)
|
41
40
|
if response.success?
|
42
41
|
ret = resolve_bos_resault response
|
43
|
-
|
42
|
+
snake_hash_keys(ret)
|
44
43
|
elsif response.timed_out?
|
45
|
-
raise BosClient::Error::TimeOut
|
44
|
+
raise BosClient::Error::TimeOut, 'got a time out'
|
46
45
|
elsif response.code == 0
|
47
|
-
raise BosClient::Error::HttpError
|
46
|
+
raise BosClient::Error::HttpError, response.return_message
|
48
47
|
else
|
49
48
|
ret = resolve_bos_resault response
|
50
49
|
if ret[:data][:code]
|
51
|
-
raise
|
50
|
+
raise BosClient::Error.bos_error ret[:data][:code], ret[:data][:message]
|
52
51
|
else
|
53
|
-
raise BosClient::Error::HttpError
|
52
|
+
raise BosClient::Error::HttpError, "HTTP request failed: #{response.code}"
|
54
53
|
end
|
55
54
|
end
|
56
55
|
end
|
57
56
|
|
58
|
-
def resolve_bos_resault
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
end
|
57
|
+
def resolve_bos_resault(r)
|
58
|
+
ret = if !r.body.empty?
|
59
|
+
JSON.parse r.body, symbolize_names: true
|
60
|
+
else
|
61
|
+
{}
|
62
|
+
end
|
65
63
|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
end
|
64
|
+
{
|
65
|
+
result: true,
|
66
|
+
status: r.code,
|
67
|
+
ts: Time.now.to_i,
|
68
|
+
version: 1.0,
|
69
|
+
data: ret
|
70
|
+
}
|
71
|
+
rescue StandardError => e
|
72
|
+
raise BosClient::Error::JSONError, e.message
|
76
73
|
end
|
77
74
|
|
78
|
-
def resolve_response_headers
|
79
|
-
|
80
|
-
|
81
|
-
result
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
raise BosClient::Error::JSONError.new e.message
|
90
|
-
end
|
75
|
+
def resolve_response_headers(r)
|
76
|
+
ret = JSON.parse r.headers, symbolize_names: true
|
77
|
+
{
|
78
|
+
result: true,
|
79
|
+
status: r.code,
|
80
|
+
ts: Time.now.to_i,
|
81
|
+
version: 1.0,
|
82
|
+
data: ret
|
83
|
+
}
|
84
|
+
rescue StandardError => e
|
85
|
+
raise BosClient::Error::JSONError, e.message
|
91
86
|
end
|
92
87
|
end
|
93
88
|
end
|
data/lib/bos_client/service.rb
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
# encoding: UTF-8
|
2
2
|
module BosClient
|
3
3
|
class << self
|
4
|
-
|
5
4
|
def buckets
|
6
5
|
list_buckets unless @result
|
7
6
|
@result[:buckets].map { |bk| Bucket.new bk }
|
@@ -13,6 +12,7 @@ module BosClient
|
|
13
12
|
end
|
14
13
|
|
15
14
|
private
|
15
|
+
|
16
16
|
def list_buckets
|
17
17
|
url = host.to_s
|
18
18
|
request = BosClient::Request.new url, method: :get
|
data/lib/bos_client/version.rb
CHANGED
data/lib/bos_client.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
# encoding: UTF-8
|
2
2
|
require 'json'
|
3
3
|
require 'uri'
|
4
|
-
require
|
5
|
-
require
|
4
|
+
require 'bos_client/version'
|
5
|
+
require 'bos_client/config'
|
6
6
|
require 'bos_client/auth'
|
7
7
|
require 'bos_client/helper'
|
8
8
|
require 'bos_client/service'
|
@@ -11,5 +11,4 @@ require 'bos_client/request'
|
|
11
11
|
require 'bos_client/bucket'
|
12
12
|
require 'bos_client/object'
|
13
13
|
module BosClient
|
14
|
-
|
15
14
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bos_client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Feng Ce
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-02-
|
11
|
+
date: 2017-02-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: typhoeus
|
@@ -80,7 +80,9 @@ dependencies:
|
|
80
80
|
- - "~>"
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '0.1'
|
83
|
-
description:
|
83
|
+
description: |-
|
84
|
+
Library for accessing BOS objects and buckets through
|
85
|
+
REST API
|
84
86
|
email:
|
85
87
|
- kalelfc@gmail.com
|
86
88
|
executables: []
|
@@ -90,6 +92,7 @@ files:
|
|
90
92
|
- ".gitignore"
|
91
93
|
- ".rspec"
|
92
94
|
- ".travis.yml"
|
95
|
+
- CHANGELOG
|
93
96
|
- CODE_OF_CONDUCT.md
|
94
97
|
- Gemfile
|
95
98
|
- LICENSE.txt
|
@@ -128,7 +131,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
128
131
|
version: '0'
|
129
132
|
requirements: []
|
130
133
|
rubyforge_project:
|
131
|
-
rubygems_version: 2.
|
134
|
+
rubygems_version: 2.6.10
|
132
135
|
signing_key:
|
133
136
|
specification_version: 4
|
134
137
|
summary: Baidu Object Storage Unofficial Ruby SDK
|