From cb748d8857f844a0e2ce07b1cdadf3e05deeab52 Mon Sep 17 00:00:00 2001 From: Liang Tao Date: Sat, 8 Feb 2014 13:00:07 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=9C=80=E6=96=B0=E7=9A=84?= =?UTF-8?q?=E5=8D=95=E6=96=87=E4=BB=B6=E7=9B=B4=E4=BC=A0=E5=AE=9E=E7=8E=B0?= =?UTF-8?q?=EF=BC=9AIO.upload=5Fwith=5Ftoken=5F2=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/qiniu/rs/io.rb | 25 +++++++++++++++++++++++++ spec/qiniu/rs/io_spec.rb | 12 ++++++++++++ 2 files changed, 37 insertions(+) diff --git a/lib/qiniu/rs/io.rb b/lib/qiniu/rs/io.rb index 8c7739d..934d027 100755 --- a/lib/qiniu/rs/io.rb +++ b/lib/qiniu/rs/io.rb @@ -25,6 +25,31 @@ module Qiniu Utils.upload_multipart_data(url, local_file, action_params, callback_query_string, uptoken) end + def upload_with_token_2(uptoken, local_file, key = nil, x_vars = nil) + ### 构造URL + url = Config.settings[:up_host] + url[/\/*$/] = '' + url += '/' + + ### 构造HTTP Body + post_data = { + :file => File.new(local_file, 'rb'), + :multipart => true, + } + if not uptoken.nil? then + post_data[:token] = uptoken + end + if not key.nil? then + post_data[:key] = key + end + if x_vars.is_a?(Hash) then + post_data.merge!(x_vars) + end + + ### 发送请求 + Utils.http_request url, post_data + end # upload_with_token_2 + private def _generate_action_params(local_file, bucket, key = nil, mime_type = nil, custom_meta = nil, enable_crc32_check = false, rotate = nil) raise NoSuchFileError, local_file unless File.exist?(local_file) diff --git a/spec/qiniu/rs/io_spec.rb b/spec/qiniu/rs/io_spec.rb index 3b0f7bb..1db2555 100755 --- a/spec/qiniu/rs/io_spec.rb +++ b/spec/qiniu/rs/io_spec.rb @@ -42,6 +42,18 @@ module Qiniu end end + context ".upload_with_token_2" do + it "should works" do + upopts = {:scope => @bucket, :expires_in => 3600, :endUser => "why404@gmail.com"} + uptoken = Qiniu::RS.generate_upload_token(upopts) + + code, data = Qiniu::RS::IO.upload_with_token_2(uptoken, __FILE__, @key) + + code.should == 200 + puts data.inspect + end + end # .upload_with_token_2 + end end end