diff --git a/lib/qiniu/rs.rb b/lib/qiniu/rs.rb index 16250a9..0538e6b 100755 --- a/lib/qiniu/rs.rb +++ b/lib/qiniu/rs.rb @@ -66,6 +66,18 @@ module Qiniu code == StatusOK end + def upload_with_token opts = {} + code, data = IO.upload_with_token(opts[:uptoken], + opts[:file], + opts[:bucket], + opts[:key], + opts[:mime_type], + opts[:note], + opts[:callback_params], + opts[:enable_crc32_check]) + code == StatusOK ? data : false + end + def stat(bucket, key) code, data = RS.stat(bucket, key) code == StatusOK ? data : false @@ -152,10 +164,6 @@ module Qiniu code == StatusOK ? data : false end - #def generate_upload_token(scope, expires_in, callback_url = nil, return_url = nil) - # Utils.generate_upload_token(scope, expires_in, callback_url, return_url) - #end - def generate_upload_token(opts = {}) token_obj = UploadToken.new(opts) token_obj.access_key = Config.settings[:access_key] diff --git a/lib/qiniu/rs/config.rb b/lib/qiniu/rs/config.rb index 8a03a8e..02855db 100755 --- a/lib/qiniu/rs/config.rb +++ b/lib/qiniu/rs/config.rb @@ -22,6 +22,7 @@ module Qiniu :auth_url => "https://acc.qbox.me/oauth2/token", :rs_host => "http://rs.qbox.me:10100", :io_host => "http://iovip.qbox.me", + :up_host => "http://m1.qbox.me:13019", :client_id => "a75604760c4da4caaa456c0c5895c061c3065c5a", :client_secret => "75df554a39f58accb7eb293b550fa59618674b7d", :access_key => "", diff --git a/lib/qiniu/rs/io.rb b/lib/qiniu/rs/io.rb index 8eb5646..36d05cc 100755 --- a/lib/qiniu/rs/io.rb +++ b/lib/qiniu/rs/io.rb @@ -34,6 +34,14 @@ module Qiniu Auth.request url, ::IO.read(local_file), options end + def upload_with_token(uptoken, local_file, bucket, key = nil, mime_type = nil, custom_meta = nil, callback_params = nil, enable_crc32_check = false) + action_params = _generate_action_params(local_file, bucket, key, mime_type, custom_meta, enable_crc32_check) + callback_params = {:bucket => bucket, :key => key, :mime_type => mime_type} if callback_params.nil? + callback_query_string = Utils.generate_query_string(callback_params) + url = Config.settings[:up_host] + '/upload' + Utils.upload_multipart_data(url, local_file, action_params, callback_query_string, uptoken) + end + private def _generate_action_params(local_file, bucket, key = nil, mime_type = nil, custom_meta = nil, enable_crc32_check = false) raise NoSuchFileError, local_file unless File.exist?(local_file) diff --git a/lib/qiniu/rs/utils.rb b/lib/qiniu/rs/utils.rb index dc7a5e4..27bf9ef 100755 --- a/lib/qiniu/rs/utils.rb +++ b/lib/qiniu/rs/utils.rb @@ -91,13 +91,14 @@ module Qiniu end end - def upload_multipart_data(url, filepath, action_string, callback_query_string = '') + def upload_multipart_data(url, filepath, action_string, callback_query_string = '', uptoken = nil) post_data = { :params => callback_query_string, :action => action_string, :file => File.new(filepath, 'rb'), :multipart => true } + post_data[:auth] = uptoken unless uptoken.nil? http_request url, post_data end @@ -129,21 +130,6 @@ module Qiniu %Q(#{access_key}:#{encoded_digest}) end -=begin - def generate_upload_token(scope, expires_in, callback_url = nil, return_url = nil) - access_key = Config.settings[:access_key] - secret_key = Config.settings[:secret_key] - params = {:scope => scope, :deadline => Time.now.to_i + expires_in} - params[:callbackUrl] = callback_url if !callback_url.nil? && !calback_url.empty? - params[:returnUrl] = return_url if !return_url.nil? && !return_url.empty? - signature = urlsafe_base64_encode(params.to_json) - hmac = HMAC::SHA1.new(secret_key) - hmac.update(signature) - encoded_digest = urlsafe_base64_encode(hmac.digest) - %Q(#{access_key}:#{encoded_digest}:#{signature}) - end -=end - end end end diff --git a/lib/qiniu/rs/version.rb b/lib/qiniu/rs/version.rb index e72d26a..7d50abe 100755 --- a/lib/qiniu/rs/version.rb +++ b/lib/qiniu/rs/version.rb @@ -2,6 +2,6 @@ module Qiniu module RS - VERSION = "2.3.3" + VERSION = "2.4.0" end end diff --git a/spec/qiniu/rs/io_spec.rb b/spec/qiniu/rs/io_spec.rb index fd88969..1c6c02e 100755 --- a/spec/qiniu/rs/io_spec.rb +++ b/spec/qiniu/rs/io_spec.rb @@ -44,6 +44,16 @@ module Qiniu end end + context ".upload_with_token" do + it "should works" do + upopts = {:scope => @bucket, :expires_in => 3600, :callback_url => "http://localhost:4567"} + uptoken = Qiniu::Auth.generate_upload_token(upopts) + code, data = Qiniu::RS::IO.upload_with_token(uptoken, __FILE__, @bucket, @key, nil, nil, nil, true) + code.should == 200 + puts data.inspect + end + end + end end end diff --git a/spec/qiniu/rs_spec.rb b/spec/qiniu/rs_spec.rb index 471c72f..ccfe8f5 100755 --- a/spec/qiniu/rs_spec.rb +++ b/spec/qiniu/rs_spec.rb @@ -71,6 +71,22 @@ module Qiniu end end + context ".upload_with_token" do + it "should works" do + uptoken_opts = {:scope => @bucket, :expires_in => 3600, :callback_url => "http://localhost:4567"} + upload_opts = { + :uptoken => Qiniu::RS.generate_upload_token(uptoken_opts), + :file => __FILE__, + :bucket => @bucket, + :key => @key, + :enable_crc32_check => true + } + result = Qiniu::RS.upload_with_token(upload_opts) + result.should_not be_false + puts result.inspect + end + end + context ".stat" do it "should works" do result = Qiniu::RS.stat(@bucket, @key) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index f4cc93b..d46ba4c 100755 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -6,15 +6,14 @@ require 'rspec' RSpec.configure do |config| config.before :all do - Qiniu::RS.establish_connection! :access_key => "3fPHl_SLkPXdioqI_A8_NGngPWVJhlDk2ktRjogH", - :secret_key => "bXTPMDJrVYRJUiSDRFtFYwycVD_mjXxYWrCYlDHy" +# Qiniu::RS.establish_connection! :access_key => "3fPHl_SLkPXdioqI_A8_NGngPWVJhlDk2ktRjogH", +# :secret_key => "bXTPMDJrVYRJUiSDRFtFYwycVD_mjXxYWrCYlDHy" -=begin Qiniu::RS.establish_connection! :access_key => "bE21M6FW9V7zAFrBY5psgKOKJQLiBj12qMWTpc57", :secret_key => "uMo7Nyq_eDK_CuQ8_FYCxoTHQZqjiaPh-cbiKO7L", :auth_url => "http://m1.qbox.me:13001/oauth2/token", :rs_host => "http://m1.qbox.me:13003", - :io_host => "http://m1.qbox.me:13004" -=end + :io_host => "http://m1.qbox.me:13004", + :up_host => "http://m1.qbox.me:13019" end end