From 3753e759ddc0a2104f17a42ab1514c24a30bf934 Mon Sep 17 00:00:00 2001 From: Liang Tao Date: Mon, 24 Mar 2014 14:55:57 +0800 Subject: [PATCH 01/13] =?UTF-8?q?=E6=B7=BB=E5=8A=A0authorize=5Fdownload=5F?= =?UTF-8?q?url()=EF=BC=8C=E7=94=A8=E4=BA=8E=E5=AF=B9=E7=A7=81=E6=9C=89?= =?UTF-8?q?=E8=B5=84=E6=BA=90=E4=B8=8B=E8=BD=BDURL=E8=BF=9B=E8=A1=8C?= =?UTF-8?q?=E6=8E=88=E6=9D=83=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/qiniu/auth.rb | 51 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/lib/qiniu/auth.rb b/lib/qiniu/auth.rb index 867e4a3..1f5d59a 100755 --- a/lib/qiniu/auth.rb +++ b/lib/qiniu/auth.rb @@ -1,5 +1,7 @@ # -*- encoding: utf-8 -*- +require 'hmac-sha1' + require 'qiniu/exceptions' module Qiniu @@ -19,7 +21,54 @@ module Qiniu [code, data] end - end + EMPTY_ARGS = {} + + ### 生成下载授权URL + def authorize_download_url(url, args = EMPTY_ARGS) + ### 提取AK/SK信息 + ak = args[:access_key] + if ak.nil? then + ak = Config.settings[:access_key] + end + + sk = args[:access_key] + if sk.nil? then + sk = Config.settings[:secret_key] + end + + ### 授权期计算 + if args[:expires].is_a?(Integer) && args[:expires] > 0 then + # 指定相对时间,单位:秒 + e = Time.now.to_i + args[:expires] + elsif args[:deadline].is_a?(Integer) then + # 指定绝对时间,常用于调试和单元测试 + e = args[:deadline] + else + # 默认授权期1小时 + e = Time.now.to_i + 3600 + end + + ### URL变换:追加授权期参数 + if url.index('?').is_a?(Fixnum) then + # 已有参数 + download_url = "#{url}&e=#{e}" + else + # 尚无参数 + download_url = "#{url}?e=#{e}" + end + + ### 生成数字签名 + sign = HMAC::SHA1.new(secret_key).update(download_url) + encoded_sign = Utils.urlsafe_base64_encode(sign) + + ### 生成下载授权凭证 + dntoken = "#{access_key}:#{encoded_sign}" + + ### 返回下载授权URL及相关参数 + return "#{download_url}&token=#{dntoken}", e, dntoken + end # authorize_download_url + + end # class << self end # module Auth end # module Qiniu From 476937bc6099b70e8901177945cad92251fcdc69 Mon Sep 17 00:00:00 2001 From: Liang Tao Date: Wed, 26 Mar 2014 14:40:33 +0800 Subject: [PATCH 02/13] =?UTF-8?q?=E5=8E=BB=E9=99=A4=E5=AF=B9ak/sk=E7=9A=84?= =?UTF-8?q?=E4=BC=A0=E5=8F=82=EF=BC=8C=E4=BB=8D=E7=84=B6=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E5=85=A8=E5=B1=80=E8=AE=BE=E5=AE=9A=E5=80=BC=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/qiniu/auth.rb | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/lib/qiniu/auth.rb b/lib/qiniu/auth.rb index 8f7a86e..c286bfe 100755 --- a/lib/qiniu/auth.rb +++ b/lib/qiniu/auth.rb @@ -26,15 +26,8 @@ module Qiniu ### 生成下载授权URL def authorize_download_url(url, args = EMPTY_ARGS) ### 提取AK/SK信息 - access_key = args[:access_key] - if access_key.nil? then - access_key = Config.settings[:access_key] - end - - secret_key = args[:secret_key] - if secret_key.nil? then - secret_key = Config.settings[:secret_key] - end + access_key = Config.settings[:access_key] + secret_key = Config.settings[:secret_key] ### 授权期计算 if args[:expires].is_a?(Integer) && args[:expires] > 0 then From 17c961da1c3ab4ada976b26d76dcc210e8c6a798 Mon Sep 17 00:00:00 2001 From: Liang Tao Date: Wed, 26 Mar 2014 15:03:28 +0800 Subject: [PATCH 03/13] =?UTF-8?q?=E6=94=B9=E4=BD=BF=E7=94=A8/mkbucket2?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E6=9D=A5=E5=88=9B=E5=BB=BABucket=EF=BC=8C?= =?UTF-8?q?=E9=81=BF=E5=85=8D=E6=89=8B=E5=8A=A8=E7=BB=91=E5=AE=9A=E5=9F=9F?= =?UTF-8?q?=E5=90=8D=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/qiniu/management.rb | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/qiniu/management.rb b/lib/qiniu/management.rb index 1d6d500..6797da2 100755 --- a/lib/qiniu/management.rb +++ b/lib/qiniu/management.rb @@ -9,10 +9,21 @@ module Qiniu Auth.request Config.settings[:rs_host] + '/buckets' end # buckets - def mkbucket(bucket_name) - Auth.request Config.settings[:rs_host] + '/mkbucket/' + bucket_name + PRIVATE_BUCKET = 0 + PUBLIC_BUCKET = 1 + + def mkbucket(bucket_name, is_public = PUBLIC_BUCKET) + Auth.request Config.settings[:rs_host] + '/mkbucket2/' + bucket_name + '/public/' + is_public.to_s end # mkbucket + def make_a_private_bucket(bucket_name) + return mkbucket(bucket_name, PRIVATE_BUCKET) + end # make_a_private_bucket + + def make_a_public_bucket(bucket_name) + return mkbucket(bucket_name, PUBLIC_BUCKET) + end # make_a_public_bucket + def stat(bucket, key) Auth.request Config.settings[:rs_host] + '/stat/' + encode_entry_uri(bucket, key) end # stat From fc5afc332a8e3802fee721a9640269203fba01ee Mon Sep 17 00:00:00 2001 From: Liang Tao Date: Thu, 27 Mar 2014 11:17:54 +0800 Subject: [PATCH 04/13] =?UTF-8?q?=E4=B8=BAauthorize=5Fdownload=5Furl?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=B5=8B=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 | 4 +-- spec/qiniu/auth_spec.rb | 78 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 2 deletions(-) create mode 100755 spec/qiniu/auth_spec.rb diff --git a/lib/qiniu/auth.rb b/lib/qiniu/auth.rb index c286bfe..19aeba1 100755 --- a/lib/qiniu/auth.rb +++ b/lib/qiniu/auth.rb @@ -57,8 +57,8 @@ module Qiniu ### 生成下载授权凭证 dntoken = "#{access_key}:#{encoded_sign}" - ### 返回下载授权URL及相关参数 - return "#{download_url}&token=#{dntoken}", e, dntoken + ### 返回下载授权URL + return "#{download_url}&token=#{dntoken}" end # authorize_download_url end # class << self diff --git a/spec/qiniu/auth_spec.rb b/spec/qiniu/auth_spec.rb new file mode 100755 index 0000000..0054e4f --- /dev/null +++ b/spec/qiniu/auth_spec.rb @@ -0,0 +1,78 @@ +# -*- encoding: utf-8 -*- + +require 'spec_helper' +require 'qiniu/auth' +require 'qiniu/storage' +require 'digest/sha1' + +module Qiniu + module Auth + describe Auth do + + before :all do + @bucket = 'RubySDK-Test-Private' + @bucket = make_unique_bucket(@bucket) + + ### 尝试创建Bucket + result = Qiniu::Storage.make_a_private_bucket(@bucket) + puts result.inspect + end + + after :all do + ### 不删除Bucket以备下次使用 + end + + ### 测试私有资源下载 + context ".download_private_file" do + it "should works" do + ### 生成Key + key = 'a_private_file' + key = make_unique_key_in_bucket(key) + puts "key=#{key}" + + ### 上传测试文件 + upopts = {:scope => @bucket, :expires_in => 3600, :endUser => "why404@gmail.com"} + uptoken = Qiniu.generate_upload_token(upopts) + + code, data, raw_headers = Qiniu::Storage.upload_with_token_2( + uptoken, + __FILE__, + key + ) + code.should == 200 + puts data.inspect + puts raw_headers.inspect + + ### 获取下载地址 + code, data = Qiniu::Storage.get(@bucket, key) + code.should == 200 + puts data.inspect + + url = data['url'] + + ### 授权下载地址(不带参数) + download_url = Qiniu::Auth.authorize_download_url(url) + puts "download_url=#{download_url}" + + result = RestClient.get(download_url) + result.code.should == 200 + result.body.should_not be_empty + + ### 授权下载地址(带参数) + download_url = Qiniu::Auth.authorize_download_url(url + '?download/a.m3u8') + puts "download_url=#{download_url}" + + result = RestClient.get(download_url) + result.code.should == 200 + result.body.should_not be_empty + + ### 删除文件 + code, data = Qiniu::Storage.delete(@bucket, key) + code.should == 200 + puts data.inspect + end + end + + end + end # module Storage +end # module Qiniu From b99ff384ef576533a1f20b34b7d557c44d1e119d Mon Sep 17 00:00:00 2001 From: Liang Tao Date: Thu, 27 Mar 2014 13:34:58 +0800 Subject: [PATCH 05/13] =?UTF-8?q?=E5=B0=86Utils.generate=5Fqbox=5Fsignatur?= =?UTF-8?q?e()=E7=A7=BB=E8=87=B3Auth.generate=5Facctoken()=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/qiniu/auth.rb | 44 ++++++++++++++++++++++++++++++++++++++++++-- lib/qiniu/utils.rb | 21 --------------------- 2 files changed, 42 insertions(+), 23 deletions(-) diff --git a/lib/qiniu/auth.rb b/lib/qiniu/auth.rb index 19aeba1..04547d3 100755 --- a/lib/qiniu/auth.rb +++ b/lib/qiniu/auth.rb @@ -1,6 +1,8 @@ # -*- encoding: utf-8 -*- +# vim: sw=2 ts=2 require 'hmac-sha1' +require 'uri' require 'qiniu/exceptions' @@ -12,8 +14,11 @@ module Qiniu include Utils def call_with_signature(url, data, retry_times = 0, options = {}) - code, data, raw_headers = http_request url, data, options.merge({:qbox_signature_token => generate_qbox_signature(url, data, options[:mime])}) - [code, data, raw_headers] + return Utils.http_request( + url, + data, + options.merge({:qbox_signature_token => generate_acctoken(url, data)}) + ) end # call_with_signature def request(url, data = nil, options = {}) @@ -61,6 +66,41 @@ module Qiniu return "#{download_url}&token=#{dntoken}" end # authorize_download_url + def generate_acctoken(url, body = '') + ### 提取AK/SK信息 + access_key = Config.settings[:access_key] + secret_key = Config.settings[:secret_key] + + ### 解析URL,生成待签名字符串 + uri = URI.parse(url) + signing_str = uri.path + + # 如有QueryString部分,则需要加上 + query_string = uri.query + if query_string.is_a?(String) && !query_string.empty? + singin_str += '?' + query_string + end + + # 追加换行符 + signing_str += "\n" + + # 如果有Body,则也加上 + # (仅限于mime == "application/x-www-form-urlencoded"的情况) + if body.is_a?(String) && !body.empty? + signing_str += body + end + + ### 生成数字签名 + sign = HMAC::SHA1.new(secret_key).update(signing_str).digest + encoded_sign = Utils.urlsafe_base64_encode(sign) + + ### 生成管理授权凭证 + acctoken = "#{access_key}:#{encoded_sign}" + + ### 返回管理授权凭证 + return acctoken + end # generate_acctoken + end # class << self end # module Auth diff --git a/lib/qiniu/utils.rb b/lib/qiniu/utils.rb index 3dd3005..4b1f024 100755 --- a/lib/qiniu/utils.rb +++ b/lib/qiniu/utils.rb @@ -128,26 +128,5 @@ module Qiniu File.open(filepath, "rb") { |f| Zlib.crc32 f.read } end - def generate_qbox_signature(url, params, mime = nil) - access_key = Config.settings[:access_key] - secret_key = Config.settings[:secret_key] - 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) - 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) - %Q(#{access_key}:#{encoded_digest}) - end - end # module Utils end # module Qiniu From e2b3162f1fac2aca8d859f1192ca262363f3b905 Mon Sep 17 00:00:00 2001 From: Liang Tao Date: Fri, 28 Mar 2014 11:36:58 +0800 Subject: [PATCH 06/13] =?UTF-8?q?=E6=B7=BB=E5=8A=A0Auth.generate=5Fuptoken?= =?UTF-8?q?()=E6=96=B9=E6=B3=95=EF=BC=8C=E5=8E=98=E6=B8=85=E4=B8=8A?= =?UTF-8?q?=E4=BC=A0=E6=8E=88=E6=9D=83=E5=87=AD=E8=AF=81=E7=AD=BE=E5=8F=91?= =?UTF-8?q?=E7=AE=97=E6=B3=95=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/qiniu/auth.rb | 122 ++++++++++++++++++++++++++++++++++++++ lib/qiniu/upload.rb | 22 +++++++ spec/qiniu/upload_spec.rb | 44 ++++++++++++++ 3 files changed, 188 insertions(+) diff --git a/lib/qiniu/auth.rb b/lib/qiniu/auth.rb index 04547d3..758838a 100755 --- a/lib/qiniu/auth.rb +++ b/lib/qiniu/auth.rb @@ -8,6 +8,113 @@ require 'qiniu/exceptions' module Qiniu module Auth + class << self + def calculate_deadline(expires_in, deadline = nil) + ### 授权期计算 + if expires_in.is_a?(Integer) && expires_in > 0 then + # 指定相对时间,单位:秒 + return Time.now.to_i + expires_in + elsif deadline.is_a?(Integer) then + # 指定绝对时间,常用于调试和单元测试 + return deadline + end + + # 默认授权期1小时 + return Time.now.to_i + 3600 + end # calculate_deadline + end # class << self + + class PutPolicy + private + def initialize(bucket, key = nil, expires_in = 3600, deadline = nil) + ### 设定scope参数(必填项目) + self.scope!(bucket, key) + + ### 设定deadline参数(必填项目) + @expires_in = expires_in + @deadline = Auth.calculate_deadline(expires_in, deadline) + end # initialize + + PARAMS = { + :scope => "scope" , + :save_key => "saveKey" , + :end_user => "endUser" , + :return_url => "returnUrl" , + :return_body => "returnBody" , + :callback_url => "callbackUrl" , + :callback_body => "callbackBody" , + :persistent_ops => "persistentOps" , + :persistent_notify_url => "persistentNotifyUrl" , + :transform => "transform" , + + :deadline => "deadline" , + :insert_only => "insertOnly" , + :fsize_limit => "fsizeLimit" , + :detect_mime => "detectMime" , + :mime_limit => "mimeLimit" , + :fop_timeout => "fopTimeout" + } + + public + attr_reader :bucket, :key + + def scope!(bucket, key = nil) + @bucket = bucket + @key = key + + if key.nil? then + # 新增语义,文件已存在则失败 + @scope = bucket + else + # 覆盖语义,文件已存在则直接覆盖 + @scope = "#{bucket}:#{key}" + end + end # scope! + + def expires_in!(seconds) + if !seconds.nil? then + return @expires_in + end + + @epires_in = seconds + @deadline = Auth.calculate_deadline(seconds) + + return @expires_in + end # expires_in! + + def allow_mime_list! (list) + @mime_limit = list + end # allow_mime_list! + + def deny_mime_list! (list) + @mime_limit = "!#{list}" + end # deny_mime_list! + + def insert_only! + @insert_only = 1 + end # insert_only! + + def detect_mime! + @detect_mime = 1 + end # detect_mime! + + def to_json + args = {} + + PARAMS.each_pair do |key, fld| + val = self.public_send(key) + if !val.nil? then + args[fld] = val + end + end + + return args.to_json + end # to_json + + PARAMS.each_pair do |key, fld| + attr_accessor key + end + end # class PutPolicy class << self @@ -101,6 +208,21 @@ module Qiniu return acctoken end # generate_acctoken + def generate_uptoken(put_policy) + ### 提取AK/SK信息 + access_key = Config.settings[:access_key] + secret_key = Config.settings[:secret_key] + + ### 生成待签名字符串 + encoded_put_policy = Utils.urlsafe_base64_encode(put_policy.to_json) + + ### 生成数字签名 + sign = HMAC::SHA1.new(secret_key).update(encoded_put_policy).digest + encoded_sign = Utils.urlsafe_base64_encode(sign) + + ### 生成上传授权凭证 + uptoken = "#{access_key}:#{encoded_sign}:#{encoded_put_policy}" + end # generate_uptoken end # class << self end # module Auth diff --git a/lib/qiniu/upload.rb b/lib/qiniu/upload.rb index faede91..ecb3743 100755 --- a/lib/qiniu/upload.rb +++ b/lib/qiniu/upload.rb @@ -1,4 +1,5 @@ # -*- encoding: utf-8 -*- +# vim: sw=2 ts=2 module Qiniu module Storage @@ -61,6 +62,27 @@ module Qiniu Utils.http_request url, post_data end # upload_with_token_2 + ### 授权举例 + # put_policy.bucket | put_policy.key | key | 语义 | 授权 + # :---------------- | :------------- | :------ | :--- | :--- + # trivial_bucket | | | 新增 | 允许,最终key为1)使用put_policy.save_key生成的值或2)资源内容的Hash值 + # trivial_bucket | | foo.txt | 新增 | 允许 + # trivial_bucket | | bar.jpg | 新增 | 允许 + # trivial_bucket | foo.txt | | 覆盖 | 允许,由SDK将put_policy.key赋值给key实现 + # trivial_bucket | foo.txt | foo.txt | 覆盖 | 允许 + # trivial_bucket | foo.txt | bar.jpg | 覆盖 | 禁止,put_policy.key与key不一致 + def upload_with_put_policy(put_policy, + local_file, + key = nil, + x_vars = nil) + uptoken = Auth.generate_uptoken(put_policy) + if key.nil? then + key = put_policy.key + end + + return upload_with_token_2(uptoken, local_file, key, x_vars) + end # upload_with_put_policy + private def _generate_action_params(local_file, bucket, diff --git a/spec/qiniu/upload_spec.rb b/spec/qiniu/upload_spec.rb index e344c21..f345c8e 100755 --- a/spec/qiniu/upload_spec.rb +++ b/spec/qiniu/upload_spec.rb @@ -1,4 +1,5 @@ # -*- encoding: utf-8 -*- +# vim: sw=2 ts=2 require 'spec_helper' require 'qiniu/auth' @@ -126,6 +127,49 @@ module Qiniu end end + context ".upload_with_put_policy" do + it "should works" do + pp = Qiniu::Auth::PutPolicy.new(@bucket, @key) + pp.end_user = "why404@gmail.com" + puts 'put_policy=' + pp.to_json + + code, data, raw_headers = Qiniu::Storage.upload_with_put_policy( + pp, + __FILE__, + @key + '-not-equal' + ) + code.should_not == 200 + puts data.inspect + puts raw_headers.inspect + + code, data, raw_headers = Qiniu::Storage.upload_with_put_policy( + pp, + __FILE__, + @key + ) + + code.should == 200 + puts data.inspect + puts raw_headers.inspect + end + end # .upload_with_put_policy + + context ".stat" do + it "should exists" do + code, data = Qiniu::Storage.stat(@bucket, @key) + puts data.inspect + code.should == 200 + end + end + + context ".delete" do + it "should works" do + code, data = Qiniu::Storage.delete(@bucket, @key) + puts data.inspect + code.should == 200 + end + end + ### 测试断点续上传 context ".resumable_upload_with_token" do it "should works" do From 0c8a8e98e02fbd9e9a2779f52534fa5ecae438b7 Mon Sep 17 00:00:00 2001 From: Liang Tao Date: Fri, 28 Mar 2014 11:47:18 +0800 Subject: [PATCH 07/13] =?UTF-8?q?=E7=BB=9F=E4=B8=80=E6=94=B9=E4=B8=BA?= =?UTF-8?q?=E4=BD=BF=E7=94=A8Auth.calculate=5Fdeadline()=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E8=AE=A1=E7=AE=97=E6=8E=88=E6=9D=83=E6=9C=9F=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/qiniu/auth.rb | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/lib/qiniu/auth.rb b/lib/qiniu/auth.rb index 758838a..3990b31 100755 --- a/lib/qiniu/auth.rb +++ b/lib/qiniu/auth.rb @@ -142,16 +142,7 @@ module Qiniu secret_key = Config.settings[:secret_key] ### 授权期计算 - if args[:expires].is_a?(Integer) && args[:expires] > 0 then - # 指定相对时间,单位:秒 - e = Time.now.to_i + args[:expires] - elsif args[:deadline].is_a?(Integer) then - # 指定绝对时间,常用于调试和单元测试 - e = args[:deadline] - else - # 默认授权期1小时 - e = Time.now.to_i + 3600 - end + e = Auth.calculate_deadline(args[:expires_in], args[:deadline]) ### URL变换:追加授权期参数 if url.index('?').is_a?(Fixnum) then From fe70576624e484b42743904f31e035437f7de1b8 Mon Sep 17 00:00:00 2001 From: Liang Tao Date: Fri, 28 Mar 2014 13:34:58 +0800 Subject: [PATCH 08/13] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=96=B0=E7=9A=84?= =?UTF-8?q?=E5=B8=B8=E9=87=8FDEFAULT=5FAUTH=5FSECONDS=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/qiniu/auth.rb | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/qiniu/auth.rb b/lib/qiniu/auth.rb index 3990b31..eee7490 100755 --- a/lib/qiniu/auth.rb +++ b/lib/qiniu/auth.rb @@ -8,6 +8,8 @@ require 'qiniu/exceptions' module Qiniu module Auth + DEFAULT_AUTH_SECONDS = 3600 + class << self def calculate_deadline(expires_in, deadline = nil) ### 授权期计算 @@ -20,13 +22,16 @@ module Qiniu end # 默认授权期1小时 - return Time.now.to_i + 3600 + return Time.now.to_i + DEFAULT_AUTH_SECONDS end # calculate_deadline end # class << self class PutPolicy private - def initialize(bucket, key = nil, expires_in = 3600, deadline = nil) + def initialize(bucket, + key = nil, + expires_in = DEFAULT_AUTH_SECONDS, + deadline = nil) ### 设定scope参数(必填项目) self.scope!(bucket, key) @@ -82,6 +87,14 @@ module Qiniu return @expires_in end # expires_in! + def expires_in=(seconds) + return expires_in!(seconds) + end # expires_in= + + def expires_in + return @expires_in + end # expires_in + def allow_mime_list! (list) @mime_limit = list end # allow_mime_list! From c91c5e424638d4fe345352aec14cabbb0281b16b Mon Sep 17 00:00:00 2001 From: Liang Tao Date: Fri, 28 Mar 2014 16:05:48 +0800 Subject: [PATCH 09/13] =?UTF-8?q?=E5=B0=86=E6=B5=8B=E8=AF=95=E7=94=A8?= =?UTF-8?q?=E4=BE=8B=E4=B8=AD=E5=AF=B9Storage.upload=5Fwith=5Ftoken=5F2?= =?UTF-8?q?=E7=9A=84=E8=B0=83=E7=94=A8=E6=9B=B4=E6=8D=A2=E4=B8=BAStorage.u?= =?UTF-8?q?pload=5Fwith=5Fput=5Fpolicy=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- spec/qiniu/auth_spec.rb | 11 ++++------- spec/qiniu/management_spec.rb | 14 ++++++-------- 2 files changed, 10 insertions(+), 15 deletions(-) diff --git a/spec/qiniu/auth_spec.rb b/spec/qiniu/auth_spec.rb index 0054e4f..afb54bc 100755 --- a/spec/qiniu/auth_spec.rb +++ b/spec/qiniu/auth_spec.rb @@ -31,13 +31,10 @@ module Qiniu puts "key=#{key}" ### 上传测试文件 - upopts = {:scope => @bucket, :expires_in => 3600, :endUser => "why404@gmail.com"} - uptoken = Qiniu.generate_upload_token(upopts) - - code, data, raw_headers = Qiniu::Storage.upload_with_token_2( - uptoken, - __FILE__, - key + pp = Auth::PutPolicy.new(@bucket, key) + code, data, raw_headers = Qiniu::Storage.upload_with_put_policy( + pp, + __FILE__ ) code.should == 200 puts data.inspect diff --git a/spec/qiniu/management_spec.rb b/spec/qiniu/management_spec.rb index 7023cc0..abd1fff 100755 --- a/spec/qiniu/management_spec.rb +++ b/spec/qiniu/management_spec.rb @@ -1,4 +1,5 @@ # -*- encoding: utf-8 -*- +# vim: sw=2 ts=2 require 'digest/sha1' require 'spec_helper' @@ -29,15 +30,12 @@ module Qiniu end ### 准备数据 - context ".upload_with_token_2" do + context ".prepare_file" do it "should works" do - upopts = {:scope => @bucket, :expires_in => 3600, :endUser => "why404@gmail.com"} - uptoken = Qiniu.generate_upload_token(upopts) - - code, data, raw_headers = Qiniu::Storage.upload_with_token_2( - uptoken, - __FILE__, - @key + pp = Auth::PutPolicy.new(@bucket, @key) + code, data, raw_headers = Qiniu::Storage.upload_with_put_policy( + pp, + __FILE__ ) code.should == 200 puts data.inspect From 1bc0476671aaef837da213b180d172fedbe290be Mon Sep 17 00:00:00 2001 From: Liang Tao Date: Fri, 28 Mar 2014 16:57:35 +0800 Subject: [PATCH 10/13] =?UTF-8?q?=E4=BF=AE=E6=AD=A3Auth=E4=B8=AD=E7=9A=84B?= =?UTF-8?q?ug=EF=BC=8C=E5=B9=B6=E4=BF=9D=E8=AF=81=E5=90=91=E4=B8=8B?= =?UTF-8?q?=E5=85=BC=E5=AE=B9=E6=80=A7=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/qiniu/auth.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/qiniu/auth.rb b/lib/qiniu/auth.rb index eee7490..10918e1 100755 --- a/lib/qiniu/auth.rb +++ b/lib/qiniu/auth.rb @@ -115,7 +115,7 @@ module Qiniu args = {} PARAMS.each_pair do |key, fld| - val = self.public_send(key) + val = self.__send__(key) if !val.nil? then args[fld] = val end @@ -189,7 +189,7 @@ module Qiniu # 如有QueryString部分,则需要加上 query_string = uri.query if query_string.is_a?(String) && !query_string.empty? - singin_str += '?' + query_string + signing_str += '?' + query_string end # 追加换行符 From 26b46859a19f48e7eb7c707538248a1425b71f74 Mon Sep 17 00:00:00 2001 From: Liang Tao Date: Mon, 31 Mar 2014 15:02:34 +0800 Subject: [PATCH 11/13] =?UTF-8?q?=E8=A1=A5=E5=85=85=E6=B3=A8=E9=87=8A?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/qiniu/auth.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/qiniu/auth.rb b/lib/qiniu/auth.rb index 10918e1..5b2e531 100755 --- a/lib/qiniu/auth.rb +++ b/lib/qiniu/auth.rb @@ -41,6 +41,7 @@ module Qiniu end # initialize PARAMS = { + # 字符串类型参数 :scope => "scope" , :save_key => "saveKey" , :end_user => "endUser" , @@ -52,13 +53,14 @@ module Qiniu :persistent_notify_url => "persistentNotifyUrl" , :transform => "transform" , + # 数值类型参数 :deadline => "deadline" , :insert_only => "insertOnly" , :fsize_limit => "fsizeLimit" , :detect_mime => "detectMime" , :mime_limit => "mimeLimit" , :fop_timeout => "fopTimeout" - } + } # PARAMS public attr_reader :bucket, :key @@ -226,6 +228,9 @@ module Qiniu ### 生成上传授权凭证 uptoken = "#{access_key}:#{encoded_sign}:#{encoded_put_policy}" + + ### 返回上传授权凭证 + return uptoken end # generate_uptoken end # class << self From 3f4c79b78f7f50667d79eae892b4f538f6fbd57c Mon Sep 17 00:00:00 2001 From: Liang Tao Date: Tue, 1 Apr 2014 10:10:30 +0800 Subject: [PATCH 12/13] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=E8=AF=B4=E6=98=8E=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 6 ++++++ Gemfile.lock | 2 +- lib/qiniu/version.rb | 2 +- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 41a0446..5935def 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ ## CHANGE LOG +### v6.2.0 + +- 重写与授权相关的函数并归入Qiniu::Auth空间,原授权凭证生成类维持不变。 + +- 添加Qiniu::Storage::PutPolicy类和Qiniu::Storage#upload_with_put_policy方法,并推荐使用两者组合实现单文件上传。 + ### v6.1.0 - Qiniu::Storage所有上传接口返回第三个值raw_headers,类型为Hash,包含已解析的HTTP响应报文中的所有Header信息。 diff --git a/Gemfile.lock b/Gemfile.lock index e546604..9ce9541 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - qiniu (6.1.0) + qiniu (6.2.0) json (~> 1.7) mime-types (~> 1.19) rest-client (~> 1.6) diff --git a/lib/qiniu/version.rb b/lib/qiniu/version.rb index fa24356..820edea 100755 --- a/lib/qiniu/version.rb +++ b/lib/qiniu/version.rb @@ -3,7 +3,7 @@ module Qiniu module Version MAJOR = 6 - MINOR = 1 + MINOR = 2 PATCH = 0 # Returns a version string by joining MAJOR, MINOR, and PATCH with '.' # From 0ccda7cdb16992b9d27b47876c48e41b86f5e3a3 Mon Sep 17 00:00:00 2001 From: Liang Tao Date: Wed, 2 Apr 2014 11:44:32 +0800 Subject: [PATCH 13/13] =?UTF-8?q?JRuby-Head=E7=8E=AF=E5=A2=83=E6=9C=89?= =?UTF-8?q?=E9=97=AE=E9=A2=98=EF=BC=8C=E5=B7=B2=E7=BB=8F=E5=90=91Travis-CI?= =?UTF-8?q?=E6=8F=90=E4=BA=A4Issue=EF=BC=8C=E6=9A=82=E6=97=B6=E5=81=9C?= =?UTF-8?q?=E6=AD=A2=E5=9C=A8=E8=AF=A5=E7=8E=AF=E5=A2=83=E7=9A=84=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .travis.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index c7863ad..6b11d42 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,7 +7,6 @@ rvm: - 2.1.1 - jruby-18mode - jruby-19mode - - jruby-head - ree before_script: - export QINIU_ACCESS_KEY=LGRqJsjX7LAvtdtX6hhyxs861lS57HwnOQmJsOuX