diff --git a/Gemfile b/Gemfile index ec8c558..385e48d 100755 --- a/Gemfile +++ b/Gemfile @@ -1,4 +1,4 @@ -source 'https://rubygems.org' +source 'http://ruby.taobao.org' # Specify your gem's dependencies in qiniu-s3.gemspec gemspec diff --git a/lib/qiniu/rs/auth.rb b/lib/qiniu/rs/auth.rb index e7b2ca9..ff7aca8 100755 --- a/lib/qiniu/rs/auth.rb +++ b/lib/qiniu/rs/auth.rb @@ -38,7 +38,7 @@ module Qiniu @refresh_token = refresh_token end - def call(url, data, retry_times = 0) + def call_with_logged_in(url, data, retry_times = 0) raise MissingAccessToken if @access_token.nil? code, data = http_request url, data, {:access_token => @access_token} if code == 401 @@ -51,16 +51,25 @@ module Qiniu if code == 200 retry_times += 1 if Config.settings[:auto_reconnect] && retry_times < Config.settings[:max_retry_times] - return call(url, data, retry_times) + return call_with_logged_in(url, data, retry_times) end end end [code, data] end + def call_with_signature(url, data, retry_times = 0) + code, data = http_request url, data, {:signature_auth => true} + [code, data] + end + def request(url, data = nil) begin - code, data = Auth.call(url, data) + if Config.settings[:access_key].empty? || Config.settings[:secret_key].empty? + code, data = Auth.call_with_logged_in(url, data) + else + code, data = Auth.call_with_signature(url, data) + end rescue [MissingAccessToken, MissingRefreshToken, MissingUsernameOrPassword] => e Log.logger.error e code, data = 401, {} diff --git a/lib/qiniu/rs/config.rb b/lib/qiniu/rs/config.rb index 7b1f6e4..28e5a73 100755 --- a/lib/qiniu/rs/config.rb +++ b/lib/qiniu/rs/config.rb @@ -21,9 +21,13 @@ module Qiniu :content_type => 'application/x-www-form-urlencoded', :auth_url => "https://acc.qbox.me/oauth2/token", :rs_host => "http://rs.qbox.me:10100", + #:rs_host => "http://localhost:10100", :io_host => "http://iovip.qbox.me", - :client_id => "", - :client_secret => "", + #:io_host => "http://localhost:10200", + :client_id => "a75604760c4da4caaa456c0c5895c061c3065c5a", + :client_secret => "75df554a39f58accb7eb293b550fa59618674b7d", + :access_key => "", + :secret_key => "", :auto_reconnect => true, :max_retry_times => 5 } diff --git a/lib/qiniu/rs/utils.rb b/lib/qiniu/rs/utils.rb index 0009cde..0175524 100755 --- a/lib/qiniu/rs/utils.rb +++ b/lib/qiniu/rs/utils.rb @@ -5,6 +5,7 @@ require 'json' require 'zlib' require 'base64' require 'rest_client' +require 'hmac-sha1' require 'qiniu/rs/exceptions' module Qiniu @@ -37,7 +38,12 @@ module Qiniu :accept => :json, :user_agent => Config.settings[:user_agent] } - header_options.merge!('Authorization' => "Bearer #{options[:access_token]}") if options[:access_token] + if options[:signature_auth] && options[:signature_auth] == true + signature_token = generate_qbox_signature(Config.settings[:access_key], Config.settings[:secret_key], url, data) + header_options.merge!('Authorization' => "QBox #{signature_token}") + elsif options[:access_token] + header_options.merge!('Authorization' => "Bearer #{options[:access_token]}") + end case options[:method] when :get response = RestClient.get url, header_options @@ -124,6 +130,22 @@ module Qiniu File.open(filepath, "rb") { |f| Zlib.crc32 f.read } end + def generate_qbox_signature(access_key, secret_key, url, params) + uri = URI.parse(url) + signature = uri.path + query_string = uri.query + signature += '?' + query_string if !query_string.nil? && !query_string.empty? + signature += "\n"; + if params.is_a?(Hash) + total_param = params.map { |key, value| key.to_s+"="+value.to_s } + signature += total_param.join("&") + end + hmac = HMAC::SHA1.new(secret_key) + hmac.update(signature) + encoded_digest = urlsafe_base64_encode(hmac.digest) + %Q(#{access_key}:#{encoded_digest}) + end + end end end diff --git a/lib/qiniu/rs/version.rb b/lib/qiniu/rs/version.rb index 4a7eaf0..d72d0e4 100755 --- a/lib/qiniu/rs/version.rb +++ b/lib/qiniu/rs/version.rb @@ -2,6 +2,6 @@ module Qiniu module RS - VERSION = "1.1.0" + VERSION = "2.0.1" end end diff --git a/qiniu-rs.gemspec b/qiniu-rs.gemspec index 5844ef0..ef1bb5f 100755 --- a/qiniu-rs.gemspec +++ b/qiniu-rs.gemspec @@ -5,8 +5,8 @@ require File.expand_path('../lib/qiniu/rs/version', __FILE__) Gem::Specification.new do |gem| gem.authors = ["why404"] gem.email = ["why404@gmail.com"] - gem.description = %q{Qiniu Cloud Storage SDK for Ruby. See: http://docs.qiniutek.com/v1/sdk/ruby/} - gem.summary = %q{Qiniu Cloud Storage SDK for Ruby} + gem.description = %q{Qiniu Resource (Cloud) Storage SDK for Ruby. See: http://docs.qiniutek.com/v2/sdk/ruby/} + gem.summary = %q{Qiniu Resource (Cloud) Storage SDK for Ruby} gem.homepage = "https://github.com/why404/qiniu-rs" gem.files = `git ls-files`.split($\) @@ -17,11 +17,12 @@ Gem::Specification.new do |gem| gem.version = Qiniu::RS::VERSION # specify any dependencies here; for example: - gem.add_development_dependency "rake" - gem.add_development_dependency "rspec" - gem.add_development_dependency "fakeweb" - gem.add_runtime_dependency "json" - gem.add_runtime_dependency "rest-client" - gem.add_runtime_dependency "mime-types" - gem.add_runtime_dependency "jruby-openssl" if RUBY_PLATFORM == "java" + gem.add_development_dependency "rake", "~> 0.9.2.2" + gem.add_development_dependency "rspec", "~> 2.10.0" + gem.add_development_dependency "fakeweb", "~> 1.3.0" + gem.add_runtime_dependency "json", "~> 1.7.3" + gem.add_runtime_dependency "rest-client", "~> 1.6.7" + gem.add_runtime_dependency "mime-types", "~> 1.19" + gem.add_runtime_dependency "ruby-hmac", "~> 0.4.0" + gem.add_runtime_dependency "jruby-openssl", "~> 0.7.7" if RUBY_PLATFORM == "java" end diff --git a/spec/qiniu/rs/image_logo_for_test.png b/spec/qiniu/rs/image_logo_for_test.png new file mode 100644 index 0000000..571c069 Binary files /dev/null and b/spec/qiniu/rs/image_logo_for_test.png differ diff --git a/spec/qiniu/rs/image_spec.rb b/spec/qiniu/rs/image_spec.rb index dc96723..9075040 100755 --- a/spec/qiniu/rs/image_spec.rb +++ b/spec/qiniu/rs/image_spec.rb @@ -2,14 +2,15 @@ require 'spec_helper' require 'qiniu/rs/auth' -require 'qiniu/rs/rs' +require 'qiniu/rs' require 'qiniu/rs/image' module Qiniu module RS describe Image do - before :all do + before :each do +=begin code, data = Qiniu::RS::Auth.exchange_by_password!("test@qbox.net", "test") code.should == 200 data.should be_an_instance_of(Hash) @@ -17,20 +18,30 @@ module Qiniu data["refresh_token"].should_not be_empty data["refresh_token"].should_not be_empty puts data.inspect +=end @bucket = "test_images" - @key = "test_image.jpg" - code2, data2 = Qiniu::RS::RS.get(@bucket, @key) - code2.should == 200 - data2["url"].should_not be_empty - puts data2.inspect + @key = "image_logo_for_test.png" - @download_url = data2["url"] + local_file = File.expand_path('../' + @key, __FILE__) + + put_url = Qiniu::RS.put_auth(10) + put_url.should_not be_false + put_url.should_not be_empty + result = Qiniu::RS.upload :url => put_url, + :file => local_file, + :bucket => @bucket, + :key => @key, + :enable_crc32_check => true + result.should be_true end context ".info" do it "should works" do - code, data = Qiniu::RS::Image.info(@download_url) + result = Qiniu::RS.get(@bucket, @key) + result["url"].should_not be_empty + puts result.inspect + code, data = Qiniu::RS::Image.info(result["url"]) code.should == 200 puts data.inspect end diff --git a/spec/qiniu/rs/io_spec.rb b/spec/qiniu/rs/io_spec.rb index 7e4b042..0563ca8 100755 --- a/spec/qiniu/rs/io_spec.rb +++ b/spec/qiniu/rs/io_spec.rb @@ -10,6 +10,7 @@ module Qiniu describe IO do before :all do +=begin code, data = Qiniu::RS::Auth.exchange_by_password!("test@qbox.net", "test") code.should == 200 data.should be_an_instance_of(Hash) @@ -17,6 +18,7 @@ module Qiniu data["refresh_token"].should_not be_empty data["refresh_token"].should_not be_empty puts data.inspect +=end code2, data2 = Qiniu::RS::IO.put_auth() code2.should == 200 diff --git a/spec/qiniu/rs/rs_spec.rb b/spec/qiniu/rs/rs_spec.rb index f48ba29..960ab85 100755 --- a/spec/qiniu/rs/rs_spec.rb +++ b/spec/qiniu/rs/rs_spec.rb @@ -11,6 +11,7 @@ module Qiniu describe RS do before :all do +=begin code, data = Qiniu::RS::Auth.exchange_by_password!("test@qbox.net", "test") code.should == 200 data.should be_an_instance_of(Hash) @@ -18,6 +19,7 @@ module Qiniu data["refresh_token"].should_not be_empty data["refresh_token"].should_not be_empty puts data.inspect +=end code2, data2 = Qiniu::RS::IO.put_auth() code2.should == 200 diff --git a/spec/qiniu/rs_spec.rb b/spec/qiniu/rs_spec.rb index 2ba7500..61ac14e 100755 --- a/spec/qiniu/rs_spec.rb +++ b/spec/qiniu/rs_spec.rb @@ -12,12 +12,14 @@ module Qiniu @domain = 'cdn.example.com' end +=begin context ".login!" do it "should works" do result = Qiniu::RS.login!("test@qbox.net", "test") result.should be_true end end +=end context ".put_auth" do it "should works" do @@ -136,7 +138,7 @@ module Qiniu context ".image_info" do it "should works" do - data = Qiniu::RS.get("test_images", "test_image.jpg") + data = Qiniu::RS.get("test_images", "image_logo_for_test.png") data.should_not be_false data.should_not be_empty puts data.inspect diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index a025c73..4161bd4 100755 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -6,7 +6,7 @@ require 'rspec' RSpec.configure do |config| config.before :all do - Qiniu::RS.establish_connection! :client_id => "abcd0c7edcdf914228ed8aa7c6cee2f2bc6155e2", - :client_secret => "fc9ef8b171a74e197b17f85ba23799860ddf3b9c" + Qiniu::RS.establish_connection! :access_key => "3fPHl_SLkPXdioqI_A8_NGngPWVJhlDk2ktRjogH", + :secret_key => "bXTPMDJrVYRJUiSDRFtFYwycVD_mjXxYWrCYlDHy" end end