From ae0dbced1b0bc4a1858c1135471692e5852c23f5 Mon Sep 17 00:00:00 2001 From: Liang Tao Date: Mon, 10 Mar 2014 12:21:15 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E6=8E=88=E6=9D=83=E5=87=AD?= =?UTF-8?q?=E8=AF=81=E7=AE=97=E6=B3=95=E5=92=8C=E7=9B=B8=E5=85=B3=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E7=94=A8=E4=BE=8B=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/qiniu/auth.rb | 6 ++++-- lib/qiniu/management.rb | 10 +++++++++- lib/qiniu/utils.rb | 5 ++++- spec/qiniu/management_spec.rb | 12 +++++++----- 4 files changed, 24 insertions(+), 9 deletions(-) diff --git a/lib/qiniu/auth.rb b/lib/qiniu/auth.rb index 38b4813..867e4a3 100755 --- a/lib/qiniu/auth.rb +++ b/lib/qiniu/auth.rb @@ -10,11 +10,13 @@ module Qiniu include Utils def call_with_signature(url, data, retry_times = 0, options = {}) - http_request url, data, options.merge({:qbox_signature_token => generate_qbox_signature(url, data)}) + code, data = http_request url, data, options.merge({:qbox_signature_token => generate_qbox_signature(url, data, options[:mime])}) + [code, data] end def request(url, data = nil, options = {}) - Auth.call_with_signature(url, data, 0, options) + code, data = Auth.call_with_signature(url, data, 0, options) + [code, data] end end diff --git a/lib/qiniu/management.rb b/lib/qiniu/management.rb index ce8cc97..1d6d500 100755 --- a/lib/qiniu/management.rb +++ b/lib/qiniu/management.rb @@ -59,7 +59,7 @@ module Qiniu encoded_uri = encode_entry_uri(bucket, key) execs << "op=/#{command}/#{encoded_uri}" end - Auth.request Config.settings[:rs_host] + "/batch?" + execs.join("&") + Auth.request Config.settings[:rs_host] + "/batch", execs.join("&"), {:mime => "application/x-www-form-urlencoded" } end # batch def batch_get(bucket, keys) @@ -101,6 +101,14 @@ module Qiniu target_encoded_entry_uri = encode_entry_uri(target_bucket, target_key) %Q(/#{command}/#{source_encoded_entry_uri}/#{target_encoded_entry_uri}) end # _generate_cp_or_mv_opstr + + def _batch_cp_or_mv(command, *op_args) + execs = [] + op_args.each do |e| + execs << 'op=' + _generate_cp_or_mv_opstr(command, e[0], e[1], e[2], e[3]) if e.size == 4 + end + Auth.request Config.settings[:rs_host] + "/batch", execs.join("&"), {:mime => "application/x-www-form-urlencoded" } + end # _batch_cp_or_mv end end # module Storage end # module Qiniu diff --git a/lib/qiniu/utils.rb b/lib/qiniu/utils.rb index 290d3db..ba7a4d8 100755 --- a/lib/qiniu/utils.rb +++ b/lib/qiniu/utils.rb @@ -126,7 +126,7 @@ module Qiniu File.open(filepath, "rb") { |f| Zlib.crc32 f.read } end - def generate_qbox_signature(url, params) + def generate_qbox_signature(url, params, mime = nil) access_key = Config.settings[:access_key] secret_key = Config.settings[:secret_key] uri = URI.parse(url) @@ -138,6 +138,9 @@ module Qiniu params_string = generate_query_string(params) signature += params_string end + if mime.is_a?(String) && mime == "application/x-www-form-urlencoded" && params.is_a?(String) + signature += params + end hmac = HMAC::SHA1.new(secret_key) hmac.update(signature) encoded_digest = urlsafe_base64_encode(hmac.digest) diff --git a/spec/qiniu/management_spec.rb b/spec/qiniu/management_spec.rb index a4a77b7..383c2c8 100755 --- a/spec/qiniu/management_spec.rb +++ b/spec/qiniu/management_spec.rb @@ -82,22 +82,25 @@ module Qiniu end end -=begin context ".batch_copy" do it "should works" do - code, data = Qiniu.batch_copy [@bucket, @key, @bucket, @key2] + code, data = Storage.batch_copy @bucket, @key, @bucket, @key2 code.should == 200 puts data.inspect #code2, data2 = Qiniu.stat(@bucket, @key2) #code2.should == 200 #puts data2.inspect + + code, data = Storage.delete @bucket, @key2 + code.should == 200 + puts data.inspect end end context ".batch_move" do it "should works" do - code, data = Qiniu.batch_move [@bucket, @key, @bucket, @key2] + code, data = Storage.batch_move @bucket, @key, @bucket, @key2 code.should == 200 puts data.inspect @@ -105,12 +108,11 @@ module Qiniu #code2.should == 200 #puts data2.inspect - code3, data3 = Qiniu.batch_move [@bucket, @key2, @bucket, @key] + code3, data3 = Storage.batch_move @bucket, @key2, @bucket, @key code3.should == 200 puts data3.inspect end end -=end =begin context ".publish" do