diff --git a/CHANGELOG.md b/CHANGELOG.md index 46a69b5..6bec2fd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ ## CHANGE LOG +### v3.3.0 + +- 私有资源下载新版实现,添加 Qiniu::RS.generate_download_token() 方法。参考 [downloadToken](http://docs.qiniutek.com/v3/api/io/#get) + ### v3.2.2 fixed E701 error diff --git a/Gemfile.lock b/Gemfile.lock index 9c5d6a1..e93c190 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - qiniu-rs (3.2.2) + qiniu-rs (3.3.0) json (~> 1.7) mime-types (~> 1.19) rest-client (~> 1.6) @@ -14,15 +14,15 @@ GEM fakeweb (1.3.0) json (1.7.5) mime-types (1.19) - rake (10.0.2) + rake (10.0.3) rest-client (1.6.7) mime-types (>= 1.16) rspec (2.12.0) rspec-core (~> 2.12.0) rspec-expectations (~> 2.12.0) rspec-mocks (~> 2.12.0) - rspec-core (2.12.1) - rspec-expectations (2.12.0) + rspec-core (2.12.2) + rspec-expectations (2.12.1) diff-lcs (~> 1.1.3) rspec-mocks (2.12.0) ruby-hmac (0.4.0) diff --git a/README.md b/README.md index 1100b7c..17ec399 100755 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Qiniu Resource (Cloud) Storage SDK for Ruby - [![Build Status](https://secure.travis-ci.org/why404/qiniu-rs-for-ruby.png?branch=master)](http://travis-ci.org/why404/qiniu-rs-for-ruby) [![Dependency Status](https://gemnasium.com/why404/qiniu-rs-for-ruby.png)](https://gemnasium.com/why404/qiniu-rs-for-ruby) +# Qiniu Resource (Cloud) Storage SDK for Ruby - [![Build Status](https://api.travis-ci.org/qiniu/ruby-sdk.png?branch=master)](https://travis-ci.org/qiniu/ruby-sdk) [![Dependency Status](https://gemnasium.com/why404/qiniu-rs-for-ruby.png)](https://gemnasium.com/why404/qiniu-rs-for-ruby) # 关于 diff --git a/lib/qiniu/rs.rb b/lib/qiniu/rs.rb index 4e18d09..fd58d19 100755 --- a/lib/qiniu/rs.rb +++ b/lib/qiniu/rs.rb @@ -17,6 +17,7 @@ module Qiniu autoload :AccessToken, 'qiniu/tokens/access_token' autoload :QboxToken, 'qiniu/tokens/qbox_token' autoload :UploadToken, 'qiniu/tokens/upload_token' + autoload :DownloadToken, 'qiniu/tokens/download_token' autoload :Abstract, 'qiniu/rs/abstract' class << self @@ -100,7 +101,7 @@ module Qiniu end def upload_file opts = {} - uncontained_opts = [:uptoken, :file, :bucket, :key] - opts.keys + uncontained_opts = [:uptoken, :file, :bucket, :key] - opts.keys raise MissingArgsError, uncontained_opts unless uncontained_opts.empty? source_file = opts[:file] @@ -232,6 +233,15 @@ module Qiniu token_obj.generate_token end + def generate_download_token(opts = {}) + token_obj = DownloadToken.new(opts) + token_obj.access_key = Config.settings[:access_key] + token_obj.secret_key = Config.settings[:secret_key] + #token_obj.expires_in = opts[:expires_in] + #token_obj.pattern = opts[:pattern] + token_obj.generate_token + end + end end diff --git a/lib/qiniu/rs/up.rb b/lib/qiniu/rs/up.rb index 98f8650..ec1663b 100755 --- a/lib/qiniu/rs/up.rb +++ b/lib/qiniu/rs/up.rb @@ -58,7 +58,7 @@ module Qiniu class TmpData def initialize(dir, filename) @tmpdir = Config.settings[:tmpdir] + File::SEPARATOR + dir - FileUtils.mkdir_p(@tmpdir) unless Dir.exists?(@tmpdir) + FileUtils.mkdir_p(@tmpdir) unless File.directory?(@tmpdir) @tmpfile = @tmpdir + File::SEPARATOR + filename end @@ -83,7 +83,7 @@ module Qiniu end def sweep! - FileUtils.rm_r(@tmpdir) if Dir.exists?(@tmpdir) + FileUtils.rm_r(@tmpdir) if File.directory?(@tmpdir) end end @@ -170,7 +170,7 @@ module Qiniu _call_binary_with_token(uptoken, url, body) end - def _resumable_put_block(uptoken, fh, block_index, block_size, chunk_size, progress, retry_times = 1, notifier) + def _resumable_put_block(uptoken, fh, block_index, block_size, chunk_size, progress, retry_times, notifier) code, data = 0, {} fpath = fh.path # this block has never been uploaded. @@ -318,6 +318,7 @@ module Qiniu end end + end end end diff --git a/lib/qiniu/rs/version.rb b/lib/qiniu/rs/version.rb index dc7e222..0e5cf34 100755 --- a/lib/qiniu/rs/version.rb +++ b/lib/qiniu/rs/version.rb @@ -4,8 +4,8 @@ module Qiniu module RS module Version MAJOR = 3 - MINOR = 2 - PATCH = 2 + MINOR = 3 + PATCH = 0 # Returns a version string by joining MAJOR, MINOR, and PATCH with '.' # # Example diff --git a/lib/qiniu/tokens/download_token.rb b/lib/qiniu/tokens/download_token.rb new file mode 100755 index 0000000..4bc5906 --- /dev/null +++ b/lib/qiniu/tokens/download_token.rb @@ -0,0 +1,33 @@ +# -*- encoding: utf-8 -*- + +require 'json' +require 'qiniu/tokens/access_token' +require 'qiniu/rs/utils' + +module Qiniu + module RS + class DownloadToken < AccessToken + + include Utils + + attr_accessor :pattern, :expires_in + + def initialize(opts = {}) + @pattern = opts[:pattern] || "*" + @expires_in = opts[:expires_in] || 3600 + end + + def generate_signature + params = {"S" => @pattern, "E" => Time.now.to_i + @expires_in} + Utils.urlsafe_base64_encode(params.to_json) + end + + def generate_token + signature = generate_signature + encoded_digest = generate_encoded_digest(signature) + %Q(#{@access_key}:#{encoded_digest}:#{signature}) + end + + end + end +end diff --git a/lib/qiniu/tokens/qbox_token.rb b/lib/qiniu/tokens/qbox_token.rb index 2073f7d..8ccdb0c 100755 --- a/lib/qiniu/tokens/qbox_token.rb +++ b/lib/qiniu/tokens/qbox_token.rb @@ -8,6 +8,8 @@ module Qiniu module RS class QboxToken < AccessToken + include Utils + attr_accessor :url, :params def initialize(opts = {}) @@ -20,10 +22,10 @@ module Qiniu signature = uri.path query_string = uri.query signature += '?' + query_string if !query_string.nil? && !query_string.empty? - signature += "\n"; + signature += "\n" if @params.is_a?(Hash) - total_param = @params.map { |key, value| %Q(#{CGI.escape(key.to_s)}=#{CGI.escape(value.to_s).gsub('+', '%20')}) } - signature += total_param.join("&") + params_string = Utils.generate_query_string(@params) + signature += params_string end signature end diff --git a/lib/qiniu/tokens/upload_token.rb b/lib/qiniu/tokens/upload_token.rb index bb13af4..24ea913 100755 --- a/lib/qiniu/tokens/upload_token.rb +++ b/lib/qiniu/tokens/upload_token.rb @@ -27,7 +27,7 @@ module Qiniu params[:callbackBodyType] = @callback_body_type if !@callback_body_type.nil? && !@callback_body_type.empty? params[:customer] = @customer if !@customer.nil? && !@customer.empty? params[:escape] = 1 if @escape == 1 || @escape == true - urlsafe_base64_encode(params.to_json) + Utils.urlsafe_base64_encode(params.to_json) end def generate_token diff --git a/spec/qiniu/rs/io_spec.rb b/spec/qiniu/rs/io_spec.rb index 70abd7f..9be4526 100755 --- a/spec/qiniu/rs/io_spec.rb +++ b/spec/qiniu/rs/io_spec.rb @@ -11,7 +11,7 @@ module Qiniu before :all do @bucket = "io_test_bucket" - @key = Digest::SHA1.hexdigest (Time.now.to_i+rand(100)).to_s + @key = Digest::SHA1.hexdigest((Time.now.to_i+rand(100)).to_s) result = Qiniu::RS.mkbucket(@bucket) puts result.inspect diff --git a/spec/qiniu/rs/rs_spec.rb b/spec/qiniu/rs/rs_spec.rb index 101916d..ca7ea91 100755 --- a/spec/qiniu/rs/rs_spec.rb +++ b/spec/qiniu/rs/rs_spec.rb @@ -12,7 +12,7 @@ module Qiniu before :all do @bucket = "rs_test_bucket" - @key = Digest::SHA1.hexdigest (Time.now.to_i+rand(100)).to_s + @key = Digest::SHA1.hexdigest((Time.now.to_i+rand(100)).to_s) @domain = 'rs-test-bucket.dn.qbox.me' code, data = Qiniu::RS::RS.mkbucket(@bucket) diff --git a/spec/qiniu/rs/up_spec.rb b/spec/qiniu/rs/up_spec.rb index 024d17b..259f1d8 100755 --- a/spec/qiniu/rs/up_spec.rb +++ b/spec/qiniu/rs/up_spec.rb @@ -11,7 +11,7 @@ module Qiniu before :all do @localfile = "bigfile.txt" - File.open(@localfile, "w"){|f| 5242888.times{f.write(Random.rand(9).to_s)}} + File.open(@localfile, "w"){|f| 5242888.times{f.write(rand(9).to_s)}} @bucket = "up_test_bucket" @key = Digest::SHA1.hexdigest(@localfile+Time.now.to_s) diff --git a/spec/qiniu/rs_spec.rb b/spec/qiniu/rs_spec.rb index 17f169b..1e7f87e 100755 --- a/spec/qiniu/rs_spec.rb +++ b/spec/qiniu/rs_spec.rb @@ -31,10 +31,13 @@ module Qiniu end after :all do - result = Qiniu::RS.drop(@bucket) - puts result.inspect + result = Qiniu::RS.unpublish(@domain) result.should_not be_false + result1 = Qiniu::RS.drop(@bucket) + puts result1.inspect + result1.should_not be_false + result2 = Qiniu::RS.drop(@test_image_bucket) puts result2.inspect result2.should_not be_false @@ -180,7 +183,7 @@ module Qiniu it "should works" do # generate bigfile for testing localfile = "test_bigfile" - File.open(localfile, "w"){|f| 5242888.times{f.write(Random.rand(9).to_s)}} + File.open(localfile, "w"){|f| 5242888.times{f.write(rand(9).to_s)}} key = Digest::SHA1.hexdigest(localfile+Time.now.to_s) # generate the upload token uptoken_opts = {:scope => @bucket, :expires_in => 3600, :customer => "awhy.xu@gmail.com", :escape => 0} @@ -281,12 +284,14 @@ module Qiniu end end +=begin context ".unpublish" do it "should works" do result = Qiniu::RS.unpublish(@domain) result.should_not be_false end end +=end context ".delete" do it "should works" do @@ -360,6 +365,16 @@ module Qiniu data = Qiniu::RS.generate_upload_token({:scope => 'test_bucket', :expires_in => 3600, :escape => 0}) data.should_not be_empty puts data.inspect + data.split(":").length.should == 3 + end + end + + context ".generate_download_token" do + it "should works" do + data = Qiniu::RS.generate_download_token({:expires_in => 1, :pattern => 'http://qiniu-rs-test.dn.qbox.me/*'}) + data.should_not be_empty + puts data.inspect + data.split(":").length.should == 3 end end