bos_client 0.1.0
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 +7 -0
- data/.gitignore +12 -0
- data/.rspec +2 -0
- data/.travis.yml +7 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +34 -0
- data/Rakefile +2 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/bos_client.gemspec +28 -0
- data/lib/bos_client/auth.rb +85 -0
- data/lib/bos_client/bucket.rb +72 -0
- data/lib/bos_client/config.rb +42 -0
- data/lib/bos_client/error.rb +19 -0
- data/lib/bos_client/helper.rb +38 -0
- data/lib/bos_client/object.rb +77 -0
- data/lib/bos_client/request.rb +93 -0
- data/lib/bos_client/service.rb +23 -0
- data/lib/bos_client/version.rb +3 -0
- data/lib/bos_client.rb +15 -0
- metadata +135 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6b4f7253d95434dfe4780441e9e8abd04de23d7a
|
4
|
+
data.tar.gz: c07a5b2a3b79b6838b53f951c0abc9a7c60095dd
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 729c021c631f4489d9f27943e50aa700b401af2303a39a65b01c26a35e4570a5061c4c75f0c7dabd9be811d812decf06324cae23e30d098b0238b4573d0025a5
|
7
|
+
data.tar.gz: c558fe178daeba6b80bb577c04458d17e8ffa0bd33178860ac4872177bfad7a8c587b1d665c4af07d39c7c26e78b93f35eceb14c3421d1cfa4204b49eebc4070
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
|
4
|
+
|
5
|
+
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
|
6
|
+
|
7
|
+
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
|
8
|
+
|
9
|
+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
|
10
|
+
|
11
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
|
12
|
+
|
13
|
+
This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Feng Ce
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
# BosClient
|
2
|
+
|
3
|
+
## Installation
|
4
|
+
|
5
|
+
Add this line to your application's Gemfile:
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
gem 'bos', :git => 'git@github.com:fcce/bos.git'
|
9
|
+
```
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install bos_client
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
|
22
|
+
## Development
|
23
|
+
|
24
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
|
25
|
+
|
26
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
27
|
+
|
28
|
+
## Contributing
|
29
|
+
|
30
|
+
1. Fork it ( https://github.com/[my-github-username]/bos_client/fork )
|
31
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
32
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
33
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
34
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "bos_client"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/bos_client.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'bos_client/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "bos_client"
|
8
|
+
spec.version = BosClient::VERSION
|
9
|
+
spec.authors = ["Feng Ce"]
|
10
|
+
spec.email = ["kalelfc@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = "Baidu Object Storage Unofficial Ruby SDK"
|
13
|
+
spec.description = "Library for accessing BOS objects and buckets through REST API"
|
14
|
+
spec.homepage = "http://github.com/fcce/bos"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.required_ruby_version = ">= 2.0"
|
22
|
+
|
23
|
+
spec.add_runtime_dependency "typhoeus", "~> 1.1"
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
27
|
+
spec.add_development_dependency "pry", "~> 0.1"
|
28
|
+
end
|
@@ -0,0 +1,85 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
require 'openssl'
|
3
|
+
require 'typhoeus'
|
4
|
+
require 'uri'
|
5
|
+
module BosClient
|
6
|
+
class Authable
|
7
|
+
|
8
|
+
def self.authorize_request request
|
9
|
+
default_headers = get_default_headers request
|
10
|
+
request.options[:headers].merge! default_headers
|
11
|
+
authorization = sign(request)
|
12
|
+
request.options[:headers].merge!({"Authorization" => authorization})
|
13
|
+
request
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
class << self
|
18
|
+
def get_canonical_time t = Time.now.to_i
|
19
|
+
Time.at(t).utc.strftime("%FT%TZ")
|
20
|
+
end
|
21
|
+
|
22
|
+
def get_default_headers request
|
23
|
+
{
|
24
|
+
"content-type" =>"text/plain",
|
25
|
+
"x-bce-date" => get_canonical_time,
|
26
|
+
"content-length" => (request.options[:body] || "").length
|
27
|
+
}
|
28
|
+
# todo PUT 方法计算的签名错误,content-length 的问题,暂时处理。
|
29
|
+
# headers.merge!({"content-length" => 0}) if request.options[:method] == :put
|
30
|
+
end
|
31
|
+
|
32
|
+
def get_canonical_uri request
|
33
|
+
uri = URI(request.base_url)
|
34
|
+
url_path = URI.encode(uri.path)
|
35
|
+
url_path == '' ? '/' : url_path
|
36
|
+
end
|
37
|
+
|
38
|
+
def get_canonical_query_string request
|
39
|
+
params = request.options[:params]
|
40
|
+
params = params.map do |k, v|
|
41
|
+
"#{URI.encode(k.to_s)}=#{encode_slash(URI.encode(v.to_s))}"
|
42
|
+
end.compact.sort.join('&')
|
43
|
+
params
|
44
|
+
end
|
45
|
+
|
46
|
+
def get_canonical_headers request
|
47
|
+
headers_to_sign_keys = ["host", "content-md5", "content-length", "content-type"]
|
48
|
+
headers_to_sign = []
|
49
|
+
canonical_headers_downcase = request.options[:headers].each do |k, v|
|
50
|
+
if headers_to_sign_keys.include?(k) || k.start_with?('x-bce')
|
51
|
+
headers_to_sign << "#{encode(k.to_s.downcase)}:#{encode(v.to_s)}"
|
52
|
+
end
|
53
|
+
end
|
54
|
+
headers_to_sign.compact.sort.join("\n")
|
55
|
+
end
|
56
|
+
|
57
|
+
def get_http_method request
|
58
|
+
(request.options[:method] || 'get').upcase
|
59
|
+
end
|
60
|
+
|
61
|
+
def encode string
|
62
|
+
URI.encode(string, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
|
63
|
+
end
|
64
|
+
|
65
|
+
def encode_slash str
|
66
|
+
str.gsub(/\//,'%2F')
|
67
|
+
end
|
68
|
+
|
69
|
+
def sign request
|
70
|
+
digest = OpenSSL::Digest.new('sha256')
|
71
|
+
sign_key_prefix = "bce-auth-v1/#{BosClient.access_key_id}/#{get_canonical_time()}/#{BosClient.expiration_in_seconds}"
|
72
|
+
sign_key = OpenSSL::HMAC.hexdigest digest, BosClient.secret_access_key, sign_key_prefix
|
73
|
+
|
74
|
+
http_method = get_http_method request
|
75
|
+
|
76
|
+
canonical_uri = get_canonical_uri request
|
77
|
+
canonical_query_string = get_canonical_query_string request
|
78
|
+
canonical_headers = get_canonical_headers request
|
79
|
+
string_to_sign = [http_method, canonical_uri, canonical_query_string, canonical_headers].join("\n")
|
80
|
+
sign_result = OpenSSL::HMAC.hexdigest digest, sign_key, string_to_sign
|
81
|
+
"#{sign_key_prefix}//#{sign_result}"
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
module BosClient
|
3
|
+
class Bucket
|
4
|
+
attr_accessor :name, :bucket_host, :creation_date, :bucket_host
|
5
|
+
|
6
|
+
def initialize(options = {})
|
7
|
+
@name = options[:name]
|
8
|
+
@location = options[:location] || BosClient.location
|
9
|
+
@creation_date = options[:creation_date]
|
10
|
+
@bucket_host = "#{BosClient.scheme}://#{@name}.#{@location}.#{BosClient.url}"
|
11
|
+
end
|
12
|
+
|
13
|
+
def accessable?
|
14
|
+
request = BosClient::Request.new @bucket_host, method: :head
|
15
|
+
response = request.run
|
16
|
+
response[:result]
|
17
|
+
end
|
18
|
+
|
19
|
+
#create bucket
|
20
|
+
# 401
|
21
|
+
|
22
|
+
# def save
|
23
|
+
# request = BosClient::Request.new @bucket_host, method: :put, params:params
|
24
|
+
# request.run
|
25
|
+
# end
|
26
|
+
|
27
|
+
def destory
|
28
|
+
request = BosClient::Request.new @bucket_host, method: :delete
|
29
|
+
response = request.run
|
30
|
+
response[:result]
|
31
|
+
end
|
32
|
+
|
33
|
+
## objects
|
34
|
+
def objects options = {}
|
35
|
+
response = list_objects options.merge({delimiter: '/'})
|
36
|
+
objs = response[:data][:contents]
|
37
|
+
objs.map { |obj| BosClient::Object.new ({bucket:self}.merge(obj)) }
|
38
|
+
end
|
39
|
+
|
40
|
+
def dirs options = {}
|
41
|
+
response = list_objects options
|
42
|
+
data = response[:data][:contents]
|
43
|
+
data = data.find_all{|e| is_dir?(e[:key]) }
|
44
|
+
data.map { |e| e[:key] }
|
45
|
+
end
|
46
|
+
|
47
|
+
def dir_objects prefix, options = {}
|
48
|
+
response = list_objects options.merge({delimiter: '/',prefix:prefix})
|
49
|
+
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)) }
|
52
|
+
end
|
53
|
+
|
54
|
+
private
|
55
|
+
def is_dir? name
|
56
|
+
name.end_with?('/')
|
57
|
+
end
|
58
|
+
|
59
|
+
def list_objects options = {}
|
60
|
+
prefix = options.fetch(:prefix, nil)
|
61
|
+
max_keys = options.fetch(:max_keys, 1000)
|
62
|
+
marker = options.fetch(:marker, nil)
|
63
|
+
delimiter = options.fetch(:delimiter, nil)
|
64
|
+
params = {maxKeys: max_keys}
|
65
|
+
params.merge!({prefix: prefix}) unless prefix.nil?
|
66
|
+
params.merge!({marker: marker}) unless marker.nil?
|
67
|
+
params.merge!({delimiter: delimiter}) unless delimiter.nil?
|
68
|
+
request = BosClient::Request.new @bucket_host, method: :get, params:params
|
69
|
+
request.run
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
module BosClient
|
3
|
+
|
4
|
+
DEFAULTS = {
|
5
|
+
scheme: 'http',
|
6
|
+
url: 'bcebos.com',
|
7
|
+
location: 'bj',
|
8
|
+
expiration_in_seconds: 1800,
|
9
|
+
access_key_id: '*****',
|
10
|
+
secret_access_key: '*****'
|
11
|
+
}
|
12
|
+
|
13
|
+
class << self
|
14
|
+
def options
|
15
|
+
@options ||= DEFAULTS.dup
|
16
|
+
end
|
17
|
+
|
18
|
+
def options=(opts)
|
19
|
+
@options = opts
|
20
|
+
end
|
21
|
+
|
22
|
+
def configure
|
23
|
+
yield self
|
24
|
+
end
|
25
|
+
|
26
|
+
def host
|
27
|
+
"#{options[:scheme]}://#{options[:location]}.#{options[:url]}"
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
DEFAULTS.each do |k, v|
|
33
|
+
self.define_singleton_method "#{k}=" do |value|
|
34
|
+
self.options.merge!(k => value)
|
35
|
+
end
|
36
|
+
|
37
|
+
self.define_singleton_method k do
|
38
|
+
self.options[k]
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
module BosClient
|
3
|
+
class Error < StandardError
|
4
|
+
class << self
|
5
|
+
|
6
|
+
def bos_error name, message
|
7
|
+
unless const_defined?(name)
|
8
|
+
BosClient::Error.const_set(name.to_sym, Class.new(BosClient::Error)).new(message)
|
9
|
+
else
|
10
|
+
eval "BosClient::Error::#{name}.new \"#{message}\""
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def const_missing(name)
|
15
|
+
BosClient::Error.const_set(name.to_sym,Class.new(BosClient::Error))
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
module BosClient
|
3
|
+
module Helper
|
4
|
+
|
5
|
+
def snake_hash_keys value
|
6
|
+
case value
|
7
|
+
when Array
|
8
|
+
value.map { |v| snake_hash_keys(v) }
|
9
|
+
when Hash
|
10
|
+
snake_hash(value)
|
11
|
+
else
|
12
|
+
value
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def snake_hash(value)
|
17
|
+
Hash[value.map { |k, v| [underscore_key(k), snake_hash_keys(v)] }]
|
18
|
+
end
|
19
|
+
|
20
|
+
def underscore_key(k)
|
21
|
+
if k.is_a? Symbol
|
22
|
+
underscore(k.to_s).to_sym
|
23
|
+
elsif k.is_a? String
|
24
|
+
underscore(k)
|
25
|
+
else
|
26
|
+
k
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def underscore(string)
|
31
|
+
string.gsub(/::/, '/')
|
32
|
+
.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
|
33
|
+
.gsub(/([a-z\d])([A-Z])/, '\1_\2')
|
34
|
+
.tr('-', '_')
|
35
|
+
.downcase
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
module BosClient
|
3
|
+
class Object
|
4
|
+
attr_accessor :bucket, :size, :path, :last_modified, :name, :storage_class
|
5
|
+
|
6
|
+
def initialize(options = {})
|
7
|
+
@bucket = options[:bucket]
|
8
|
+
@size = options[:size]
|
9
|
+
@file = options[:key]
|
10
|
+
@path, @name = File.split(options[:key])
|
11
|
+
@last_modified = options[:last_modified]
|
12
|
+
@storage_class = options[:storage_class]
|
13
|
+
end
|
14
|
+
|
15
|
+
|
16
|
+
def save
|
17
|
+
save_to nil
|
18
|
+
end
|
19
|
+
|
20
|
+
def save_as name
|
21
|
+
save_to nil, name
|
22
|
+
end
|
23
|
+
|
24
|
+
def save_to path, name = nil
|
25
|
+
headers = {
|
26
|
+
"host" => @bucket.bucket_host,
|
27
|
+
}
|
28
|
+
|
29
|
+
params = {}
|
30
|
+
url = URI.encode("http://#{@bucket.bucket_host}/#{@file}")
|
31
|
+
request = Typhoeus::Request.new(url, method: :get,headers:headers,params:params)
|
32
|
+
request = BosClient::Authable.authorize_request(request)
|
33
|
+
|
34
|
+
request.on_complete do |response|
|
35
|
+
if path
|
36
|
+
flie = "#{path}/#{name || @name}"
|
37
|
+
else
|
38
|
+
flie = "#{name || @name}"
|
39
|
+
end
|
40
|
+
write_file flie, response.body
|
41
|
+
end
|
42
|
+
request.run
|
43
|
+
end
|
44
|
+
|
45
|
+
def destory
|
46
|
+
request = BosClient::Request.new "#{@bucket.bucket_host}/#{@file}", method: :delete
|
47
|
+
response = request.run
|
48
|
+
response[:result]
|
49
|
+
end
|
50
|
+
|
51
|
+
def self.upload options = {}
|
52
|
+
bucket = options[:bucket]
|
53
|
+
file = options[:file]
|
54
|
+
origin_file_name = File.basename(file)
|
55
|
+
filename = options[:filename] || origin_file_name
|
56
|
+
path = options[:path] || ''
|
57
|
+
storage_class = options[:storage_class] || "STANDARD"
|
58
|
+
headers = options[:headers] || {}
|
59
|
+
|
60
|
+
params = {
|
61
|
+
append: ""
|
62
|
+
}
|
63
|
+
headers.merge!({"x-bce-storage-class" => storage_class})
|
64
|
+
request = BosClient::Request.new "#{bucket.bucket_host}/#{File.join(path,filename)}?append", method: :post, params:params, headers: headers, body: File.open(file,"r").read
|
65
|
+
response = request.run
|
66
|
+
response[:result]
|
67
|
+
end
|
68
|
+
|
69
|
+
private
|
70
|
+
def write_file(filename, data)
|
71
|
+
file = File.new(filename, "wb")
|
72
|
+
file.write(data)
|
73
|
+
file.close
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,93 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
module BosClient
|
3
|
+
class Request
|
4
|
+
include BosClient::Helper
|
5
|
+
attr_accessor :uri, :options
|
6
|
+
|
7
|
+
def initialize url , options={}
|
8
|
+
@uri = URI(URI.encode(url))
|
9
|
+
@options = options
|
10
|
+
end
|
11
|
+
|
12
|
+
def run
|
13
|
+
request = format_request
|
14
|
+
resolve_response request.run
|
15
|
+
end
|
16
|
+
|
17
|
+
def response_headers
|
18
|
+
request = format_request
|
19
|
+
resolve_response_headers request.run
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
def format_request
|
24
|
+
headers = { "host" => @uri.host }
|
25
|
+
|
26
|
+
if @options[:headers].nil?
|
27
|
+
@options[:headers] = headers
|
28
|
+
else
|
29
|
+
@options[:headers].merge! headers
|
30
|
+
end
|
31
|
+
|
32
|
+
if @options[:params].nil?
|
33
|
+
@options[:params] = {}
|
34
|
+
end
|
35
|
+
|
36
|
+
request = Typhoeus::Request.new @uri.to_s, @options
|
37
|
+
BosClient::Authable.authorize_request request
|
38
|
+
end
|
39
|
+
|
40
|
+
def resolve_response response
|
41
|
+
if response.success?
|
42
|
+
ret = resolve_bos_resault response
|
43
|
+
return snake_hash_keys(ret)
|
44
|
+
elsif response.timed_out?
|
45
|
+
raise BosClient::Error::TimeOut.new "got a time out"
|
46
|
+
elsif response.code == 0
|
47
|
+
raise BosClient::Error::HttpError.new response.return_message
|
48
|
+
else
|
49
|
+
ret = resolve_bos_resault response
|
50
|
+
if ret[:data][:code]
|
51
|
+
raise BosClient::Error.bos_error ret[:data][:code], ret[:data][:message]
|
52
|
+
else
|
53
|
+
raise BosClient::Error::HttpError.new "HTTP request failed: #{response.code.to_s}"
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def resolve_bos_resault r
|
59
|
+
begin
|
60
|
+
if r.body.size > 0
|
61
|
+
ret = JSON.parse r.body,{:symbolize_names => true}
|
62
|
+
else
|
63
|
+
ret = {}
|
64
|
+
end
|
65
|
+
|
66
|
+
result = {
|
67
|
+
result: true,
|
68
|
+
status: r.code,
|
69
|
+
ts: Time.now.to_i,
|
70
|
+
version: 1.0,
|
71
|
+
data: ret
|
72
|
+
}
|
73
|
+
rescue Exception => e
|
74
|
+
raise BosClient::Error::JSONError.new e.message
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def resolve_response_headers r
|
79
|
+
begin
|
80
|
+
ret = JSON.parse r.headers,{:symbolize_names => true}
|
81
|
+
result = {
|
82
|
+
result: true,
|
83
|
+
status: r.code,
|
84
|
+
ts: Time.now.to_i,
|
85
|
+
version: 1.0,
|
86
|
+
data: ret
|
87
|
+
}
|
88
|
+
rescue Exception => e
|
89
|
+
raise BosClient::Error::JSONError.new e.message
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
module BosClient
|
3
|
+
class << self
|
4
|
+
|
5
|
+
def buckets
|
6
|
+
list_buckets unless @result
|
7
|
+
@result[:buckets].map { |bk| Bucket.new bk }
|
8
|
+
end
|
9
|
+
|
10
|
+
def owner
|
11
|
+
list_buckets unless @result
|
12
|
+
@result[:owner]
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
def list_buckets
|
17
|
+
url = host.to_s
|
18
|
+
request = BosClient::Request.new url, method: :get
|
19
|
+
response = request.run
|
20
|
+
@result = response[:data]
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/lib/bos_client.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
require 'json'
|
3
|
+
require 'uri'
|
4
|
+
require "bos_client/version"
|
5
|
+
require "bos_client/config"
|
6
|
+
require 'bos_client/auth'
|
7
|
+
require 'bos_client/helper'
|
8
|
+
require 'bos_client/service'
|
9
|
+
require 'bos_client/error'
|
10
|
+
require 'bos_client/request'
|
11
|
+
require 'bos_client/bucket'
|
12
|
+
require 'bos_client/object'
|
13
|
+
module BosClient
|
14
|
+
|
15
|
+
end
|
metadata
ADDED
@@ -0,0 +1,135 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: bos_client
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Feng Ce
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-02-15 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: typhoeus
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.1'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.1'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.6'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.6'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: pry
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0.1'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0.1'
|
83
|
+
description: Library for accessing BOS objects and buckets through REST API
|
84
|
+
email:
|
85
|
+
- kalelfc@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- ".rspec"
|
92
|
+
- ".travis.yml"
|
93
|
+
- CODE_OF_CONDUCT.md
|
94
|
+
- Gemfile
|
95
|
+
- LICENSE.txt
|
96
|
+
- README.md
|
97
|
+
- Rakefile
|
98
|
+
- bin/console
|
99
|
+
- bin/setup
|
100
|
+
- bos_client.gemspec
|
101
|
+
- lib/bos_client.rb
|
102
|
+
- lib/bos_client/auth.rb
|
103
|
+
- lib/bos_client/bucket.rb
|
104
|
+
- lib/bos_client/config.rb
|
105
|
+
- lib/bos_client/error.rb
|
106
|
+
- lib/bos_client/helper.rb
|
107
|
+
- lib/bos_client/object.rb
|
108
|
+
- lib/bos_client/request.rb
|
109
|
+
- lib/bos_client/service.rb
|
110
|
+
- lib/bos_client/version.rb
|
111
|
+
homepage: http://github.com/fcce/bos
|
112
|
+
licenses:
|
113
|
+
- MIT
|
114
|
+
metadata: {}
|
115
|
+
post_install_message:
|
116
|
+
rdoc_options: []
|
117
|
+
require_paths:
|
118
|
+
- lib
|
119
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
120
|
+
requirements:
|
121
|
+
- - ">="
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
version: '2.0'
|
124
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
125
|
+
requirements:
|
126
|
+
- - ">="
|
127
|
+
- !ruby/object:Gem::Version
|
128
|
+
version: '0'
|
129
|
+
requirements: []
|
130
|
+
rubyforge_project:
|
131
|
+
rubygems_version: 2.2.2
|
132
|
+
signing_key:
|
133
|
+
specification_version: 4
|
134
|
+
summary: Baidu Object Storage Unofficial Ruby SDK
|
135
|
+
test_files: []
|