Merge branch 'release/v2.0.1'

This commit is contained in:
404
2012-06-27 20:44:49 +08:00
12 changed files with 82 additions and 29 deletions
+1 -1
View File
@@ -1,4 +1,4 @@
source 'https://rubygems.org'
source 'http://ruby.taobao.org'
# Specify your gem's dependencies in qiniu-s3.gemspec
gemspec
+12 -3
View File
@@ -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, {}
+6 -2
View File
@@ -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
}
+23 -1
View File
@@ -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
+1 -1
View File
@@ -2,6 +2,6 @@
module Qiniu
module RS
VERSION = "1.1.0"
VERSION = "2.0.1"
end
end
+10 -9
View File
@@ -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
Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

+20 -9
View File
@@ -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
+2
View File
@@ -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
+2
View File
@@ -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
+3 -1
View File
@@ -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
+2 -2
View File
@@ -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