qiniu-rs 1.1.0 → 2.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -1,4 +1,4 @@
1
- source 'https://rubygems.org'
1
+ source 'http://ruby.taobao.org'
2
2
 
3
3
  # Specify your gem's dependencies in qiniu-s3.gemspec
4
4
  gemspec
data/lib/qiniu/rs/auth.rb CHANGED
@@ -38,7 +38,7 @@ module Qiniu
38
38
  @refresh_token = refresh_token
39
39
  end
40
40
 
41
- def call(url, data, retry_times = 0)
41
+ def call_with_logged_in(url, data, retry_times = 0)
42
42
  raise MissingAccessToken if @access_token.nil?
43
43
  code, data = http_request url, data, {:access_token => @access_token}
44
44
  if code == 401
@@ -51,16 +51,25 @@ module Qiniu
51
51
  if code == 200
52
52
  retry_times += 1
53
53
  if Config.settings[:auto_reconnect] && retry_times < Config.settings[:max_retry_times]
54
- return call(url, data, retry_times)
54
+ return call_with_logged_in(url, data, retry_times)
55
55
  end
56
56
  end
57
57
  end
58
58
  [code, data]
59
59
  end
60
60
 
61
+ def call_with_signature(url, data, retry_times = 0)
62
+ code, data = http_request url, data, {:signature_auth => true}
63
+ [code, data]
64
+ end
65
+
61
66
  def request(url, data = nil)
62
67
  begin
63
- code, data = Auth.call(url, data)
68
+ if Config.settings[:access_key].empty? || Config.settings[:secret_key].empty?
69
+ code, data = Auth.call_with_logged_in(url, data)
70
+ else
71
+ code, data = Auth.call_with_signature(url, data)
72
+ end
64
73
  rescue [MissingAccessToken, MissingRefreshToken, MissingUsernameOrPassword] => e
65
74
  Log.logger.error e
66
75
  code, data = 401, {}
@@ -21,9 +21,13 @@ module Qiniu
21
21
  :content_type => 'application/x-www-form-urlencoded',
22
22
  :auth_url => "https://acc.qbox.me/oauth2/token",
23
23
  :rs_host => "http://rs.qbox.me:10100",
24
+ #:rs_host => "http://localhost:10100",
24
25
  :io_host => "http://iovip.qbox.me",
25
- :client_id => "",
26
- :client_secret => "",
26
+ #:io_host => "http://localhost:10200",
27
+ :client_id => "a75604760c4da4caaa456c0c5895c061c3065c5a",
28
+ :client_secret => "75df554a39f58accb7eb293b550fa59618674b7d",
29
+ :access_key => "",
30
+ :secret_key => "",
27
31
  :auto_reconnect => true,
28
32
  :max_retry_times => 5
29
33
  }
@@ -5,6 +5,7 @@ require 'json'
5
5
  require 'zlib'
6
6
  require 'base64'
7
7
  require 'rest_client'
8
+ require 'hmac-sha1'
8
9
  require 'qiniu/rs/exceptions'
9
10
 
10
11
  module Qiniu
@@ -37,7 +38,12 @@ module Qiniu
37
38
  :accept => :json,
38
39
  :user_agent => Config.settings[:user_agent]
39
40
  }
40
- header_options.merge!('Authorization' => "Bearer #{options[:access_token]}") if options[:access_token]
41
+ if options[:signature_auth] && options[:signature_auth] == true
42
+ signature_token = generate_qbox_signature(Config.settings[:access_key], Config.settings[:secret_key], url, data)
43
+ header_options.merge!('Authorization' => "QBox #{signature_token}")
44
+ elsif options[:access_token]
45
+ header_options.merge!('Authorization' => "Bearer #{options[:access_token]}")
46
+ end
41
47
  case options[:method]
42
48
  when :get
43
49
  response = RestClient.get url, header_options
@@ -124,6 +130,22 @@ module Qiniu
124
130
  File.open(filepath, "rb") { |f| Zlib.crc32 f.read }
125
131
  end
126
132
 
133
+ def generate_qbox_signature(access_key, secret_key, url, params)
134
+ uri = URI.parse(url)
135
+ signature = uri.path
136
+ query_string = uri.query
137
+ signature += '?' + query_string if !query_string.nil? && !query_string.empty?
138
+ signature += "\n";
139
+ if params.is_a?(Hash)
140
+ total_param = params.map { |key, value| key.to_s+"="+value.to_s }
141
+ signature += total_param.join("&")
142
+ end
143
+ hmac = HMAC::SHA1.new(secret_key)
144
+ hmac.update(signature)
145
+ encoded_digest = urlsafe_base64_encode(hmac.digest)
146
+ %Q(#{access_key}:#{encoded_digest})
147
+ end
148
+
127
149
  end
128
150
  end
129
151
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Qiniu
4
4
  module RS
5
- VERSION = "1.1.0"
5
+ VERSION = "2.0.1"
6
6
  end
7
7
  end
data/qiniu-rs.gemspec CHANGED
@@ -5,8 +5,8 @@ require File.expand_path('../lib/qiniu/rs/version', __FILE__)
5
5
  Gem::Specification.new do |gem|
6
6
  gem.authors = ["why404"]
7
7
  gem.email = ["why404@gmail.com"]
8
- gem.description = %q{Qiniu Cloud Storage SDK for Ruby. See: http://docs.qiniutek.com/v1/sdk/ruby/}
9
- gem.summary = %q{Qiniu Cloud Storage SDK for Ruby}
8
+ gem.description = %q{Qiniu Resource (Cloud) Storage SDK for Ruby. See: http://docs.qiniutek.com/v2/sdk/ruby/}
9
+ gem.summary = %q{Qiniu Resource (Cloud) Storage SDK for Ruby}
10
10
  gem.homepage = "https://github.com/why404/qiniu-rs"
11
11
 
12
12
  gem.files = `git ls-files`.split($\)
@@ -17,11 +17,12 @@ Gem::Specification.new do |gem|
17
17
  gem.version = Qiniu::RS::VERSION
18
18
 
19
19
  # specify any dependencies here; for example:
20
- gem.add_development_dependency "rake"
21
- gem.add_development_dependency "rspec"
22
- gem.add_development_dependency "fakeweb"
23
- gem.add_runtime_dependency "json"
24
- gem.add_runtime_dependency "rest-client"
25
- gem.add_runtime_dependency "mime-types"
26
- gem.add_runtime_dependency "jruby-openssl" if RUBY_PLATFORM == "java"
20
+ gem.add_development_dependency "rake", "~> 0.9.2.2"
21
+ gem.add_development_dependency "rspec", "~> 2.10.0"
22
+ gem.add_development_dependency "fakeweb", "~> 1.3.0"
23
+ gem.add_runtime_dependency "json", "~> 1.7.3"
24
+ gem.add_runtime_dependency "rest-client", "~> 1.6.7"
25
+ gem.add_runtime_dependency "mime-types", "~> 1.19"
26
+ gem.add_runtime_dependency "ruby-hmac", "~> 0.4.0"
27
+ gem.add_runtime_dependency "jruby-openssl", "~> 0.7.7" if RUBY_PLATFORM == "java"
27
28
  end
@@ -2,14 +2,15 @@
2
2
 
3
3
  require 'spec_helper'
4
4
  require 'qiniu/rs/auth'
5
- require 'qiniu/rs/rs'
5
+ require 'qiniu/rs'
6
6
  require 'qiniu/rs/image'
7
7
 
8
8
  module Qiniu
9
9
  module RS
10
10
  describe Image do
11
11
 
12
- before :all do
12
+ before :each do
13
+ =begin
13
14
  code, data = Qiniu::RS::Auth.exchange_by_password!("test@qbox.net", "test")
14
15
  code.should == 200
15
16
  data.should be_an_instance_of(Hash)
@@ -17,20 +18,30 @@ module Qiniu
17
18
  data["refresh_token"].should_not be_empty
18
19
  data["refresh_token"].should_not be_empty
19
20
  puts data.inspect
21
+ =end
20
22
 
21
23
  @bucket = "test_images"
22
- @key = "test_image.jpg"
23
- code2, data2 = Qiniu::RS::RS.get(@bucket, @key)
24
- code2.should == 200
25
- data2["url"].should_not be_empty
26
- puts data2.inspect
24
+ @key = "image_logo_for_test.png"
27
25
 
28
- @download_url = data2["url"]
26
+ local_file = File.expand_path('../' + @key, __FILE__)
27
+
28
+ put_url = Qiniu::RS.put_auth(10)
29
+ put_url.should_not be_false
30
+ put_url.should_not be_empty
31
+ result = Qiniu::RS.upload :url => put_url,
32
+ :file => local_file,
33
+ :bucket => @bucket,
34
+ :key => @key,
35
+ :enable_crc32_check => true
36
+ result.should be_true
29
37
  end
30
38
 
31
39
  context ".info" do
32
40
  it "should works" do
33
- code, data = Qiniu::RS::Image.info(@download_url)
41
+ result = Qiniu::RS.get(@bucket, @key)
42
+ result["url"].should_not be_empty
43
+ puts result.inspect
44
+ code, data = Qiniu::RS::Image.info(result["url"])
34
45
  code.should == 200
35
46
  puts data.inspect
36
47
  end
@@ -10,6 +10,7 @@ module Qiniu
10
10
  describe IO do
11
11
 
12
12
  before :all do
13
+ =begin
13
14
  code, data = Qiniu::RS::Auth.exchange_by_password!("test@qbox.net", "test")
14
15
  code.should == 200
15
16
  data.should be_an_instance_of(Hash)
@@ -17,6 +18,7 @@ module Qiniu
17
18
  data["refresh_token"].should_not be_empty
18
19
  data["refresh_token"].should_not be_empty
19
20
  puts data.inspect
21
+ =end
20
22
 
21
23
  code2, data2 = Qiniu::RS::IO.put_auth()
22
24
  code2.should == 200
@@ -11,6 +11,7 @@ module Qiniu
11
11
  describe RS do
12
12
 
13
13
  before :all do
14
+ =begin
14
15
  code, data = Qiniu::RS::Auth.exchange_by_password!("test@qbox.net", "test")
15
16
  code.should == 200
16
17
  data.should be_an_instance_of(Hash)
@@ -18,6 +19,7 @@ module Qiniu
18
19
  data["refresh_token"].should_not be_empty
19
20
  data["refresh_token"].should_not be_empty
20
21
  puts data.inspect
22
+ =end
21
23
 
22
24
  code2, data2 = Qiniu::RS::IO.put_auth()
23
25
  code2.should == 200
@@ -12,12 +12,14 @@ module Qiniu
12
12
  @domain = 'cdn.example.com'
13
13
  end
14
14
 
15
+ =begin
15
16
  context ".login!" do
16
17
  it "should works" do
17
18
  result = Qiniu::RS.login!("test@qbox.net", "test")
18
19
  result.should be_true
19
20
  end
20
21
  end
22
+ =end
21
23
 
22
24
  context ".put_auth" do
23
25
  it "should works" do
@@ -136,7 +138,7 @@ module Qiniu
136
138
 
137
139
  context ".image_info" do
138
140
  it "should works" do
139
- data = Qiniu::RS.get("test_images", "test_image.jpg")
141
+ data = Qiniu::RS.get("test_images", "image_logo_for_test.png")
140
142
  data.should_not be_false
141
143
  data.should_not be_empty
142
144
  puts data.inspect
data/spec/spec_helper.rb CHANGED
@@ -6,7 +6,7 @@ require 'rspec'
6
6
 
7
7
  RSpec.configure do |config|
8
8
  config.before :all do
9
- Qiniu::RS.establish_connection! :client_id => "abcd0c7edcdf914228ed8aa7c6cee2f2bc6155e2",
10
- :client_secret => "fc9ef8b171a74e197b17f85ba23799860ddf3b9c"
9
+ Qiniu::RS.establish_connection! :access_key => "3fPHl_SLkPXdioqI_A8_NGngPWVJhlDk2ktRjogH",
10
+ :secret_key => "bXTPMDJrVYRJUiSDRFtFYwycVD_mjXxYWrCYlDHy"
11
11
  end
12
12
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: qiniu-rs
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 2.0.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,105 +9,121 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-06-08 00:00:00.000000000 Z
12
+ date: 2012-06-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
- - - ! '>='
19
+ - - ~>
20
20
  - !ruby/object:Gem::Version
21
- version: '0'
21
+ version: 0.9.2.2
22
22
  type: :development
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
25
25
  none: false
26
26
  requirements:
27
- - - ! '>='
27
+ - - ~>
28
28
  - !ruby/object:Gem::Version
29
- version: '0'
29
+ version: 0.9.2.2
30
30
  - !ruby/object:Gem::Dependency
31
31
  name: rspec
32
32
  requirement: !ruby/object:Gem::Requirement
33
33
  none: false
34
34
  requirements:
35
- - - ! '>='
35
+ - - ~>
36
36
  - !ruby/object:Gem::Version
37
- version: '0'
37
+ version: 2.10.0
38
38
  type: :development
39
39
  prerelease: false
40
40
  version_requirements: !ruby/object:Gem::Requirement
41
41
  none: false
42
42
  requirements:
43
- - - ! '>='
43
+ - - ~>
44
44
  - !ruby/object:Gem::Version
45
- version: '0'
45
+ version: 2.10.0
46
46
  - !ruby/object:Gem::Dependency
47
47
  name: fakeweb
48
48
  requirement: !ruby/object:Gem::Requirement
49
49
  none: false
50
50
  requirements:
51
- - - ! '>='
51
+ - - ~>
52
52
  - !ruby/object:Gem::Version
53
- version: '0'
53
+ version: 1.3.0
54
54
  type: :development
55
55
  prerelease: false
56
56
  version_requirements: !ruby/object:Gem::Requirement
57
57
  none: false
58
58
  requirements:
59
- - - ! '>='
59
+ - - ~>
60
60
  - !ruby/object:Gem::Version
61
- version: '0'
61
+ version: 1.3.0
62
62
  - !ruby/object:Gem::Dependency
63
63
  name: json
64
64
  requirement: !ruby/object:Gem::Requirement
65
65
  none: false
66
66
  requirements:
67
- - - ! '>='
67
+ - - ~>
68
68
  - !ruby/object:Gem::Version
69
- version: '0'
69
+ version: 1.7.3
70
70
  type: :runtime
71
71
  prerelease: false
72
72
  version_requirements: !ruby/object:Gem::Requirement
73
73
  none: false
74
74
  requirements:
75
- - - ! '>='
75
+ - - ~>
76
76
  - !ruby/object:Gem::Version
77
- version: '0'
77
+ version: 1.7.3
78
78
  - !ruby/object:Gem::Dependency
79
79
  name: rest-client
80
80
  requirement: !ruby/object:Gem::Requirement
81
81
  none: false
82
82
  requirements:
83
- - - ! '>='
83
+ - - ~>
84
84
  - !ruby/object:Gem::Version
85
- version: '0'
85
+ version: 1.6.7
86
86
  type: :runtime
87
87
  prerelease: false
88
88
  version_requirements: !ruby/object:Gem::Requirement
89
89
  none: false
90
90
  requirements:
91
- - - ! '>='
91
+ - - ~>
92
92
  - !ruby/object:Gem::Version
93
- version: '0'
93
+ version: 1.6.7
94
94
  - !ruby/object:Gem::Dependency
95
95
  name: mime-types
96
96
  requirement: !ruby/object:Gem::Requirement
97
97
  none: false
98
98
  requirements:
99
- - - ! '>='
99
+ - - ~>
100
100
  - !ruby/object:Gem::Version
101
- version: '0'
101
+ version: '1.19'
102
102
  type: :runtime
103
103
  prerelease: false
104
104
  version_requirements: !ruby/object:Gem::Requirement
105
105
  none: false
106
106
  requirements:
107
- - - ! '>='
107
+ - - ~>
108
108
  - !ruby/object:Gem::Version
109
- version: '0'
110
- description: ! 'Qiniu Cloud Storage SDK for Ruby. See: http://docs.qiniutek.com/v1/sdk/ruby/'
109
+ version: '1.19'
110
+ - !ruby/object:Gem::Dependency
111
+ name: ruby-hmac
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ~>
116
+ - !ruby/object:Gem::Version
117
+ version: 0.4.0
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ~>
124
+ - !ruby/object:Gem::Version
125
+ version: 0.4.0
126
+ description: ! 'Qiniu Resource (Cloud) Storage SDK for Ruby. See: http://docs.qiniutek.com/v2/sdk/ruby/'
111
127
  email:
112
128
  - why404@gmail.com
113
129
  executables: []
@@ -155,7 +171,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
155
171
  version: '0'
156
172
  segments:
157
173
  - 0
158
- hash: -2090865148466765232
174
+ hash: -3989750708149586731
159
175
  required_rubygems_version: !ruby/object:Gem::Requirement
160
176
  none: false
161
177
  requirements:
@@ -164,13 +180,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
164
180
  version: '0'
165
181
  segments:
166
182
  - 0
167
- hash: -2090865148466765232
183
+ hash: -3989750708149586731
168
184
  requirements: []
169
185
  rubyforge_project:
170
186
  rubygems_version: 1.8.24
171
187
  signing_key:
172
188
  specification_version: 3
173
- summary: Qiniu Cloud Storage SDK for Ruby
189
+ summary: Qiniu Resource (Cloud) Storage SDK for Ruby
174
190
  test_files:
175
191
  - spec/qiniu/rs/auth_spec.rb
176
192
  - spec/qiniu/rs/image_spec.rb