From 818c4bb5b3e29a5cf6599d6e73117e0f0f60cd38 Mon Sep 17 00:00:00 2001 From: Liang Tao Date: Mon, 27 Jan 2014 18:39:50 +0800 Subject: [PATCH 01/21] =?UTF-8?q?=E7=A7=BB=E9=99=A4=E6=8C=81=E4=B9=85?= =?UTF-8?q?=E5=8C=96=E4=BB=A3=E7=A0=81=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/qiniu/rs/up.rb | 69 ++++++++-------------------------------------- 1 file changed, 11 insertions(+), 58 deletions(-) diff --git a/lib/qiniu/rs/up.rb b/lib/qiniu/rs/up.rb index ec1663b..ac9faa3 100755 --- a/lib/qiniu/rs/up.rb +++ b/lib/qiniu/rs/up.rb @@ -14,9 +14,6 @@ module Qiniu module RS module UP - PROGRESS_TMP_FILE = 'progresses' - CHECKSUM_TMP_FILE = 'ctxes' - module AbstractClass class ChunkProgressNotifier include Qiniu::RS::Abstract @@ -32,62 +29,19 @@ module Qiniu end class ChunkProgressNotifier < AbstractClass::ChunkProgressNotifier - attr_reader :tmpdata - def initialize(id) - @tmpdata = UP::TmpData.new(id, PROGRESS_TMP_FILE) - end def notify(index, progress) - @tmpdata.set(index, progress) logmsg = "chunk #{progress[:offset]/Config.settings[:chunk_size]} in block #{index} successfully uploaded.\n" + progress.to_s Utils.debug(logmsg) end end class BlockProgressNotifier < AbstractClass::BlockProgressNotifier - attr_reader :tmpdata - def initialize(id) - @tmpdata = UP::TmpData.new(id, CHECKSUM_TMP_FILE) - end def notify(index, checksum) - @tmpdata.set(index, checksum) Utils.debug "block #{index}: {ctx: #{checksum}} successfully uploaded." Utils.debug "block #{index}: {checksum: #{checksum}} successfully uploaded." end end - class TmpData - def initialize(dir, filename) - @tmpdir = Config.settings[:tmpdir] + File::SEPARATOR + dir - FileUtils.mkdir_p(@tmpdir) unless File.directory?(@tmpdir) - @tmpfile = @tmpdir + File::SEPARATOR + filename - end - - def init(values) - File.open(@tmpfile, "w") do |f| - YAML::dump(values, f) - Utils.debug %Q(Initializing tmpfile: #{@tmpfile}) - end - end - - def all - File.exist?(@tmpfile) ? YAML.load_file(@tmpfile) : [] - end - - def set(index, value) - values = all - values[index] = value - File.open(@tmpfile, "w") do |f| - YAML::dump(values, f) - Utils.debug %Q(Updating tmpfile: #{@tmpfile}) - end - end - - def sweep! - FileUtils.rm_r(@tmpdir) if File.directory?(@tmpdir) - end - end - - class << self include Utils @@ -290,30 +244,29 @@ module Qiniu end def _resumable_upload(uptoken, fh, fsize, bucket, key, mime_type = nil, custom_meta = nil, customer = nil, callback_params = nil, rotate = nil) + block_count = _block_count(fsize) + chunk_notifier = ChunkProgressNotifier.new(key) block_notifier = BlockProgressNotifier.new(key) - progresses = chunk_notifier.tmpdata.all - if progresses.empty? - block_count.times{progresses << _new_block_put_progress_data} - chunk_notifier.tmpdata.init(progresses) - end - checksums = block_notifier.tmpdata.all - if checksums.empty? - block_count.times{checksums << ''} - block_notifier.tmpdata.init(checksums) - end + + progresses = [] + block_count.times{progresses << _new_block_put_progress_data} + checksums = [] + block_count.times{checksums << ''} + code, data = _resumable_put(uptoken, fh, checksums, progresses, block_notifier, chunk_notifier) + if Utils.is_response_ok?(code) uphost = data["host"] entry_uri = bucket + ':' + key code, data = _mkfile(uphost, uptoken, entry_uri, fsize, checksums, mime_type, custom_meta, customer, callback_params, rotate) end + if Utils.is_response_ok?(code) Utils.debug "File #{fh.path} {size: #{fsize}} successfully uploaded." - chunk_notifier.tmpdata.sweep! - block_notifier.tmpdata.sweep! end + [code, data] end From 0f3704c60fb077c5baa384299b2ca0081fa3e35c Mon Sep 17 00:00:00 2001 From: Liang Tao Date: Tue, 28 Jan 2014 12:18:25 +0800 Subject: [PATCH 02/21] =?UTF-8?q?=E5=88=A0=E9=99=A4=E9=9D=9E=E5=BF=85?= =?UTF-8?q?=E9=9C=80=E7=9A=84=E5=88=9D=E5=A7=8B=E5=8C=96key=E5=8F=82?= =?UTF-8?q?=E6=95=B0=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/qiniu/rs/up.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/qiniu/rs/up.rb b/lib/qiniu/rs/up.rb index ac9faa3..84ff127 100755 --- a/lib/qiniu/rs/up.rb +++ b/lib/qiniu/rs/up.rb @@ -247,8 +247,8 @@ module Qiniu block_count = _block_count(fsize) - chunk_notifier = ChunkProgressNotifier.new(key) - block_notifier = BlockProgressNotifier.new(key) + chunk_notifier = ChunkProgressNotifier.new() + block_notifier = BlockProgressNotifier.new() progresses = [] block_count.times{progresses << _new_block_put_progress_data} From b0335577e52f83c29e86da63697001cfead4ea5e Mon Sep 17 00:00:00 2001 From: Liang Tao Date: Tue, 28 Jan 2014 12:20:31 +0800 Subject: [PATCH 03/21] =?UTF-8?q?=E4=BB=A54M=E4=B8=BA=E5=8D=95=E4=BD=8D?= =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=95=B0=E6=8D=AE=E7=89=87=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/qiniu/rs/up.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/qiniu/rs/up.rb b/lib/qiniu/rs/up.rb index 84ff127..55a603a 100755 --- a/lib/qiniu/rs/up.rb +++ b/lib/qiniu/rs/up.rb @@ -127,6 +127,7 @@ module Qiniu 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. if progress[:ctx] == nil || progress[:ctx].empty? progress[:offset] = 0 @@ -159,6 +160,7 @@ module Qiniu elsif progress[:offset] + progress[:restsize] != block_size raise BlockSizeNotMathchError.new(fpath, block_index, progress[:offset], progress[:restsize], block_size) end + # loop uploading other chunks except the first one while progress[:restsize].to_i > 0 && progress[:restsize] < block_size # choose the smaller one @@ -213,7 +215,9 @@ module Qiniu if progresses[block_index].nil? progresses[block_index] = _new_block_put_progress_data end - code, data = _resumable_put_block(uptoken, fh, block_index, block_size, Config.settings[:chunk_size], progresses[block_index], Config.settings[:max_retry_times], chunk_notifier) + #code, data = _resumable_put_block(uptoken, fh, block_index, block_size, Config.settings[:chunk_size], progresses[block_index], Config.settings[:max_retry_times], chunk_notifier) + # Put the whole block as a chunk + code, data = _resumable_put_block(uptoken, fh, block_index, block_size, block_size, progresses[block_index], Config.settings[:max_retry_times], chunk_notifier) if Utils.is_response_ok?(code) #checksums[block_index] = data["checksum"] checksums[block_index] = data["ctx"] From 3302ecde6a219a9e4b5f649a046175e8a9aa3d4d Mon Sep 17 00:00:00 2001 From: Liang Tao Date: Tue, 28 Jan 2014 16:04:55 +0800 Subject: [PATCH 04/21] =?UTF-8?q?=E5=88=A0=E9=99=A4=E8=BF=87=E6=97=B6?= =?UTF-8?q?=E7=9A=84=E6=B5=8B=E8=AF=95=E6=A1=88=E4=BE=8B=EF=BC=8C=E5=B9=B6?= =?UTF-8?q?=E4=B8=BA=E6=96=B0=E6=94=B9=E5=8A=A8=E9=80=BB=E8=BE=91=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E6=96=B0=E6=A1=88=E4=BE=8B=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/qiniu/rs/config.rb | 4 +- lib/qiniu/rs/rs.rb | 2 + spec/qiniu/rs/auth_spec.rb | 58 ----------------------- spec/qiniu/rs/image_spec.rb | 2 + spec/qiniu/rs/rs_spec.rb | 2 + spec/qiniu/rs/up_spec.rb | 93 +++++++++++++++++++++++++++++++++++++ spec/qiniu/rs_spec.rb | 4 ++ 7 files changed, 105 insertions(+), 60 deletions(-) delete mode 100755 spec/qiniu/rs/auth_spec.rb diff --git a/lib/qiniu/rs/config.rb b/lib/qiniu/rs/config.rb index 159447c..d7f65df 100755 --- a/lib/qiniu/rs/config.rb +++ b/lib/qiniu/rs/config.rb @@ -20,9 +20,9 @@ module Qiniu :method => :post, :content_type => 'application/x-www-form-urlencoded', :auth_url => "https://acc.qbox.me/oauth2/token", - :rs_host => "http://rs.qbox.me", + :rs_host => "http://rs.qiniu.com", :io_host => "http://iovip.qbox.me", - :up_host => "http://up.qbox.me", + :up_host => "http://up.qiniu.com", :pub_host => "http://pu.qbox.me:10200", :eu_host => "http://eu.qbox.me", :client_id => "a75604760c4da4caaa456c0c5895c061c3065c5a", diff --git a/lib/qiniu/rs/rs.rb b/lib/qiniu/rs/rs.rb index 79bbd1e..d95c7b6 100755 --- a/lib/qiniu/rs/rs.rb +++ b/lib/qiniu/rs/rs.rb @@ -103,6 +103,7 @@ module Qiniu %Q(/#{command}/#{source_encoded_entry_uri}/#{target_encoded_entry_uri}) end +=begin def _batch_cp_or_mv(command, *op_args) execs = [] op_args.each do |e| @@ -110,6 +111,7 @@ module Qiniu end Auth.request Config.settings[:rs_host] + "/batch?" + execs.join("&") end +=end end end diff --git a/spec/qiniu/rs/auth_spec.rb b/spec/qiniu/rs/auth_spec.rb deleted file mode 100755 index e7b9d6a..0000000 --- a/spec/qiniu/rs/auth_spec.rb +++ /dev/null @@ -1,58 +0,0 @@ -# -*- encoding: utf-8 -*- - -require 'spec_helper' -require 'qiniu/rs/auth' - -module Qiniu - module RS - describe Auth do - - before :all do - @username = "qboxtest" - @password = "qboxtest123" - - code, data = Qiniu::RS::Auth.exchange_by_password!(@username, @password) - code.should == 200 - data.should be_an_instance_of(Hash) - data["access_token"].should_not be_empty - data["refresh_token"].should_not be_empty - data["refresh_token"].should_not be_empty - - @access_token = data["access_token"] - @refresh_token = data["refresh_token"] - puts data.inspect - end - - context ".exchange_by_password" do - it "should sign in failed when pass a non-existent username" do - code, data = Qiniu::RS::Auth.exchange_by_password!("a_non_existent_user@example.com", "password") - code.should == 401 - data["error_code"].should == 11 - data["error"].should == "failed_authentication" - puts data.inspect - end - - it "should sign in failed when pass a wrong password" do - code, data = Qiniu::RS::Auth.exchange_by_password!(@username, "a-wrong-password") - code.should == 401 - data["error_code"].should == 11 - data["error"].should == "failed_authentication" - puts data.inspect - end - end - - context ".exchange_by_refresh_token" do - it "should works" do - @refresh_token.should_not be_empty - code, data = Qiniu::RS::Auth.exchange_by_refresh_token!(@refresh_token) - code.should == 200 - data["access_token"].should_not be_empty - data["refresh_token"].should_not be_empty - data["expires_in"].should_not be_zero - puts data.inspect - end - end - - end - end -end diff --git a/spec/qiniu/rs/image_spec.rb b/spec/qiniu/rs/image_spec.rb index 135897c..5cfee7e 100755 --- a/spec/qiniu/rs/image_spec.rb +++ b/spec/qiniu/rs/image_spec.rb @@ -66,12 +66,14 @@ module Qiniu end end +=begin context ".exif" do it "should works" do code, data = Qiniu::RS::Image.exif(@source_image_url) puts data.inspect end end +=end context ".mogrify_preview_url" do it "should works" do diff --git a/spec/qiniu/rs/rs_spec.rb b/spec/qiniu/rs/rs_spec.rb index 3e3a785..67569ae 100755 --- a/spec/qiniu/rs/rs_spec.rb +++ b/spec/qiniu/rs/rs_spec.rb @@ -90,6 +90,7 @@ module Qiniu end end +=begin context ".batch_copy" do it "should works" do code, data = Qiniu::RS::RS.batch_copy [@bucket, @key, @bucket, @key2] @@ -117,6 +118,7 @@ module Qiniu puts data3.inspect end end +=end =begin context ".publish" do diff --git a/spec/qiniu/rs/up_spec.rb b/spec/qiniu/rs/up_spec.rb index 91b0849..0c22a34 100755 --- a/spec/qiniu/rs/up_spec.rb +++ b/spec/qiniu/rs/up_spec.rb @@ -15,6 +15,18 @@ module Qiniu @bucket = 'RubySdkTest' + (Time.now.to_i+rand(1000)).to_s @key = Digest::SHA1.hexdigest(@localfile+Time.now.to_s) + @localfile2 = "bigfile2.txt" + File.open(@localfile2, "w"){|f| (1 << 22).times{f.write(rand(9).to_s)}} + @key2 = Digest::SHA1.hexdigest(@localfile2+Time.now.to_s) + + @localfile3 = "bigfile3.txt" + File.open(@localfile3, "w"){|f| (1 << 23).times{f.write(rand(9).to_s)}} + @key3 = Digest::SHA1.hexdigest(@localfile3+Time.now.to_s) + + @localfile4 = "smallfile.txt" + File.open(@localfile4, "w"){|f| (1 << 20).times{f.write(rand(9).to_s)}} + @key4 = Digest::SHA1.hexdigest(@localfile4+Time.now.to_s) + code, data = Qiniu::RS::RS.mkbucket(@bucket) puts [code, data].inspect code.should == 200 @@ -22,6 +34,9 @@ module Qiniu after :all do File.unlink(@localfile) if File.exists?(@localfile) + File.unlink(@localfile2) if File.exists?(@localfile2) + File.unlink(@localfile3) if File.exists?(@localfile3) + File.unlink(@localfile4) if File.exists?(@localfile4) code, data = Qiniu::RS::RS.drop(@bucket) puts [code, data].inspect @@ -54,6 +69,84 @@ module Qiniu end end + context ".upload_with_token2" do + it "should works" do + upopts = {:scope => @bucket, :expires_in => 3600, :customer => "why404@gmail.com"} + uptoken = Qiniu::RS.generate_upload_token(upopts) + code, data = Qiniu::RS::UP.upload_with_token(uptoken, @localfile2, @bucket, @key2) + puts data.inspect + (code/100).should == 2 + end + end + + context ".stat" do + it "should exists" do + code, data = Qiniu::RS::RS.stat(@bucket, @key2) + puts data.inspect + code.should == 200 + end + end + + context ".delete" do + it "should works" do + code, data = Qiniu::RS::RS.delete(@bucket, @key2) + puts data.inspect + code.should == 200 + end + end + + context ".upload_with_token3" do + it "should works" do + upopts = {:scope => @bucket, :expires_in => 3600, :customer => "why404@gmail.com"} + uptoken = Qiniu::RS.generate_upload_token(upopts) + code, data = Qiniu::RS::UP.upload_with_token(uptoken, @localfile3, @bucket, @key3) + puts data.inspect + (code/100).should == 2 + end + end + + context ".stat" do + it "should exists" do + code, data = Qiniu::RS::RS.stat(@bucket, @key3) + puts data.inspect + code.should == 200 + end + end + + context ".delete" do + it "should works" do + code, data = Qiniu::RS::RS.delete(@bucket, @key3) + puts data.inspect + code.should == 200 + end + end + + context ".upload_with_token4" do + it "should works" do + upopts = {:scope => @bucket, :expires_in => 3600, :customer => "why404@gmail.com"} + uptoken = Qiniu::RS.generate_upload_token(upopts) + code, data = Qiniu::RS::UP.upload_with_token(uptoken, @localfile4, @bucket, @key4) + puts data.inspect + (code/100).should == 2 + end + end + + context ".stat" do + it "should exists" do + code, data = Qiniu::RS::RS.stat(@bucket, @key4) + puts data.inspect + code.should == 200 + end + end + + context ".delete" do + it "should works" do + code, data = Qiniu::RS::RS.delete(@bucket, @key4) + puts data.inspect + code.should == 200 + end + end + end end end diff --git a/spec/qiniu/rs_spec.rb b/spec/qiniu/rs_spec.rb index fa2cdb3..22a1699 100755 --- a/spec/qiniu/rs_spec.rb +++ b/spec/qiniu/rs_spec.rb @@ -296,6 +296,7 @@ module Qiniu end =end +=begin context ".batch_copy" do it "should works" do result = Qiniu::RS.batch_copy [@bucket, @key, @bucket, @key2] @@ -318,6 +319,7 @@ module Qiniu result3.should_not be_false end end +=end context ".move" do it "should works" do @@ -369,6 +371,7 @@ module Qiniu end end +=begin context ".image_exif" do it "should works" do data = Qiniu::RS.get(@test_image_bucket, @test_image_key) @@ -379,6 +382,7 @@ module Qiniu puts result.inspect end end +=end context ".image_mogrify_save_as" do it "should works" do From ef9c90c7c55013cc4c72a59c7e8f354de53625dc Mon Sep 17 00:00:00 2001 From: Liang Tao Date: Fri, 7 Feb 2014 18:06:02 +0800 Subject: [PATCH 05/21] =?UTF-8?q?=E5=88=A0=E9=99=A4=E5=BA=9F=E5=BC=83?= =?UTF-8?q?=E7=9A=84=E7=99=BB=E5=BD=95=E6=96=B9=E5=BC=8F=E5=AE=9E=E7=8E=B0?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/qiniu/rs.rb | 5 --- lib/qiniu/rs/auth.rb | 80 +++++--------------------------------------- 2 files changed, 8 insertions(+), 77 deletions(-) diff --git a/lib/qiniu/rs.rb b/lib/qiniu/rs.rb index d248d15..fda7d68 100755 --- a/lib/qiniu/rs.rb +++ b/lib/qiniu/rs.rb @@ -28,11 +28,6 @@ module Qiniu Config.initialize_connect opts end - def login!(user, pwd) - code, data = Auth.exchange_by_password!(user, pwd) - code == StatusOK - end - def mkbucket(bucket_name) code, data = RS.mkbucket(bucket_name) code == StatusOK diff --git a/lib/qiniu/rs/auth.rb b/lib/qiniu/rs/auth.rb index bd93a79..9c688c6 100755 --- a/lib/qiniu/rs/auth.rb +++ b/lib/qiniu/rs/auth.rb @@ -5,85 +5,21 @@ require 'qiniu/rs/exceptions' module Qiniu module RS module Auth + class << self + include Utils - def exchange_by_password!(username, password) - @username = username - @password = password - post_data = { - :client_id => Config.settings[:client_id], - :grant_type => "password", - :username => username, - :password => password - } - code, data = http_request Config.settings[:auth_url], post_data - reset_token(data["access_token"], data["refresh_token"]) if Utils.is_response_ok?(code) - [code, data] - end - - def exchange_by_refresh_token!(refresh_token) - post_data = { - :client_id => Config.settings[:client_id], - :grant_type => "refresh_token", - :refresh_token => refresh_token - } - code, data = http_request Config.settings[:auth_url], post_data - reset_token(data["access_token"], data["refresh_token"]) if Utils.is_response_ok?(code) - [code, data] - end - - def reset_token(access_token, refresh_token) - @access_token = access_token - @refresh_token = refresh_token - end - - def call_with_logged_in(url, data, retry_times = 0) - raise MissingAccessToken if @access_token.nil? - code, data = http_request url, data, {:access_token => @access_token} - if code == 401 - raise MissingRefreshToken if @refresh_token.nil? - code, data = exchange_by_refresh_token!(@refresh_token) - if code == 401 - raise MissingUsernameOrPassword if (@username.nil? || @password.nil?) - code, data = exchange_by_password!(@username, @password) - end - if Utils.is_response_ok?(code) - retry_times += 1 - if Config.settings[:auto_reconnect] && retry_times < Config.settings[:max_retry_times] - return call_with_logged_in(url, data, retry_times) - end - end - end - [code, data] - end - def call_with_signature(url, data, retry_times = 0, options = {}) - code, data = 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)}) end def request(url, data = nil, options = {}) - begin - if Config.settings[:access_key].empty? || Config.settings[:secret_key].empty? - code, data = Auth.call_with_logged_in(url, data) - else - code, data = Auth.call_with_signature(url, data, 0, options) - end - rescue MissingAccessToken => e - Log.logger.error e - code, data = 401, {} - rescue MissingRefreshToken => e - Log.logger.error e - code, data = 401, {} - rescue MissingUsernameOrPassword => e - Log.logger.error e - code, data = 401, {} - end - [code, data] + Auth.call_with_signature(url, data, 0, options) end end - end - end -end + + end # module Auth + end # module RS +end # module Qiniu From 08f36221b7f20f8694539e123c5595eb53ed6129 Mon Sep 17 00:00:00 2001 From: Liang Tao Date: Sat, 8 Feb 2014 11:50:53 +0800 Subject: [PATCH 06/21] =?UTF-8?q?=E5=88=A0=E9=99=A4=E8=BF=87=E6=97=B6?= =?UTF-8?q?=E7=9A=84IO.put=5Fauth=E4=B8=8EIO.upload=5Ffile=E7=BB=84?= =?UTF-8?q?=E5=90=88=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/qiniu/rs.rb | 17 --------------- lib/qiniu/rs/io.rb | 17 --------------- spec/qiniu/rs/io_spec.rb | 13 ----------- spec/qiniu/rs/rs_spec.rb | 13 +++-------- spec/qiniu/rs_spec.rb | 47 ++++++++++------------------------------ 5 files changed, 14 insertions(+), 93 deletions(-) diff --git a/lib/qiniu/rs.rb b/lib/qiniu/rs.rb index fda7d68..3b7323b 100755 --- a/lib/qiniu/rs.rb +++ b/lib/qiniu/rs.rb @@ -70,23 +70,6 @@ module Qiniu end =end - def put_auth(expires_in = nil, callback_url = nil) - code, data = IO.put_auth(expires_in, callback_url) - code == StatusOK ? data["url"] : false - end - - def upload opts = {} - code, data = IO.upload_file(opts[:url], - opts[:file], - opts[:bucket], - opts[:key], - opts[:mime_type], - opts[:note], - opts[:callback_params], - opts[:enable_crc32_check]) - code == StatusOK - end - def put_file opts = {} code, data = IO.put_file(opts[:file], opts[:bucket], diff --git a/lib/qiniu/rs/io.rb b/lib/qiniu/rs/io.rb index cccabe3..8c7739d 100755 --- a/lib/qiniu/rs/io.rb +++ b/lib/qiniu/rs/io.rb @@ -10,23 +10,6 @@ module Qiniu class << self include Utils - def put_auth(expires_in = nil, callback_url = nil) - url = Config.settings[:io_host] + "/put-auth/" - url += "#{expires_in}" if !expires_in.nil? && expires_in > 0 - if !callback_url.nil? && !callback_url.empty? - encoded_callback_url = Utils.urlsafe_base64_encode(callback_url) - url += "/callback/#{encoded_callback_url}" - end - Auth.request(url) - end - - def upload_file(url, 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) - Utils.upload_multipart_data(url, local_file, action_params, callback_query_string) - end - def put_file(local_file, bucket, key = nil, mime_type = nil, custom_meta = nil, enable_crc32_check = false) action_params = _generate_action_params(local_file, bucket, key, mime_type, custom_meta, enable_crc32_check) url = Config.settings[:io_host] + action_params diff --git a/spec/qiniu/rs/io_spec.rb b/spec/qiniu/rs/io_spec.rb index 51075b9..3b0f7bb 100755 --- a/spec/qiniu/rs/io_spec.rb +++ b/spec/qiniu/rs/io_spec.rb @@ -24,19 +24,6 @@ module Qiniu result.should_not be_false end - context ".upload_file" do - it "should works" do - code, data = Qiniu::RS::IO.put_auth() - code.should == 200 - data["url"].should_not be_empty - data["expiresIn"].should_not be_zero - puts data.inspect - code2, data2 = Qiniu::RS::IO.upload_file(data["url"], __FILE__, @bucket, @key) - code2.should == 200 - puts data2.inspect - end - end - context ".put_file" do it "should works" do code, data = Qiniu::RS::IO.put_file(__FILE__, @bucket, @key, 'application/x-ruby', 'customMeta', true) diff --git a/spec/qiniu/rs/rs_spec.rb b/spec/qiniu/rs/rs_spec.rb index 67569ae..3954c85 100755 --- a/spec/qiniu/rs/rs_spec.rb +++ b/spec/qiniu/rs/rs_spec.rb @@ -27,18 +27,11 @@ module Qiniu code.should == 200 end - context "IO.upload_file" do + context ".put_file" do it "should works" do - code, data = Qiniu::RS::IO.put_auth() - puts data.inspect + code, data = Qiniu::RS::IO.put_file(__FILE__, @bucket, @key, 'application/x-ruby', 'customMeta', true) code.should == 200 - data["url"].should_not be_empty - data["expiresIn"].should_not be_zero - @put_url = data["url"] - - code2, data2 = Qiniu::RS::IO.upload_file(@put_url, __FILE__, @bucket, @key) - puts data2.inspect - code2.should == 200 + puts data.inspect end end diff --git a/spec/qiniu/rs_spec.rb b/spec/qiniu/rs_spec.rb index 22a1699..8c0dd80 100755 --- a/spec/qiniu/rs_spec.rb +++ b/spec/qiniu/rs_spec.rb @@ -44,6 +44,17 @@ module Qiniu result2.should_not be_false end + context ".put_file" do + it "should works" do + result = Qiniu::RS.put_file :file => __FILE__, + :bucket => @bucket, + :key => @key, + :mime_type => 'application/x-ruby', + :enable_crc32_check => true + result.should be_true + end + end + context ".buckets" do it "should works" do result = Qiniu::RS.buckets @@ -105,42 +116,6 @@ module Qiniu end =end - context ".put_auth" do - it "should works" do - result = Qiniu::RS.put_auth(10) - result.should_not be_false - result.should_not be_empty - puts result.inspect - end - end - - context ".upload" do - it "should works" do - put_url = Qiniu::RS.put_auth(10) - put_url.should_not be_false - put_url.should_not be_empty - puts put_url.inspect - result = Qiniu::RS.upload :url => put_url, - :file => __FILE__, - :bucket => @bucket, - :key => @key, - :mime_type => 'application/x-ruby', - :enable_crc32_check => true - result.should be_true - end - end - - context ".put_file" do - it "should works" do - result = Qiniu::RS.put_file :file => __FILE__, - :bucket => @bucket, - :key => @key, - :mime_type => 'application/x-ruby', - :enable_crc32_check => true - result.should be_true - end - end - context ".upload_file" do it "should works" do uptoken_opts = {:scope => @bucket, :escape => 0} From cb748d8857f844a0e2ce07b1cdadf3e05deeab52 Mon Sep 17 00:00:00 2001 From: Liang Tao Date: Sat, 8 Feb 2014 13:00:07 +0800 Subject: [PATCH 07/21] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=9C=80=E6=96=B0?= =?UTF-8?q?=E7=9A=84=E5=8D=95=E6=96=87=E4=BB=B6=E7=9B=B4=E4=BC=A0=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0=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 From 1a1affc3d3704f0b0f45091ad4f10c1ef2409d76 Mon Sep 17 00:00:00 2001 From: Liang Tao Date: Sat, 8 Feb 2014 15:14:47 +0800 Subject: [PATCH 08/21] =?UTF-8?q?=E5=88=A0=E9=99=A4=E5=B7=B2=E7=BB=8F?= =?UTF-8?q?=E5=BA=9F=E5=BC=83=E7=9A=84EU=E6=9C=8D=E5=8A=A1=E7=9B=B8?= =?UTF-8?q?=E5=85=B3=E4=BB=A3=E7=A0=81=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/qiniu/rs.rb | 12 -------- lib/qiniu/rs/eu.rb | 22 -------------- spec/qiniu/rs/eu_spec.rb | 66 ---------------------------------------- spec/qiniu/rs_spec.rb | 21 ------------- 4 files changed, 121 deletions(-) delete mode 100755 lib/qiniu/rs/eu.rb delete mode 100755 spec/qiniu/rs/eu_spec.rb diff --git a/lib/qiniu/rs.rb b/lib/qiniu/rs.rb index 3b7323b..0c0f457 100755 --- a/lib/qiniu/rs.rb +++ b/lib/qiniu/rs.rb @@ -58,18 +58,6 @@ module Qiniu code == StatusOK end -=begin - def set_watermark(customer_id, options = {}) - code, data = EU.set_watermark(customer_id, options) - code == StatusOK - end - - def get_watermark(customer_id = nil) - code, data = EU.get_watermark(customer_id) - code == StatusOK ? data : false - end -=end - def put_file opts = {} code, data = IO.put_file(opts[:file], opts[:bucket], diff --git a/lib/qiniu/rs/eu.rb b/lib/qiniu/rs/eu.rb deleted file mode 100755 index f684417..0000000 --- a/lib/qiniu/rs/eu.rb +++ /dev/null @@ -1,22 +0,0 @@ -# -*- encoding: utf-8 -*- - -module Qiniu - module RS - module EU - class << self - include Utils - - def set_watermark(customer_id, options = {}) - Auth.request Config.settings[:eu_host] + '/wmset', options.merge({:customer => customer_id}) - end - - def get_watermark(customer_id = nil) - post_data = customer_id.nil? ? nil : {:customer => customer_id} - Auth.request Config.settings[:eu_host] + '/wmget', post_data - end - - end - end - end -end - diff --git a/spec/qiniu/rs/eu_spec.rb b/spec/qiniu/rs/eu_spec.rb deleted file mode 100755 index 5e5894a..0000000 --- a/spec/qiniu/rs/eu_spec.rb +++ /dev/null @@ -1,66 +0,0 @@ -# -*- encoding: utf-8 -*- - -require 'spec_helper' -require 'qiniu/rs' -require 'qiniu/rs/pub' -require 'qiniu/rs/eu' - -module Qiniu - module RS - describe EU do - - before :all do - @customer_id = "why404@gmail.com" - @bucket = 'RubySdkTest' + (Time.now.to_i+rand(1000)).to_s - @key = "image_logo_for_test.png" - - result = Qiniu::RS.mkbucket(@bucket) - puts result.inspect - result.should be_true - - local_file = File.expand_path('../' + @key, __FILE__) - upopts = {:scope => @bucket, :expires_in => 3600, :customer => @customer_id} - uptoken = Qiniu::RS.generate_upload_token(upopts) - - data = Qiniu::RS.upload_file :uptoken => uptoken, :file => local_file, :bucket => @bucket, :key => @key - puts data.inspect - - code2, data2 = Qiniu::RS::Pub.set_separator(@bucket, "-") - code2.should == 200 - puts data2.inspect - - code3, data3 = Qiniu::RS::Pub.set_style(@bucket, "small.jpg", "imageView/1/w/120/h/120/q/85/format/jpg/watermark/1") - code3.should == 200 - puts data3.inspect - end - - after :all do - result = Qiniu::RS.drop(@bucket) - puts result.inspect - result.should_not be_false - end - -=begin - context ".set_watermark" do - it "should works" do - options = { - :text => "Powered by QiniuRS" - } - code, data = Qiniu::RS::EU.set_watermark(@customer_id, options) - code.should == 200 - puts data.inspect - end - end - - context ".get_watermark" do - it "should works" do - code, data = Qiniu::RS::EU.get_watermark(@customer_id) - code.should == 200 - puts data.inspect - end - end -=end - - end - end -end diff --git a/spec/qiniu/rs_spec.rb b/spec/qiniu/rs_spec.rb index 8c0dd80..4665b12 100755 --- a/spec/qiniu/rs_spec.rb +++ b/spec/qiniu/rs_spec.rb @@ -95,27 +95,6 @@ module Qiniu end end -=begin - context ".set_watermark" do - it "should works" do - options = { - :text => "Powered by QiniuRS" - } - result = Qiniu::RS.set_watermark(1, options) - result.should_not be_false - puts result.inspect - end - end - - context ".get_watermark" do - it "should works" do - result = Qiniu::RS.get_watermark(1) - result.should_not be_false - puts result.inspect - end - end -=end - context ".upload_file" do it "should works" do uptoken_opts = {:scope => @bucket, :escape => 0} From 5e8e69a339e2865ba0967d0bdfc5ab311d2b6f61 Mon Sep 17 00:00:00 2001 From: Liang Tao Date: Sat, 8 Feb 2014 17:11:25 +0800 Subject: [PATCH 09/21] =?UTF-8?q?=E4=BD=BF=E7=94=A8=E6=9C=80=E6=96=B0?= =?UTF-8?q?=E7=9A=84imageInfo=E6=8E=A5=E5=8F=A3=E8=A7=84=E6=A0=BC=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/qiniu/rs/image.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/qiniu/rs/image.rb b/lib/qiniu/rs/image.rb index 63091b5..78e128e 100755 --- a/lib/qiniu/rs/image.rb +++ b/lib/qiniu/rs/image.rb @@ -7,12 +7,12 @@ module Qiniu include Utils def info(url) - Utils.http_request url + '/imageInfo', nil, {:method => :get} - end + Utils.http_request url + '?imageInfo', nil, {:method => :get} + end # info def exif(url) Utils.http_request url + '?exif', nil, {:method => :get} - end + end # exif def preivew_url(url, spec) url + '/imagePreview/' + spec.to_s From 3a3d0268126e347809cae86a5e26e57666e97578 Mon Sep 17 00:00:00 2001 From: Liang Tao Date: Sat, 8 Feb 2014 17:24:43 +0800 Subject: [PATCH 10/21] =?UTF-8?q?=E5=88=A0=E9=99=A4=E8=BF=87=E6=9C=9F?= =?UTF-8?q?=E7=9A=84preview=E6=8E=A5=E5=8F=A3=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/qiniu/rs.rb | 4 ---- lib/qiniu/rs/image.rb | 4 ---- 2 files changed, 8 deletions(-) diff --git a/lib/qiniu/rs.rb b/lib/qiniu/rs.rb index 0c0f457..d15216f 100755 --- a/lib/qiniu/rs.rb +++ b/lib/qiniu/rs.rb @@ -195,10 +195,6 @@ module Qiniu code == StatusOK ? data : false end - def image_preview_url(url, spec) - Image.preivew_url(url, spec) - end - def image_mogrify_preview_url(source_image_url, options) Image.mogrify_preview_url(source_image_url, options) end diff --git a/lib/qiniu/rs/image.rb b/lib/qiniu/rs/image.rb index 78e128e..9dbdf7c 100755 --- a/lib/qiniu/rs/image.rb +++ b/lib/qiniu/rs/image.rb @@ -14,10 +14,6 @@ module Qiniu Utils.http_request url + '?exif', nil, {:method => :get} end # exif - def preivew_url(url, spec) - url + '/imagePreview/' + spec.to_s - end - def mogrify_preview_url(source_image_url, options) source_image_url + '?' + generate_mogrify_params_string(options) end From de61097fde15b05aa8769cf64a27663bfb8743b7 Mon Sep 17 00:00:00 2001 From: Liang Tao Date: Tue, 25 Feb 2014 16:58:18 +0800 Subject: [PATCH 11/21] =?UTF-8?q?=E5=8E=BB=E9=99=A4=E7=BB=9F=E4=B8=80?= =?UTF-8?q?=E7=9A=84RS=E7=A9=BA=E9=97=B4=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/qiniu-rs.rb | 2 +- lib/qiniu.rb | 231 ++++++++++ lib/qiniu/{rs => }/abstract.rb | 6 +- lib/qiniu/{rs => }/auth.rb | 4 +- lib/qiniu/{rs => }/config.rb | 6 +- lib/qiniu/{rs => }/exceptions.rb | 4 +- lib/qiniu/{rs => }/image.rb | 6 +- lib/qiniu/{rs => }/io.rb | 8 +- lib/qiniu/{rs => }/log.rb | 6 +- lib/qiniu/{rs => }/pub.rb | 6 +- lib/qiniu/rs.rb | 339 +++++--------- lib/qiniu/rs/rs.rb | 119 ----- lib/qiniu/tokens/access_token.rb | 10 +- lib/qiniu/tokens/download_token.rb | 8 +- lib/qiniu/tokens/qbox_token.rb | 6 +- lib/qiniu/tokens/upload_token.rb | 8 +- lib/qiniu/{rs => }/up.rb | 16 +- lib/qiniu/{rs => }/utils.rb | 8 +- lib/qiniu/{rs => }/version.rb | 6 +- spec/qiniu/{rs => }/abstract_spec.rb | 6 +- spec/qiniu/{rs => }/image_logo_for_test.png | Bin spec/qiniu/{rs => }/image_spec.rb | 26 +- spec/qiniu/{rs => }/io_spec.rb | 24 +- spec/qiniu/{rs => }/pub_spec.rb | 24 +- spec/qiniu/qiniu_spec.rb | 385 ++++++++++++++++ spec/qiniu/rs/rs_spec.rb | 180 -------- spec/qiniu/rs/version_spec.rb | 10 - spec/qiniu/rs_spec.rb | 473 ++++++-------------- spec/qiniu/{rs => }/up_spec.rb | 54 +-- spec/qiniu/{rs => }/utils_spec.rb | 2 +- spec/qiniu/version_spec.rb | 10 + spec/spec_helper.rb | 4 +- 32 files changed, 981 insertions(+), 1016 deletions(-) create mode 100755 lib/qiniu.rb rename lib/qiniu/{rs => }/abstract.rb (92%) rename lib/qiniu/{rs => }/auth.rb (88%) rename lib/qiniu/{rs => }/config.rb (97%) rename lib/qiniu/{rs => }/exceptions.rb (99%) rename lib/qiniu/{rs => }/image.rb (95%) rename lib/qiniu/{rs => }/io.rb (97%) rename lib/qiniu/{rs => }/log.rb (83%) rename lib/qiniu/{rs => }/pub.rb (96%) delete mode 100755 lib/qiniu/rs/rs.rb rename lib/qiniu/{rs => }/up.rb (98%) rename lib/qiniu/{rs => }/utils.rb (98%) rename lib/qiniu/{rs => }/version.rb (90%) rename spec/qiniu/{rs => }/abstract_spec.rb (82%) rename spec/qiniu/{rs => }/image_logo_for_test.png (100%) rename spec/qiniu/{rs => }/image_spec.rb (74%) rename spec/qiniu/{rs => }/io_spec.rb (65%) rename spec/qiniu/{rs => }/pub_spec.rb (63%) create mode 100755 spec/qiniu/qiniu_spec.rb delete mode 100755 spec/qiniu/rs/rs_spec.rb delete mode 100755 spec/qiniu/rs/version_spec.rb rename spec/qiniu/{rs => }/up_spec.rb (66%) rename spec/qiniu/{rs => }/utils_spec.rb (98%) create mode 100755 spec/qiniu/version_spec.rb diff --git a/lib/qiniu-rs.rb b/lib/qiniu-rs.rb index 75057b3..1c54a93 100755 --- a/lib/qiniu-rs.rb +++ b/lib/qiniu-rs.rb @@ -1,2 +1,2 @@ # More logical way to require 'qiniu-rs' -require File.join(File.dirname(__FILE__), 'qiniu', 'rs') +require File.join(File.dirname(__FILE__), 'qiniu', 'qiniu') diff --git a/lib/qiniu.rb b/lib/qiniu.rb new file mode 100755 index 0000000..c766a04 --- /dev/null +++ b/lib/qiniu.rb @@ -0,0 +1,231 @@ +# -*- encoding: utf-8 -*- + +module Qiniu + autoload :Version, 'qiniu/version' + autoload :Utils, 'qiniu/utils' + autoload :Auth, 'qiniu/auth' + autoload :Config, 'qiniu/config' + autoload :Log, 'qiniu/log' + autoload :Exception, 'qiniu/exceptions' + autoload :IO, 'qiniu/io' + autoload :UP, 'qiniu/up' + autoload :RS, 'qiniu/rs' + autoload :Pub, 'qiniu/pub' + autoload :Image, 'qiniu/image' + 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/abstract' + + class << self + + StatusOK = 200 + + def establish_connection!(opts = {}) + Config.initialize_connect opts + end + + def mkbucket(bucket_name) + code, data = RS.mkbucket(bucket_name) + code == StatusOK + end + + def buckets + code, data = RS.buckets + code == StatusOK ? data : false + end + + def set_protected(bucket, protected_mode) + code, data = Pub.set_protected(bucket, protected_mode) + code == StatusOK + end + + def set_separator(bucket, separator) + code, data = Pub.set_separator(bucket, separator) + code == StatusOK + end + + def set_style(bucket, name, style) + code, data = Pub.set_style(bucket, name, style) + code == StatusOK + end + + def unset_style(bucket, name) + code, data = Pub.unset_style(bucket, name) + code == StatusOK + end + + def put_file opts = {} + code, data = IO.put_file(opts[:file], + opts[:bucket], + opts[:key], + opts[:mime_type], + opts[:note], + opts[:enable_crc32_check]) + code == StatusOK + end + + def upload_file opts = {} + uncontained_opts = [:uptoken, :file, :bucket, :key] - opts.keys + raise MissingArgsError, uncontained_opts unless uncontained_opts.empty? + + source_file = opts[:file] + raise NoSuchFileError, source_file unless File.exist?(source_file) + + opts[:enable_resumable_upload] = true unless opts.has_key?(:enable_resumable_upload) + + if opts[:enable_resumable_upload] && File::size(source_file) > Config.settings[:block_size] + code, data = UP.upload_with_token(opts[:uptoken], + opts[:file], + opts[:bucket], + opts[:key], + opts[:mime_type], + opts[:note], + opts[:customer], + opts[:callback_params], + opts[:rotate]) + else + 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], + opts[:rotate]) + end + raise UploadFailedError.new(code, data) if code != StatusOK + return data + end + + def stat(bucket, key) + code, data = RS.stat(bucket, key) + code == StatusOK ? data : false + end + + def get(bucket, key, save_as = nil, expires_in = nil, version = nil) + code, data = RS.get(bucket, key, save_as, expires_in, version) + code == StatusOK ? data : false + end + + def download(bucket, key, save_as = nil, expires_in = nil, version = nil) + code, data = RS.get(bucket, key, save_as, expires_in, version) + code == StatusOK ? data["url"] : false + end + + def copy(source_bucket, source_key, target_bucket, target_key) + code, data = RS.copy(source_bucket, source_key, target_bucket, target_key) + code == StatusOK + end + + def move(source_bucket, source_key, target_bucket, target_key) + code, data = RS.move(source_bucket, source_key, target_bucket, target_key) + code == StatusOK + end + + def delete(bucket, key) + code, data = RS.delete(bucket, key) + code == StatusOK + end + + def batch(command, bucket, keys) + code, data = RS.batch(command, bucket, keys) + code == StatusOK ? data : false + end + + def batch_stat(bucket, keys) + code, data = RS.batch_stat(bucket, keys) + code == StatusOK ? data : false + end + + def batch_get(bucket, keys) + code, data = RS.batch_get(bucket, keys) + code == StatusOK ? data : false + end + + def batch_copy(*args) + code, data = RS.batch_copy(args) + code == StatusOK + end + + def batch_move(*args) + code, data = RS.batch_move(args) + code == StatusOK + end + + def batch_download(bucket, keys) + code, data = RS.batch_get(bucket, keys) + return false unless code == StatusOK + links = [] + data.each { |e| links << e["data"]["url"] } + links + end + + def batch_delete(bucket, keys) + code, data = RS.batch_delete(bucket, keys) + code == StatusOK ? data : false + end + + def publish(domain, bucket) + code, data = RS.publish(domain, bucket) + code == StatusOK + end + + def unpublish(domain) + code, data = RS.unpublish(domain) + code == StatusOK + end + + def drop(bucket) + code, data = RS.drop(bucket) + code == StatusOK + end + + def image_info(url) + code, data = Image.info(url) + code == StatusOK ? data : false + end + + def image_exif(url) + code, data = Image.exif(url) + code == StatusOK ? data : false + end + + def image_mogrify_preview_url(source_image_url, options) + Image.mogrify_preview_url(source_image_url, options) + end + + def image_mogrify_save_as(bucket, key, source_image_url, options) + code, data = RS.image_mogrify_save_as(bucket, key, source_image_url, options) + code == StatusOK ? data : false + end + + def generate_upload_token(opts = {}) + token_obj = UploadToken.new(opts) + token_obj.access_key = Config.settings[:access_key] + token_obj.secret_key = Config.settings[:secret_key] + #token_obj.scope = opts[:scope] + #token_obj.expires_in = opts[:expires_in] + #token_obj.callback_url = opts[:callback_url] + #token_obj.callback_body_type = opts[:callback_body_type] + #token_obj.customer = opts[:customer] + #token_obj.escape = opts[:escape] + #token_obj.async_options = opts[:async_options] + #token_obj.return_body = opts[:return_body] + 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 # module Qiniu diff --git a/lib/qiniu/rs/abstract.rb b/lib/qiniu/abstract.rb similarity index 92% rename from lib/qiniu/rs/abstract.rb rename to lib/qiniu/abstract.rb index d9fc671..af32cba 100644 --- a/lib/qiniu/rs/abstract.rb +++ b/lib/qiniu/abstract.rb @@ -1,7 +1,6 @@ # -*- encoding: utf-8 -*- module Qiniu - module RS module Abstract def self.included(base) base.extend(ClassMethods) @@ -19,6 +18,5 @@ module Qiniu end end end - end - end -end + end # module Abstract +end # module Qiniu diff --git a/lib/qiniu/rs/auth.rb b/lib/qiniu/auth.rb similarity index 88% rename from lib/qiniu/rs/auth.rb rename to lib/qiniu/auth.rb index 9c688c6..38b4813 100755 --- a/lib/qiniu/rs/auth.rb +++ b/lib/qiniu/auth.rb @@ -1,9 +1,8 @@ # -*- encoding: utf-8 -*- -require 'qiniu/rs/exceptions' +require 'qiniu/exceptions' module Qiniu - module RS module Auth class << self @@ -21,5 +20,4 @@ module Qiniu end end # module Auth - end # module RS end # module Qiniu diff --git a/lib/qiniu/rs/config.rb b/lib/qiniu/config.rb similarity index 97% rename from lib/qiniu/rs/config.rb rename to lib/qiniu/config.rb index d7f65df..53216c3 100755 --- a/lib/qiniu/rs/config.rb +++ b/lib/qiniu/config.rb @@ -11,7 +11,6 @@ require 'tmpdir' module Qiniu - module RS module Config class << self @@ -58,6 +57,5 @@ module Qiniu end end - end - end -end + end # module Config +end # module Qiniu diff --git a/lib/qiniu/rs/exceptions.rb b/lib/qiniu/exceptions.rb similarity index 99% rename from lib/qiniu/rs/exceptions.rb rename to lib/qiniu/exceptions.rb index fa123f9..352da9c 100755 --- a/lib/qiniu/rs/exceptions.rb +++ b/lib/qiniu/exceptions.rb @@ -1,7 +1,6 @@ # -*- encoding: utf-8 -*- module Qiniu - module RS class Exception < RuntimeError end @@ -118,5 +117,4 @@ module Qiniu end end - end -end +end # module Qiniu diff --git a/lib/qiniu/rs/image.rb b/lib/qiniu/image.rb similarity index 95% rename from lib/qiniu/rs/image.rb rename to lib/qiniu/image.rb index 9dbdf7c..0d7aa95 100755 --- a/lib/qiniu/rs/image.rb +++ b/lib/qiniu/image.rb @@ -1,7 +1,6 @@ # -*- encoding: utf-8 -*- module Qiniu - module RS module Image class << self include Utils @@ -33,6 +32,5 @@ module Qiniu end end - end - end -end + end # module Image +end # module Qiniu diff --git a/lib/qiniu/rs/io.rb b/lib/qiniu/io.rb similarity index 97% rename from lib/qiniu/rs/io.rb rename to lib/qiniu/io.rb index 934d027..b8311b8 100755 --- a/lib/qiniu/rs/io.rb +++ b/lib/qiniu/io.rb @@ -2,10 +2,9 @@ require 'mime/types' require 'digest/sha1' -require 'qiniu/rs/exceptions' +require 'qiniu/exceptions' module Qiniu - module RS module IO class << self include Utils @@ -67,6 +66,5 @@ module Qiniu end end - end - end -end + end # module IO +end # module Qiniu diff --git a/lib/qiniu/rs/log.rb b/lib/qiniu/log.rb similarity index 83% rename from lib/qiniu/rs/log.rb rename to lib/qiniu/log.rb index 0a3c8a0..490fe56 100755 --- a/lib/qiniu/rs/log.rb +++ b/lib/qiniu/log.rb @@ -3,7 +3,6 @@ require 'logger' module Qiniu - module RS module Log class << self attr_accessor :logger @@ -12,6 +11,5 @@ module Qiniu @logger ||= Logger.new(STDERR) end end - end - end -end + end # module Log +end # module Qiniu diff --git a/lib/qiniu/rs/pub.rb b/lib/qiniu/pub.rb similarity index 96% rename from lib/qiniu/rs/pub.rb rename to lib/qiniu/pub.rb index b1e4df2..b384d0c 100755 --- a/lib/qiniu/rs/pub.rb +++ b/lib/qiniu/pub.rb @@ -1,7 +1,6 @@ # -*- encoding: utf-8 -*- module Qiniu - module RS module Pub class << self include Utils @@ -31,7 +30,6 @@ module Qiniu end end - end - end -end + end # module Pub +end # module Qiniu diff --git a/lib/qiniu/rs.rb b/lib/qiniu/rs.rb index d15216f..9ba9ff2 100755 --- a/lib/qiniu/rs.rb +++ b/lib/qiniu/rs.rb @@ -1,234 +1,117 @@ # -*- encoding: utf-8 -*- module Qiniu - module RS - autoload :Version, 'qiniu/rs/version' - autoload :Config, 'qiniu/rs/config' - autoload :Log, 'qiniu/rs/log' - autoload :Exception, 'qiniu/rs/exceptions' - autoload :Utils, 'qiniu/rs/utils' - autoload :Auth, 'qiniu/rs/auth' - autoload :IO, 'qiniu/rs/io' - autoload :UP, 'qiniu/rs/up' - autoload :RS, 'qiniu/rs/rs' - autoload :EU, 'qiniu/rs/eu' - autoload :Pub, 'qiniu/rs/pub' - autoload :Image, 'qiniu/rs/image' - 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' + module RS + class << self + include Utils - class << self - - StatusOK = 200 - - def establish_connection!(opts = {}) - Config.initialize_connect opts - end - - def mkbucket(bucket_name) - code, data = RS.mkbucket(bucket_name) - code == StatusOK - end - - def buckets - code, data = RS.buckets - code == StatusOK ? data : false - end - - def set_protected(bucket, protected_mode) - code, data = Pub.set_protected(bucket, protected_mode) - code == StatusOK - end - - def set_separator(bucket, separator) - code, data = Pub.set_separator(bucket, separator) - code == StatusOK - end - - def set_style(bucket, name, style) - code, data = Pub.set_style(bucket, name, style) - code == StatusOK - end - - def unset_style(bucket, name) - code, data = Pub.unset_style(bucket, name) - code == StatusOK - end - - def put_file opts = {} - code, data = IO.put_file(opts[:file], - opts[:bucket], - opts[:key], - opts[:mime_type], - opts[:note], - opts[:enable_crc32_check]) - code == StatusOK - end - - def upload_file opts = {} - uncontained_opts = [:uptoken, :file, :bucket, :key] - opts.keys - raise MissingArgsError, uncontained_opts unless uncontained_opts.empty? - - source_file = opts[:file] - raise NoSuchFileError, source_file unless File.exist?(source_file) - - opts[:enable_resumable_upload] = true unless opts.has_key?(:enable_resumable_upload) - - if opts[:enable_resumable_upload] && File::size(source_file) > Config.settings[:block_size] - code, data = UP.upload_with_token(opts[:uptoken], - opts[:file], - opts[:bucket], - opts[:key], - opts[:mime_type], - opts[:note], - opts[:customer], - opts[:callback_params], - opts[:rotate]) - else - 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], - opts[:rotate]) + def buckets + Auth.request Config.settings[:rs_host] + '/buckets' end - raise UploadFailedError.new(code, data) if code != StatusOK - return data + + def mkbucket(bucket_name) + Auth.request Config.settings[:rs_host] + '/mkbucket/' + bucket_name + end + + def stat(bucket, key) + Auth.request Config.settings[:rs_host] + '/stat/' + encode_entry_uri(bucket, key) + end + + def get(bucket, key, save_as = nil, expires_in = nil, version = nil) + url = Config.settings[:rs_host] + '/get/' + encode_entry_uri(bucket, key) + url += '/base/' + version unless version.nil? + url += '/attName/' + Utils.urlsafe_base64_encode(save_as) unless save_as.nil? + url += '/expires/' + expires_in.to_s if !expires_in.nil? && expires_in > 0 + Auth.request url + end + + def copy(source_bucket, source_key, target_bucket, target_key) + uri = _generate_cp_or_mv_opstr('copy', source_bucket, source_key, target_bucket, target_key) + Auth.request Config.settings[:rs_host] + uri + end + + def move(source_bucket, source_key, target_bucket, target_key) + uri = _generate_cp_or_mv_opstr('move', source_bucket, source_key, target_bucket, target_key) + Auth.request Config.settings[:rs_host] + uri + end + + def delete(bucket, key) + Auth.request Config.settings[:rs_host] + '/delete/' + encode_entry_uri(bucket, key) + end + + def publish(domain, bucket) + encoded_domain = Utils.urlsafe_base64_encode(domain) + Auth.request Config.settings[:rs_host] + "/publish/#{encoded_domain}/from/#{bucket}" + end + + def unpublish(domain) + encoded_domain = Utils.urlsafe_base64_encode(domain) + Auth.request Config.settings[:rs_host] + "/unpublish/#{encoded_domain}" + end + + def drop(bucket) + Auth.request Config.settings[:rs_host] + "/drop/#{bucket}" + end + + def batch(command, bucket, keys) + execs = [] + keys.each do |key| + encoded_uri = encode_entry_uri(bucket, key) + execs << "op=/#{command}/#{encoded_uri}" + end + Auth.request Config.settings[:rs_host] + "/batch?" + execs.join("&") + end + + def batch_get(bucket, keys) + batch("get", bucket, keys) + end + + def batch_stat(bucket, keys) + batch("stat", bucket, keys) + end + + def batch_copy(*args) + _batch_cp_or_mv('copy', args) + end + + def batch_move(*args) + _batch_cp_or_mv('move', args) + end + + def batch_delete(bucket, keys) + batch("delete", bucket, keys) + end + + def save_as(bucket, key, source_url, op_params_string) + encoded_uri = encode_entry_uri(bucket, key) + save_as_string = '/save-as/' + encoded_uri + new_url = source_url + '?' + op_params_string + save_as_string + Auth.request new_url + end + + def image_mogrify_save_as(bucket, key, source_image_url, options) + mogrify_params_string = Image.generate_mogrify_params_string(options) + save_as(bucket, key, source_image_url, mogrify_params_string) + end + + private + + def _generate_cp_or_mv_opstr(command, source_bucket, source_key, target_bucket, target_key) + source_encoded_entry_uri = encode_entry_uri(source_bucket, source_key) + target_encoded_entry_uri = encode_entry_uri(target_bucket, target_key) + %Q(/#{command}/#{source_encoded_entry_uri}/#{target_encoded_entry_uri}) + end + +=begin + 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("&") + end +=end + end - - def stat(bucket, key) - code, data = RS.stat(bucket, key) - code == StatusOK ? data : false - end - - def get(bucket, key, save_as = nil, expires_in = nil, version = nil) - code, data = RS.get(bucket, key, save_as, expires_in, version) - code == StatusOK ? data : false - end - - def download(bucket, key, save_as = nil, expires_in = nil, version = nil) - code, data = RS.get(bucket, key, save_as, expires_in, version) - code == StatusOK ? data["url"] : false - end - - def copy(source_bucket, source_key, target_bucket, target_key) - code, data = RS.copy(source_bucket, source_key, target_bucket, target_key) - code == StatusOK - end - - def move(source_bucket, source_key, target_bucket, target_key) - code, data = RS.move(source_bucket, source_key, target_bucket, target_key) - code == StatusOK - end - - def delete(bucket, key) - code, data = RS.delete(bucket, key) - code == StatusOK - end - - def batch(command, bucket, keys) - code, data = RS.batch(command, bucket, keys) - code == StatusOK ? data : false - end - - def batch_stat(bucket, keys) - code, data = RS.batch_stat(bucket, keys) - code == StatusOK ? data : false - end - - def batch_get(bucket, keys) - code, data = RS.batch_get(bucket, keys) - code == StatusOK ? data : false - end - - def batch_copy(*args) - code, data = RS.batch_copy(args) - code == StatusOK - end - - def batch_move(*args) - code, data = RS.batch_move(args) - code == StatusOK - end - - def batch_download(bucket, keys) - code, data = RS.batch_get(bucket, keys) - return false unless code == StatusOK - links = [] - data.each { |e| links << e["data"]["url"] } - links - end - - def batch_delete(bucket, keys) - code, data = RS.batch_delete(bucket, keys) - code == StatusOK ? data : false - end - - def publish(domain, bucket) - code, data = RS.publish(domain, bucket) - code == StatusOK - end - - def unpublish(domain) - code, data = RS.unpublish(domain) - code == StatusOK - end - - def drop(bucket) - code, data = RS.drop(bucket) - code == StatusOK - end - - def image_info(url) - code, data = Image.info(url) - code == StatusOK ? data : false - end - - def image_exif(url) - code, data = Image.exif(url) - code == StatusOK ? data : false - end - - def image_mogrify_preview_url(source_image_url, options) - Image.mogrify_preview_url(source_image_url, options) - end - - def image_mogrify_save_as(bucket, key, source_image_url, options) - code, data = RS.image_mogrify_save_as(bucket, key, source_image_url, options) - code == StatusOK ? data : false - end - - def generate_upload_token(opts = {}) - token_obj = UploadToken.new(opts) - token_obj.access_key = Config.settings[:access_key] - token_obj.secret_key = Config.settings[:secret_key] - #token_obj.scope = opts[:scope] - #token_obj.expires_in = opts[:expires_in] - #token_obj.callback_url = opts[:callback_url] - #token_obj.callback_body_type = opts[:callback_body_type] - #token_obj.customer = opts[:customer] - #token_obj.escape = opts[:escape] - #token_obj.async_options = opts[:async_options] - #token_obj.return_body = opts[:return_body] - 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 -end + end # module RS +end # module Qiniu diff --git a/lib/qiniu/rs/rs.rb b/lib/qiniu/rs/rs.rb deleted file mode 100755 index d95c7b6..0000000 --- a/lib/qiniu/rs/rs.rb +++ /dev/null @@ -1,119 +0,0 @@ -# -*- encoding: utf-8 -*- - -module Qiniu - module RS - module RS - class << self - include Utils - - def buckets - Auth.request Config.settings[:rs_host] + '/buckets' - end - - def mkbucket(bucket_name) - Auth.request Config.settings[:rs_host] + '/mkbucket/' + bucket_name - end - - def stat(bucket, key) - Auth.request Config.settings[:rs_host] + '/stat/' + encode_entry_uri(bucket, key) - end - - def get(bucket, key, save_as = nil, expires_in = nil, version = nil) - url = Config.settings[:rs_host] + '/get/' + encode_entry_uri(bucket, key) - url += '/base/' + version unless version.nil? - url += '/attName/' + Utils.urlsafe_base64_encode(save_as) unless save_as.nil? - url += '/expires/' + expires_in.to_s if !expires_in.nil? && expires_in > 0 - Auth.request url - end - - def copy(source_bucket, source_key, target_bucket, target_key) - uri = _generate_cp_or_mv_opstr('copy', source_bucket, source_key, target_bucket, target_key) - Auth.request Config.settings[:rs_host] + uri - end - - def move(source_bucket, source_key, target_bucket, target_key) - uri = _generate_cp_or_mv_opstr('move', source_bucket, source_key, target_bucket, target_key) - Auth.request Config.settings[:rs_host] + uri - end - - def delete(bucket, key) - Auth.request Config.settings[:rs_host] + '/delete/' + encode_entry_uri(bucket, key) - end - - def publish(domain, bucket) - encoded_domain = Utils.urlsafe_base64_encode(domain) - Auth.request Config.settings[:rs_host] + "/publish/#{encoded_domain}/from/#{bucket}" - end - - def unpublish(domain) - encoded_domain = Utils.urlsafe_base64_encode(domain) - Auth.request Config.settings[:rs_host] + "/unpublish/#{encoded_domain}" - end - - def drop(bucket) - Auth.request Config.settings[:rs_host] + "/drop/#{bucket}" - end - - def batch(command, bucket, keys) - execs = [] - keys.each do |key| - encoded_uri = encode_entry_uri(bucket, key) - execs << "op=/#{command}/#{encoded_uri}" - end - Auth.request Config.settings[:rs_host] + "/batch?" + execs.join("&") - end - - def batch_get(bucket, keys) - batch("get", bucket, keys) - end - - def batch_stat(bucket, keys) - batch("stat", bucket, keys) - end - - def batch_copy(*args) - _batch_cp_or_mv('copy', args) - end - - def batch_move(*args) - _batch_cp_or_mv('move', args) - end - - def batch_delete(bucket, keys) - batch("delete", bucket, keys) - end - - def save_as(bucket, key, source_url, op_params_string) - encoded_uri = encode_entry_uri(bucket, key) - save_as_string = '/save-as/' + encoded_uri - new_url = source_url + '?' + op_params_string + save_as_string - Auth.request new_url - end - - def image_mogrify_save_as(bucket, key, source_image_url, options) - mogrify_params_string = Image.generate_mogrify_params_string(options) - save_as(bucket, key, source_image_url, mogrify_params_string) - end - - private - - def _generate_cp_or_mv_opstr(command, source_bucket, source_key, target_bucket, target_key) - source_encoded_entry_uri = encode_entry_uri(source_bucket, source_key) - target_encoded_entry_uri = encode_entry_uri(target_bucket, target_key) - %Q(/#{command}/#{source_encoded_entry_uri}/#{target_encoded_entry_uri}) - end - -=begin - 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("&") - end -=end - - end - end - end -end diff --git a/lib/qiniu/tokens/access_token.rb b/lib/qiniu/tokens/access_token.rb index 78aa99a..43534e0 100755 --- a/lib/qiniu/tokens/access_token.rb +++ b/lib/qiniu/tokens/access_token.rb @@ -1,11 +1,10 @@ # -*- encoding: utf-8 -*- require 'hmac-sha1' -require 'qiniu/rs/config' -require 'qiniu/rs/utils' +require 'qiniu/config' +require 'qiniu/utils' module Qiniu - module RS class AccessToken include Utils @@ -18,6 +17,5 @@ module Qiniu urlsafe_base64_encode(hmac.digest) end - end - end -end + end # AccessToken +end # module Qiniu diff --git a/lib/qiniu/tokens/download_token.rb b/lib/qiniu/tokens/download_token.rb index f681520..ccf75a5 100755 --- a/lib/qiniu/tokens/download_token.rb +++ b/lib/qiniu/tokens/download_token.rb @@ -2,10 +2,9 @@ require 'json' require 'qiniu/tokens/access_token' -require 'qiniu/rs/utils' +require 'qiniu/utils' module Qiniu - module RS class DownloadToken < AccessToken include Utils @@ -28,6 +27,5 @@ module Qiniu %Q(#{@access_key}:#{encoded_digest}:#{signature}) end - end - end -end + end # moidule DownloadToken +end # module Qiniu diff --git a/lib/qiniu/tokens/qbox_token.rb b/lib/qiniu/tokens/qbox_token.rb index 8ccdb0c..bca67c2 100755 --- a/lib/qiniu/tokens/qbox_token.rb +++ b/lib/qiniu/tokens/qbox_token.rb @@ -5,7 +5,6 @@ require 'json' require 'qiniu/tokens/access_token' module Qiniu - module RS class QboxToken < AccessToken include Utils @@ -35,6 +34,5 @@ module Qiniu %Q(#{@access_key}:#{encoded_digest}) end - end - end -end + end # module QboxToken +end # module Qiniu diff --git a/lib/qiniu/tokens/upload_token.rb b/lib/qiniu/tokens/upload_token.rb index d2fae00..c5202f8 100755 --- a/lib/qiniu/tokens/upload_token.rb +++ b/lib/qiniu/tokens/upload_token.rb @@ -2,10 +2,9 @@ require 'json' require 'qiniu/tokens/access_token' -require 'qiniu/rs/utils' +require 'qiniu/utils' module Qiniu - module RS class UploadToken < AccessToken include Utils @@ -44,6 +43,5 @@ module Qiniu %Q(#{@access_key}:#{encoded_digest}:#{signature}) end - end - end -end + end # module UploadToken +end # module Qiniu diff --git a/lib/qiniu/rs/up.rb b/lib/qiniu/up.rb similarity index 98% rename from lib/qiniu/rs/up.rb rename to lib/qiniu/up.rb index 55a603a..0ed2fe9 100755 --- a/lib/qiniu/rs/up.rb +++ b/lib/qiniu/up.rb @@ -6,23 +6,22 @@ require 'tmpdir' require 'fileutils' require 'mime/types' require 'digest/sha1' -require 'qiniu/rs/abstract' -require 'qiniu/rs/exceptions' -require 'qiniu/rs/io' +require 'qiniu/abstract' +require 'qiniu/exceptions' +require 'qiniu/io' module Qiniu - module RS module UP module AbstractClass class ChunkProgressNotifier - include Qiniu::RS::Abstract + include Qiniu::Abstract abstract_methods :notify # def notify(block_index, block_put_progress); end end class BlockProgressNotifier - include Qiniu::RS::Abstract + include Qiniu::Abstract abstract_methods :notify # def notify(block_index, checksum); end end @@ -276,6 +275,5 @@ module Qiniu end - end - end -end + end # module UP +end # module Qiniu diff --git a/lib/qiniu/rs/utils.rb b/lib/qiniu/utils.rb similarity index 98% rename from lib/qiniu/rs/utils.rb rename to lib/qiniu/utils.rb index 3c0d169..290d3db 100755 --- a/lib/qiniu/rs/utils.rb +++ b/lib/qiniu/utils.rb @@ -7,10 +7,9 @@ require 'zlib' require 'base64' require 'rest_client' require 'hmac-sha1' -require 'qiniu/rs/exceptions' +require 'qiniu/exceptions' module Qiniu - module RS module Utils extend self def urlsafe_base64_encode content @@ -145,6 +144,5 @@ module Qiniu %Q(#{access_key}:#{encoded_digest}) end - end - end -end + end # module Utils +end # module Qiniu diff --git a/lib/qiniu/rs/version.rb b/lib/qiniu/version.rb similarity index 90% rename from lib/qiniu/rs/version.rb rename to lib/qiniu/version.rb index c74a1b0..c6e5ea3 100755 --- a/lib/qiniu/rs/version.rb +++ b/lib/qiniu/version.rb @@ -1,7 +1,6 @@ # -*- encoding: utf-8 -*- module Qiniu - module RS module Version MAJOR = 3 MINOR = 4 @@ -14,6 +13,5 @@ module Qiniu def self.to_s [MAJOR, MINOR, PATCH].join('.') end - end - end -end + end # Version +end # module Qiniu diff --git a/spec/qiniu/rs/abstract_spec.rb b/spec/qiniu/abstract_spec.rb similarity index 82% rename from spec/qiniu/rs/abstract_spec.rb rename to spec/qiniu/abstract_spec.rb index e69af62..372cf33 100755 --- a/spec/qiniu/rs/abstract_spec.rb +++ b/spec/qiniu/abstract_spec.rb @@ -1,12 +1,12 @@ # -*- encoding: utf-8 -*- require 'spec_helper' -require 'qiniu/rs/abstract' +require 'qiniu/abstract' -describe Qiniu::RS::Abstract do +describe Qiniu::Abstract do before(:each) do @klass = Class.new do - include Qiniu::RS::Abstract + include Qiniu::Abstract abstract_methods :foo, :bar end diff --git a/spec/qiniu/rs/image_logo_for_test.png b/spec/qiniu/image_logo_for_test.png similarity index 100% rename from spec/qiniu/rs/image_logo_for_test.png rename to spec/qiniu/image_logo_for_test.png diff --git a/spec/qiniu/rs/image_spec.rb b/spec/qiniu/image_spec.rb similarity index 74% rename from spec/qiniu/rs/image_spec.rb rename to spec/qiniu/image_spec.rb index 5cfee7e..d21d347 100755 --- a/spec/qiniu/rs/image_spec.rb +++ b/spec/qiniu/image_spec.rb @@ -1,12 +1,12 @@ # -*- encoding: utf-8 -*- require 'spec_helper' -require 'qiniu/rs/auth' +require 'qiniu/auth' require 'qiniu/rs' -require 'qiniu/rs/image' +require 'qiniu/image' module Qiniu - module RS + module Image describe Image do before :all do @@ -14,7 +14,7 @@ module Qiniu @bucket = 'RubySdkTest' + (Time.now.to_i+rand(1000)).to_s @key = "image_logo_for_test.png" - result = Qiniu::RS.mkbucket(@bucket) + result = Qiniu.mkbucket(@bucket) puts result.inspect result.should be_true @@ -27,8 +27,8 @@ module Qiniu :async_options => "imageView/1/w/120/h/120", :return_body => '{"size":$(fsize), "hash":$(etag), "width":$(imageInfo.width), "height":$(imageInfo.height)}' } - uptoken = Qiniu::RS.generate_upload_token(upopts) - data = Qiniu::RS.upload_file :uptoken => uptoken, :file => local_file, :bucket => @bucket, :key => @key + uptoken = Qiniu.generate_upload_token(upopts) + data = Qiniu.upload_file :uptoken => uptoken, :file => local_file, :bucket => @bucket, :key => @key puts data.inspect data["size"].should_not be_zero @@ -36,7 +36,7 @@ module Qiniu data["width"].should_not be_zero data["height"].should_not be_zero - result = Qiniu::RS.get(@bucket, @key) + result = Qiniu.get(@bucket, @key) result["url"].should_not be_empty puts result.inspect @source_image_url = result["url"] @@ -53,14 +53,14 @@ module Qiniu end after :all do - result = Qiniu::RS.drop(@bucket) + result = Qiniu.drop(@bucket) puts result.inspect result.should_not be_false end context ".info" do it "should works" do - code, data = Qiniu::RS::Image.info(@source_image_url) + code, data = Qiniu::Image.info(@source_image_url) code.should == 200 puts data.inspect end @@ -69,7 +69,7 @@ module Qiniu =begin context ".exif" do it "should works" do - code, data = Qiniu::RS::Image.exif(@source_image_url) + code, data = Qiniu::Image.exif(@source_image_url) puts data.inspect end end @@ -77,11 +77,11 @@ module Qiniu context ".mogrify_preview_url" do it "should works" do - mogrify_preview_url = Qiniu::RS::Image.mogrify_preview_url(@source_image_url, @mogrify_options) + mogrify_preview_url = Qiniu::Image.mogrify_preview_url(@source_image_url, @mogrify_options) puts mogrify_preview_url.inspect end end end - end -end + end # module Image +end # module Qiniu diff --git a/spec/qiniu/rs/io_spec.rb b/spec/qiniu/io_spec.rb similarity index 65% rename from spec/qiniu/rs/io_spec.rb rename to spec/qiniu/io_spec.rb index 1db2555..9b596fb 100755 --- a/spec/qiniu/rs/io_spec.rb +++ b/spec/qiniu/io_spec.rb @@ -1,32 +1,32 @@ # -*- encoding: utf-8 -*- require 'spec_helper' -require 'qiniu/rs/auth' -require 'qiniu/rs/io' +require 'qiniu/auth' +require 'qiniu/io' require 'digest/sha1' module Qiniu - module RS + module IO describe IO do before :all do @bucket = 'RubySdkTest' + (Time.now.to_i+rand(1000)).to_s @key = Digest::SHA1.hexdigest((Time.now.to_i+rand(100)).to_s) - result = Qiniu::RS.mkbucket(@bucket) + result = Qiniu.mkbucket(@bucket) puts result.inspect result.should be_true end after :all do - result = Qiniu::RS.drop(@bucket) + result = Qiniu.drop(@bucket) puts result.inspect result.should_not be_false end context ".put_file" do it "should works" do - code, data = Qiniu::RS::IO.put_file(__FILE__, @bucket, @key, 'application/x-ruby', 'customMeta', true) + code, data = Qiniu::IO.put_file(__FILE__, @bucket, @key, 'application/x-ruby', 'customMeta', true) code.should == 200 puts data.inspect end @@ -35,8 +35,8 @@ module Qiniu context ".upload_with_token" do it "should works" do upopts = {:scope => @bucket, :expires_in => 3600, :customer => "why404@gmail.com"} - uptoken = Qiniu::RS.generate_upload_token(upopts) - code, data = Qiniu::RS::IO.upload_with_token(uptoken, __FILE__, @bucket, @key, nil, nil, nil, true) + uptoken = Qiniu.generate_upload_token(upopts) + code, data = Qiniu::IO.upload_with_token(uptoken, __FILE__, @bucket, @key, nil, nil, nil, true) code.should == 200 puts data.inspect end @@ -45,9 +45,9 @@ module Qiniu 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) + uptoken = Qiniu.generate_upload_token(upopts) - code, data = Qiniu::RS::IO.upload_with_token_2(uptoken, __FILE__, @key) + code, data = Qiniu::IO.upload_with_token_2(uptoken, __FILE__, @key) code.should == 200 puts data.inspect @@ -55,5 +55,5 @@ module Qiniu end # .upload_with_token_2 end - end -end + end # module IO +end # module Qiniu diff --git a/spec/qiniu/rs/pub_spec.rb b/spec/qiniu/pub_spec.rb similarity index 63% rename from spec/qiniu/rs/pub_spec.rb rename to spec/qiniu/pub_spec.rb index 1addab7..03c2d60 100755 --- a/spec/qiniu/rs/pub_spec.rb +++ b/spec/qiniu/pub_spec.rb @@ -1,30 +1,30 @@ # -*- encoding: utf-8 -*- require 'spec_helper' -require 'qiniu/rs/auth' -require 'qiniu/rs/rs' -require 'qiniu/rs/pub' +require 'qiniu/auth' +require 'qiniu' +require 'qiniu/pub' module Qiniu - module RS + module Pub describe Pub do before :all do @bucket = 'RubySdkTest' + (Time.now.to_i+rand(1000)).to_s - result = Qiniu::RS.mkbucket(@bucket) + result = Qiniu.mkbucket(@bucket) puts result.inspect result.should_not be_false end after :all do - result = Qiniu::RS.drop(@bucket) + result = Qiniu.drop(@bucket) puts result.inspect result.should_not be_false end context ".set_protected" do it "should works" do - code, data = Qiniu::RS::Pub.set_protected(@bucket, 1) + code, data = Qiniu::Pub.set_protected(@bucket, 1) code.should == 200 puts data.inspect end @@ -32,7 +32,7 @@ module Qiniu context ".set_separator" do it "should works" do - code, data = Qiniu::RS::Pub.set_separator(@bucket, "-") + code, data = Qiniu::Pub.set_separator(@bucket, "-") code.should == 200 puts data.inspect end @@ -40,7 +40,7 @@ module Qiniu context ".set_style" do it "should works" do - code, data = Qiniu::RS::Pub.set_style(@bucket, "small.jpg", "imageMogr/auto-orient/thumbnail/!120x120r/gravity/center/crop/!120x120/quality/80") + code, data = Qiniu::Pub.set_style(@bucket, "small.jpg", "imageMogr/auto-orient/thumbnail/!120x120r/gravity/center/crop/!120x120/quality/80") code.should == 200 puts data.inspect end @@ -48,12 +48,12 @@ module Qiniu context ".unset_style" do it "should works" do - code, data = Qiniu::RS::Pub.unset_style(@bucket, "small.jpg") + code, data = Qiniu::Pub.unset_style(@bucket, "small.jpg") code.should == 200 puts data.inspect end end end - end -end + end # module Pub +end # module Qiniu diff --git a/spec/qiniu/qiniu_spec.rb b/spec/qiniu/qiniu_spec.rb new file mode 100755 index 0000000..5f39dfb --- /dev/null +++ b/spec/qiniu/qiniu_spec.rb @@ -0,0 +1,385 @@ +# -*- encoding: utf-8 -*- + +require 'digest/sha1' +require 'spec_helper' +require 'qiniu/rs' +require 'qiniu/exceptions' + +module Qiniu + describe RS do + + before :all do + @bucket = 'RubySdkTest' + (Time.now.to_i+rand(1000)).to_s + @key = Digest::SHA1.hexdigest Time.now.to_s + @key2 = @key + rand(100).to_s + #@domain = @bucket + '.dn.qbox.me' + + result = Qiniu.mkbucket(@bucket) + result.should_not be_false + + @test_image_bucket = 'RubySdkTest' + (Time.now.to_i+rand(1000)).to_s + result2 = Qiniu.mkbucket(@test_image_bucket) + puts result2.inspect + result2.should be_true + + @test_image_key = 'image_logo_for_test.png' + local_file = File.expand_path('./' + @test_image_key, File.dirname(__FILE__)) + puts local_file.inspect + upopts = {:scope => @test_image_bucket, :expires_in => 3600, :customer => "why404@gmail.com"} + uptoken = Qiniu.generate_upload_token(upopts) + data = Qiniu.upload_file :uptoken => uptoken, :file => local_file, :bucket => @test_image_bucket, :key => @test_image_key + puts data.inspect + end + + after :all do + #result = Qiniu.unpublish(@domain) + #result.should_not be_false + + result1 = Qiniu.drop(@bucket) + puts result1.inspect + result1.should_not be_false + + result2 = Qiniu.drop(@test_image_bucket) + puts result2.inspect + result2.should_not be_false + end + + context ".put_file" do + it "should works" do + result = Qiniu.put_file :file => __FILE__, + :bucket => @bucket, + :key => @key, + :mime_type => 'application/x-ruby', + :enable_crc32_check => true + result.should be_true + end + end + + context ".buckets" do + it "should works" do + result = Qiniu.buckets + result.should_not be_false + puts result.inspect + end + end + + context ".set_protected" do + it "should works" do + result = Qiniu.set_protected(@bucket, 1) + result.should_not be_false + puts result.inspect + end + end + + context ".set_separator" do + it "should works" do + result = Qiniu.set_separator(@bucket, "-") + result.should_not be_false + puts result.inspect + end + end + + context ".set_style" do + it "should works" do + result = Qiniu.set_style(@bucket, "small.jpg", "imageMogr/auto-orient/thumbnail/!120x120r/gravity/center/crop/!120x120/quality/80") + result.should_not be_false + puts result.inspect + end + end + + context ".unset_style" do + it "should works" do + result = Qiniu.unset_style(@bucket, "small.jpg") + result.should_not be_false + puts result.inspect + end + end + + context ".upload_file" do + it "should works" do + uptoken_opts = {:scope => @bucket, :escape => 0} + upload_opts = { + :uptoken => Qiniu.generate_upload_token(uptoken_opts), + :file => __FILE__, + :bucket => @bucket, + :key => @key, + :enable_crc32_check => true + } + result = Qiniu.upload_file(upload_opts) + result.should_not be_false + puts result.inspect + end + + it "should raise MissingArgsError" do + uptoken_opts = {:scope => @bucket, :escape => 0} + upload_opts = { + :uptoken => Qiniu.generate_upload_token(uptoken_opts), + :file => __FILE__, + :key => @key, + :enable_crc32_check => true + } + lambda { Qiniu.upload_file(upload_opts) }.should raise_error(MissingArgsError) + end + + it "should raise NoSuchFileError" do + uptoken_opts = {:scope => @bucket, :escape => 0} + upload_opts = { + :uptoken => Qiniu.generate_upload_token(uptoken_opts), + :file => 'no_this_file', + :bucket => @bucket, + :key => @key, + :enable_crc32_check => true + } + lambda { Qiniu.upload_file(upload_opts) }.should raise_error(NoSuchFileError) + end + end + + context ".resumable_upload_file" do + it "should works" do + # generate bigfile for testing + localfile = "test_bigfile" + 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 => "why404@gmail.com", :escape => 0} + uptoken = Qiniu.generate_upload_token(uptoken_opts) + # uploading + upload_opts = { + :uptoken => uptoken, + :file => localfile, + :bucket => @bucket, + :key => key + } + #uploading + result1 = Qiniu.upload_file(upload_opts) + #drop the bigfile + File.unlink(localfile) if File.exists?(localfile) + #expect + puts result1.inspect + result1.should_not be_false + result1.should_not be_empty + #stat + result2 = Qiniu.stat(@bucket, key) + puts result2.inspect + result2.should_not be_false + #delete + result3 = Qiniu.delete(@bucket, key) + puts result3.inspect + result3.should_not be_false + end + end + + context ".stat" do + it "should works" do + result = Qiniu.stat(@bucket, @key) + result.should_not be_false + result.should_not be_empty + puts result.inspect + end + end + + context ".get" do + it "should works" do + result = Qiniu.get(@bucket, @key, "rs_spec.rb", 10) + result.should_not be_false + result.should_not be_empty + puts result.inspect + end + end + + context ".download" do + it "should works" do + result = Qiniu.download(@bucket, @key, "rs_spec.rb", 10) + result.should_not be_false + result.should_not be_empty + puts result.inspect + end + end + + context ".batch" do + it "should works" do + result = Qiniu.batch("stat", @bucket, [@key]) + result.should_not be_false + result.should_not be_empty + puts result.inspect + end + end + + context ".batch_stat" do + it "should works" do + result = Qiniu.batch_stat(@bucket, [@key]) + result.should_not be_false + result.should_not be_empty + puts result.inspect + end + end + + context ".batch_get" do + it "should works" do + result = Qiniu.batch_get(@bucket, [@key]) + result.should_not be_false + result.should_not be_empty + puts result.inspect + end + end + + context ".batch_download" do + it "should works" do + result = Qiniu.batch_download(@bucket, [@key]) + result.should_not be_false + result.should_not be_empty + puts result.inspect + end + end + +=begin + context ".publish" do + it "should works" do + result = Qiniu.publish(@domain, @bucket) + result.should_not be_false + end + end +=end + +=begin + context ".unpublish" do + it "should works" do + result = Qiniu.unpublish(@domain) + result.should_not be_false + end + end +=end + +=begin + context ".batch_copy" do + it "should works" do + result = Qiniu.batch_copy [@bucket, @key, @bucket, @key2] + result.should_not be_false + + #result2 = Qiniu.stat(@bucket, @key2) + #result2.should_not be_false + end + end + + context ".batch_move" do + it "should works" do + result = Qiniu.batch_move [@bucket, @key, @bucket, @key2] + result.should_not be_false + + #result2 = Qiniu.stat(@bucket, @key2) + #result2.should_not be_false + + result3 = Qiniu.batch_move [@bucket, @key2, @bucket, @key] + result3.should_not be_false + end + end +=end + + context ".move" do + it "should works" do + result = Qiniu.move(@bucket, @key, @bucket, @key2) + result.should_not be_false + + result2 = Qiniu.stat(@bucket, @key2) + result2.should_not be_false + + result3 = Qiniu.move(@bucket, @key2, @bucket, @key) + result3.should_not be_false + end + end + + context ".copy" do + it "should works" do + result = Qiniu.copy(@bucket, @key, @bucket, @key2) + result.should_not be_false + + #result2 = Qiniu.stat(@bucket, @key2) + #result2.should_not be_false + end + end + + context ".delete" do + it "should works" do + result = Qiniu.delete(@bucket, @key) + result.should_not be_false + end + end + + context ".drop" do + it "should works" do + result = Qiniu.drop(@bucket) + result.should_not be_false + end + end + + context ".image_info" do + it "should works" do + data = Qiniu.get(@test_image_bucket, @test_image_key) + data.should_not be_false + data.should_not be_empty + puts data.inspect + result = Qiniu.image_info(data["url"]) + result.should_not be_false + result.should_not be_empty + puts result.inspect + end + end + +=begin + context ".image_exif" do + it "should works" do + data = Qiniu.get(@test_image_bucket, @test_image_key) + data.should_not be_false + data.should_not be_empty + puts data.inspect + result = Qiniu.image_exif(data["url"]) + puts result.inspect + end + end +=end + + context ".image_mogrify_save_as" do + it "should works" do + data = Qiniu.get(@test_image_bucket, @test_image_key) + data.should_not be_false + data.should_not be_empty + puts data.inspect + + dest_key = "cropped-" + @test_image_key + src_img_url = data["url"] + mogrify_options = { + :thumbnail => "!120x120>", + :gravity => "center", + :crop => "!120x120a0a0", + :quality => 85, + :rotate => 45, + :format => "jpg", + :auto_orient => true + } + result2 = Qiniu.image_mogrify_save_as(@test_image_bucket, dest_key, src_img_url, mogrify_options) + result2.should_not be_false + result2.should_not be_empty + puts result2.inspect + end + end + + context ".generate_upload_token" do + it "should works" do + data = Qiniu.generate_upload_token({:scope => @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.generate_download_token({:expires_in => 1, :pattern => 'http://*.dn.qbox.me/*'}) + data.should_not be_empty + puts data.inspect + data.split(":").length.should == 3 + end + end + + end +end # module Qiniu diff --git a/spec/qiniu/rs/rs_spec.rb b/spec/qiniu/rs/rs_spec.rb deleted file mode 100755 index 3954c85..0000000 --- a/spec/qiniu/rs/rs_spec.rb +++ /dev/null @@ -1,180 +0,0 @@ -# -*- encoding: utf-8 -*- - -require 'digest/sha1' -require 'spec_helper' -require 'qiniu/rs/auth' -require 'qiniu/rs/io' -require 'qiniu/rs/rs' - -module Qiniu - module RS - describe RS do - - before :all do - @bucket = 'RubySdkTest' + (Time.now.to_i+rand(1000)).to_s - @key = Digest::SHA1.hexdigest((Time.now.to_i+rand(100)).to_s) - @key2 = @key + rand(100).to_s - #@domain = @bucket + '.dn.qbox.me' - - code, data = Qiniu::RS::RS.mkbucket(@bucket) - puts [code, data].inspect - code.should == 200 - end - - after :all do - code, data = Qiniu::RS::RS.drop(@bucket) - puts [code, data].inspect - code.should == 200 - end - - context ".put_file" do - it "should works" do - code, data = Qiniu::RS::IO.put_file(__FILE__, @bucket, @key, 'application/x-ruby', 'customMeta', true) - code.should == 200 - puts data.inspect - end - end - - context ".buckets" do - it "should works" do - code, data = Qiniu::RS::RS.buckets - code.should == 200 - puts data.inspect - end - end - - context ".stat" do - it "should works" do - code, data = Qiniu::RS::RS.stat(@bucket, @key) - code.should == 200 - puts data.inspect - end - end - - context ".get" do - it "should works" do - code, data = Qiniu::RS::RS.get(@bucket, @key, "rs_spec.rb", 1) - code.should == 200 - puts data.inspect - end - end - - context ".batch" do - it "should works" do - code, data = Qiniu::RS::RS.batch("stat", @bucket, [@key]) - code.should == 200 - puts data.inspect - end - end - - context ".batch_stat" do - it "should works" do - code, data = Qiniu::RS::RS.batch_stat(@bucket, [@key]) - code.should == 200 - puts data.inspect - end - end - - context ".batch_get" do - it "should works" do - code, data = Qiniu::RS::RS.batch_get(@bucket, [@key]) - code.should == 200 - puts data.inspect - end - end - -=begin - context ".batch_copy" do - it "should works" do - code, data = Qiniu::RS::RS.batch_copy [@bucket, @key, @bucket, @key2] - code.should == 200 - puts data.inspect - - #code2, data2 = Qiniu::RS::RS.stat(@bucket, @key2) - #code2.should == 200 - #puts data2.inspect - end - end - - context ".batch_move" do - it "should works" do - code, data = Qiniu::RS::RS.batch_move [@bucket, @key, @bucket, @key2] - code.should == 200 - puts data.inspect - - #code2, data2 = Qiniu::RS::RS.stat(@bucket, @key2) - #code2.should == 200 - #puts data2.inspect - - code3, data3 = Qiniu::RS::RS.batch_move [@bucket, @key2, @bucket, @key] - code3.should == 200 - puts data3.inspect - end - end -=end - -=begin - context ".publish" do - it "should works" do - code, data = Qiniu::RS::RS.publish(@domain, @bucket) - code.should == 200 - puts data.inspect - end - end - - context ".unpublish" do - it "should works" do - code, data = Qiniu::RS::RS.unpublish(@domain) - code.should == 200 - puts data.inspect - end - end -=end - - context ".move" do - it "should works" do - code, data = Qiniu::RS::RS.move(@bucket, @key, @bucket, @key2) - code.should == 200 - puts data.inspect - - code2, data2 = Qiniu::RS::RS.stat(@bucket, @key2) - code2.should == 200 - puts data2.inspect - - code3, data3 = Qiniu::RS::RS.move(@bucket, @key2, @bucket, @key) - code3.should == 200 - puts data3.inspect - end - end - - context ".copy" do - it "should works" do - code, data = Qiniu::RS::RS.copy(@bucket, @key, @bucket, @key2) - code.should == 200 - puts data.inspect - - #code2, data2 = Qiniu::RS::RS.stat(@bucket, @key2) - #code2.should == 200 - #puts data2.inspect - end - end - - context ".delete" do - it "should works" do - code, data = Qiniu::RS::RS.delete(@bucket, @key) - code.should == 200 - puts data.inspect - end - end - - context ".drop" do - it "should works" do - code, data = Qiniu::RS::RS.drop(@bucket) - code.should == 200 - puts data.inspect - end - end - - end - end -end diff --git a/spec/qiniu/rs/version_spec.rb b/spec/qiniu/rs/version_spec.rb deleted file mode 100755 index 4df286b..0000000 --- a/spec/qiniu/rs/version_spec.rb +++ /dev/null @@ -1,10 +0,0 @@ -# -*- encoding: utf-8 -*- - -require 'spec_helper' -require 'qiniu/rs/version' - -describe Qiniu::RS::Version do - it "should has a VERSION" do - Qiniu::RS::Version.to_s.should =~ /^\d+\.\d+\.\d+?$/ - end -end diff --git a/spec/qiniu/rs_spec.rb b/spec/qiniu/rs_spec.rb index 4665b12..cc6bc31 100755 --- a/spec/qiniu/rs_spec.rb +++ b/spec/qiniu/rs_spec.rb @@ -2,384 +2,179 @@ require 'digest/sha1' require 'spec_helper' -require 'qiniu/rs' -require 'qiniu/rs/exceptions' +require 'qiniu/auth' +require 'qiniu/io' +require 'qiniu' module Qiniu - describe RS do + module RS + describe RS do - before :all do - @bucket = 'RubySdkTest' + (Time.now.to_i+rand(1000)).to_s - @key = Digest::SHA1.hexdigest Time.now.to_s - @key2 = @key + rand(100).to_s - #@domain = @bucket + '.dn.qbox.me' + before :all do + @bucket = 'RubySdkTest' + (Time.now.to_i+rand(1000)).to_s + @key = Digest::SHA1.hexdigest((Time.now.to_i+rand(100)).to_s) + @key2 = @key + rand(100).to_s + #@domain = @bucket + '.dn.qbox.me' - result = Qiniu::RS.mkbucket(@bucket) - result.should_not be_false - - @test_image_bucket = 'RubySdkTest' + (Time.now.to_i+rand(1000)).to_s - result2 = Qiniu::RS.mkbucket(@test_image_bucket) - puts result2.inspect - result2.should be_true - - @test_image_key = 'image_logo_for_test.png' - local_file = File.expand_path('./rs/' + @test_image_key, File.dirname(__FILE__)) - puts local_file.inspect - upopts = {:scope => @test_image_bucket, :expires_in => 3600, :customer => "why404@gmail.com"} - uptoken = Qiniu::RS.generate_upload_token(upopts) - data = Qiniu::RS.upload_file :uptoken => uptoken, :file => local_file, :bucket => @test_image_bucket, :key => @test_image_key - puts data.inspect - end - - after :all do - #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 - end - - context ".put_file" do - it "should works" do - result = Qiniu::RS.put_file :file => __FILE__, - :bucket => @bucket, - :key => @key, - :mime_type => 'application/x-ruby', - :enable_crc32_check => true - result.should be_true - end - end - - context ".buckets" do - it "should works" do - result = Qiniu::RS.buckets - result.should_not be_false - puts result.inspect - end - end - - context ".set_protected" do - it "should works" do - result = Qiniu::RS.set_protected(@bucket, 1) - result.should_not be_false - puts result.inspect - end - end - - context ".set_separator" do - it "should works" do - result = Qiniu::RS.set_separator(@bucket, "-") - result.should_not be_false - puts result.inspect - end - end - - context ".set_style" do - it "should works" do - result = Qiniu::RS.set_style(@bucket, "small.jpg", "imageMogr/auto-orient/thumbnail/!120x120r/gravity/center/crop/!120x120/quality/80") - result.should_not be_false - puts result.inspect - end - end - - context ".unset_style" do - it "should works" do - result = Qiniu::RS.unset_style(@bucket, "small.jpg") - result.should_not be_false - puts result.inspect - end - end - - context ".upload_file" do - it "should works" do - uptoken_opts = {:scope => @bucket, :escape => 0} - upload_opts = { - :uptoken => Qiniu::RS.generate_upload_token(uptoken_opts), - :file => __FILE__, - :bucket => @bucket, - :key => @key, - :enable_crc32_check => true - } - result = Qiniu::RS.upload_file(upload_opts) - result.should_not be_false - puts result.inspect + code, data = Qiniu::RS.mkbucket(@bucket) + puts [code, data].inspect + code.should == 200 end - it "should raise MissingArgsError" do - uptoken_opts = {:scope => @bucket, :escape => 0} - upload_opts = { - :uptoken => Qiniu::RS.generate_upload_token(uptoken_opts), - :file => __FILE__, - :key => @key, - :enable_crc32_check => true - } - lambda { Qiniu::RS.upload_file(upload_opts) }.should raise_error(RS::MissingArgsError) + after :all do + code, data = Qiniu::RS.drop(@bucket) + puts [code, data].inspect + code.should == 200 end - it "should raise NoSuchFileError" do - uptoken_opts = {:scope => @bucket, :escape => 0} - upload_opts = { - :uptoken => Qiniu::RS.generate_upload_token(uptoken_opts), - :file => 'no_this_file', - :bucket => @bucket, - :key => @key, - :enable_crc32_check => true - } - lambda { Qiniu::RS.upload_file(upload_opts) }.should raise_error(RS::NoSuchFileError) + context ".put_file" do + it "should works" do + code, data = Qiniu::IO.put_file(__FILE__, @bucket, @key, 'application/x-ruby', 'customMeta', true) + code.should == 200 + puts data.inspect + end end - end - context ".resumable_upload_file" do - it "should works" do - # generate bigfile for testing - localfile = "test_bigfile" - 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 => "why404@gmail.com", :escape => 0} - uptoken = Qiniu::RS.generate_upload_token(uptoken_opts) - # uploading - upload_opts = { - :uptoken => uptoken, - :file => localfile, - :bucket => @bucket, - :key => key - } - #uploading - result1 = Qiniu::RS.upload_file(upload_opts) - #drop the bigfile - File.unlink(localfile) if File.exists?(localfile) - #expect - puts result1.inspect - result1.should_not be_false - result1.should_not be_empty - #stat - result2 = Qiniu::RS.stat(@bucket, key) - puts result2.inspect - result2.should_not be_false - #delete - result3 = Qiniu::RS.delete(@bucket, key) - puts result3.inspect - result3.should_not be_false + context ".buckets" do + it "should works" do + code, data = Qiniu::RS.buckets + code.should == 200 + puts data.inspect + end end - end - context ".stat" do - it "should works" do - result = Qiniu::RS.stat(@bucket, @key) - result.should_not be_false - result.should_not be_empty - puts result.inspect + context ".stat" do + it "should works" do + code, data = Qiniu::RS.stat(@bucket, @key) + code.should == 200 + puts data.inspect + end end - end - context ".get" do - it "should works" do - result = Qiniu::RS.get(@bucket, @key, "rs_spec.rb", 10) - result.should_not be_false - result.should_not be_empty - puts result.inspect + context ".get" do + it "should works" do + code, data = Qiniu::RS.get(@bucket, @key, "rs_spec.rb", 1) + code.should == 200 + puts data.inspect + end end - end - context ".download" do - it "should works" do - result = Qiniu::RS.download(@bucket, @key, "rs_spec.rb", 10) - result.should_not be_false - result.should_not be_empty - puts result.inspect + context ".batch" do + it "should works" do + code, data = Qiniu::RS.batch("stat", @bucket, [@key]) + code.should == 200 + puts data.inspect + end end - end - context ".batch" do - it "should works" do - result = Qiniu::RS.batch("stat", @bucket, [@key]) - result.should_not be_false - result.should_not be_empty - puts result.inspect + context ".batch_stat" do + it "should works" do + code, data = Qiniu::RS.batch_stat(@bucket, [@key]) + code.should == 200 + puts data.inspect + end end - end - context ".batch_stat" do - it "should works" do - result = Qiniu::RS.batch_stat(@bucket, [@key]) - result.should_not be_false - result.should_not be_empty - puts result.inspect + context ".batch_get" do + it "should works" do + code, data = Qiniu::RS.batch_get(@bucket, [@key]) + code.should == 200 + puts data.inspect + end end - end - - context ".batch_get" do - it "should works" do - result = Qiniu::RS.batch_get(@bucket, [@key]) - result.should_not be_false - result.should_not be_empty - puts result.inspect - end - end - - context ".batch_download" do - it "should works" do - result = Qiniu::RS.batch_download(@bucket, [@key]) - result.should_not be_false - result.should_not be_empty - puts result.inspect - end - end =begin - context ".publish" do - it "should works" do - result = Qiniu::RS.publish(@domain, @bucket) - result.should_not be_false + context ".batch_copy" do + it "should works" do + code, data = Qiniu.batch_copy [@bucket, @key, @bucket, @key2] + code.should == 200 + puts data.inspect + + #code2, data2 = Qiniu.stat(@bucket, @key2) + #code2.should == 200 + #puts data2.inspect + end + end + + context ".batch_move" do + it "should works" do + code, data = Qiniu.batch_move [@bucket, @key, @bucket, @key2] + code.should == 200 + puts data.inspect + + #code2, data2 = Qiniu.stat(@bucket, @key2) + #code2.should == 200 + #puts data2.inspect + + code3, data3 = Qiniu.batch_move [@bucket, @key2, @bucket, @key] + code3.should == 200 + puts data3.inspect + end end - end =end =begin - context ".unpublish" do - it "should works" do - result = Qiniu::RS.unpublish(@domain) - result.should_not be_false + context ".publish" do + it "should works" do + code, data = Qiniu.publish(@domain, @bucket) + code.should == 200 + puts data.inspect + end + end + + context ".unpublish" do + it "should works" do + code, data = Qiniu.unpublish(@domain) + code.should == 200 + puts data.inspect + end end - end =end -=begin - context ".batch_copy" do - it "should works" do - result = Qiniu::RS.batch_copy [@bucket, @key, @bucket, @key2] - result.should_not be_false + context ".move" do + it "should works" do + code, data = Qiniu::RS.move(@bucket, @key, @bucket, @key2) + code.should == 200 + puts data.inspect - #result2 = Qiniu::RS.stat(@bucket, @key2) - #result2.should_not be_false + code2, data2 = Qiniu::RS.stat(@bucket, @key2) + code2.should == 200 + puts data2.inspect + + code3, data3 = Qiniu::RS.move(@bucket, @key2, @bucket, @key) + code3.should == 200 + puts data3.inspect + end end - end - context ".batch_move" do - it "should works" do - result = Qiniu::RS::RS.batch_move [@bucket, @key, @bucket, @key2] - result.should_not be_false + context ".copy" do + it "should works" do + code, data = Qiniu::RS.copy(@bucket, @key, @bucket, @key2) + code.should == 200 + puts data.inspect - #result2 = Qiniu::RS.stat(@bucket, @key2) - #result2.should_not be_false - - result3 = Qiniu::RS.batch_move [@bucket, @key2, @bucket, @key] - result3.should_not be_false + #code2, data2 = Qiniu.stat(@bucket, @key2) + #code2.should == 200 + #puts data2.inspect + end end - end -=end - context ".move" do - it "should works" do - result = Qiniu::RS.move(@bucket, @key, @bucket, @key2) - result.should_not be_false - - result2 = Qiniu::RS.stat(@bucket, @key2) - result2.should_not be_false - - result3 = Qiniu::RS.move(@bucket, @key2, @bucket, @key) - result3.should_not be_false + context ".delete" do + it "should works" do + code, data = Qiniu::RS.delete(@bucket, @key) + code.should == 200 + puts data.inspect + end end - end - context ".copy" do - it "should works" do - result = Qiniu::RS.copy(@bucket, @key, @bucket, @key2) - result.should_not be_false - - #result2 = Qiniu::RS.stat(@bucket, @key2) - #result2.should_not be_false + context ".drop" do + it "should works" do + code, data = Qiniu::RS.drop(@bucket) + code.should == 200 + puts data.inspect + end end + end - - context ".delete" do - it "should works" do - result = Qiniu::RS.delete(@bucket, @key) - result.should_not be_false - end - end - - context ".drop" do - it "should works" do - result = Qiniu::RS.drop(@bucket) - result.should_not be_false - end - end - - context ".image_info" do - it "should works" do - data = Qiniu::RS.get(@test_image_bucket, @test_image_key) - data.should_not be_false - data.should_not be_empty - puts data.inspect - result = Qiniu::RS.image_info(data["url"]) - result.should_not be_false - result.should_not be_empty - puts result.inspect - end - end - -=begin - context ".image_exif" do - it "should works" do - data = Qiniu::RS.get(@test_image_bucket, @test_image_key) - data.should_not be_false - data.should_not be_empty - puts data.inspect - result = Qiniu::RS.image_exif(data["url"]) - puts result.inspect - end - end -=end - - context ".image_mogrify_save_as" do - it "should works" do - data = Qiniu::RS.get(@test_image_bucket, @test_image_key) - data.should_not be_false - data.should_not be_empty - puts data.inspect - - dest_key = "cropped-" + @test_image_key - src_img_url = data["url"] - mogrify_options = { - :thumbnail => "!120x120>", - :gravity => "center", - :crop => "!120x120a0a0", - :quality => 85, - :rotate => 45, - :format => "jpg", - :auto_orient => true - } - result2 = Qiniu::RS.image_mogrify_save_as(@test_image_bucket, dest_key, src_img_url, mogrify_options) - result2.should_not be_false - result2.should_not be_empty - puts result2.inspect - end - end - - context ".generate_upload_token" do - it "should works" do - data = Qiniu::RS.generate_upload_token({:scope => @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://*.dn.qbox.me/*'}) - data.should_not be_empty - puts data.inspect - data.split(":").length.should == 3 - end - end - end end diff --git a/spec/qiniu/rs/up_spec.rb b/spec/qiniu/up_spec.rb similarity index 66% rename from spec/qiniu/rs/up_spec.rb rename to spec/qiniu/up_spec.rb index 0c22a34..dd82c2e 100755 --- a/spec/qiniu/rs/up_spec.rb +++ b/spec/qiniu/up_spec.rb @@ -2,32 +2,32 @@ require 'digest/sha1' require 'spec_helper' -require 'qiniu/rs/rs' -require 'qiniu/rs/up' +require 'qiniu' +require 'qiniu/up' module Qiniu - module RS + module UP describe UP do before :all do @localfile = "bigfile.txt" - File.open(@localfile, "w"){|f| 5242888.times{f.write(rand(9).to_s)}} + File.open(@localfile, "w"){|f| 5242888.times{ f.write(rand(9).to_s) }} @bucket = 'RubySdkTest' + (Time.now.to_i+rand(1000)).to_s @key = Digest::SHA1.hexdigest(@localfile+Time.now.to_s) @localfile2 = "bigfile2.txt" - File.open(@localfile2, "w"){|f| (1 << 22).times{f.write(rand(9).to_s)}} + File.open(@localfile2, "w"){|f| (1 << 22).times{ f.write(rand(9).to_s) }} @key2 = Digest::SHA1.hexdigest(@localfile2+Time.now.to_s) @localfile3 = "bigfile3.txt" - File.open(@localfile3, "w"){|f| (1 << 23).times{f.write(rand(9).to_s)}} + File.open(@localfile3, "w"){|f| (1 << 23).times{ f.write(rand(9).to_s) }} @key3 = Digest::SHA1.hexdigest(@localfile3+Time.now.to_s) @localfile4 = "smallfile.txt" - File.open(@localfile4, "w"){|f| (1 << 20).times{f.write(rand(9).to_s)}} + File.open(@localfile4, "w"){|f| (1 << 20).times{ f.write(rand(9).to_s) }} @key4 = Digest::SHA1.hexdigest(@localfile4+Time.now.to_s) - code, data = Qiniu::RS::RS.mkbucket(@bucket) + code, data = Qiniu::RS.mkbucket(@bucket) puts [code, data].inspect code.should == 200 end @@ -38,7 +38,7 @@ module Qiniu File.unlink(@localfile3) if File.exists?(@localfile3) File.unlink(@localfile4) if File.exists?(@localfile4) - code, data = Qiniu::RS::RS.drop(@bucket) + code, data = Qiniu::RS.drop(@bucket) puts [code, data].inspect code.should == 200 end @@ -46,8 +46,8 @@ module Qiniu context ".upload_with_token" do it "should works" do upopts = {:scope => @bucket, :expires_in => 3600, :customer => "why404@gmail.com"} - uptoken = Qiniu::RS.generate_upload_token(upopts) - code, data = Qiniu::RS::UP.upload_with_token(uptoken, @localfile, @bucket, @key) + uptoken = Qiniu.generate_upload_token(upopts) + code, data = Qiniu::UP.upload_with_token(uptoken, @localfile, @bucket, @key) puts data.inspect (code/100).should == 2 end @@ -55,7 +55,7 @@ module Qiniu context ".stat" do it "should exists" do - code, data = Qiniu::RS::RS.stat(@bucket, @key) + code, data = Qiniu::RS.stat(@bucket, @key) puts data.inspect code.should == 200 end @@ -63,7 +63,7 @@ module Qiniu context ".delete" do it "should works" do - code, data = Qiniu::RS::RS.delete(@bucket, @key) + code, data = Qiniu::RS.delete(@bucket, @key) puts data.inspect code.should == 200 end @@ -72,8 +72,8 @@ module Qiniu context ".upload_with_token2" do it "should works" do upopts = {:scope => @bucket, :expires_in => 3600, :customer => "why404@gmail.com"} - uptoken = Qiniu::RS.generate_upload_token(upopts) - code, data = Qiniu::RS::UP.upload_with_token(uptoken, @localfile2, @bucket, @key2) + uptoken = Qiniu.generate_upload_token(upopts) + code, data = Qiniu::UP.upload_with_token(uptoken, @localfile2, @bucket, @key2) puts data.inspect (code/100).should == 2 end @@ -81,7 +81,7 @@ module Qiniu context ".stat" do it "should exists" do - code, data = Qiniu::RS::RS.stat(@bucket, @key2) + code, data = Qiniu::RS.stat(@bucket, @key2) puts data.inspect code.should == 200 end @@ -89,7 +89,7 @@ module Qiniu context ".delete" do it "should works" do - code, data = Qiniu::RS::RS.delete(@bucket, @key2) + code, data = Qiniu::RS.delete(@bucket, @key2) puts data.inspect code.should == 200 end @@ -98,8 +98,8 @@ module Qiniu context ".upload_with_token3" do it "should works" do upopts = {:scope => @bucket, :expires_in => 3600, :customer => "why404@gmail.com"} - uptoken = Qiniu::RS.generate_upload_token(upopts) - code, data = Qiniu::RS::UP.upload_with_token(uptoken, @localfile3, @bucket, @key3) + uptoken = Qiniu.generate_upload_token(upopts) + code, data = Qiniu::UP.upload_with_token(uptoken, @localfile3, @bucket, @key3) puts data.inspect (code/100).should == 2 end @@ -107,7 +107,7 @@ module Qiniu context ".stat" do it "should exists" do - code, data = Qiniu::RS::RS.stat(@bucket, @key3) + code, data = Qiniu::RS.stat(@bucket, @key3) puts data.inspect code.should == 200 end @@ -115,7 +115,7 @@ module Qiniu context ".delete" do it "should works" do - code, data = Qiniu::RS::RS.delete(@bucket, @key3) + code, data = Qiniu::RS.delete(@bucket, @key3) puts data.inspect code.should == 200 end @@ -124,8 +124,8 @@ module Qiniu context ".upload_with_token4" do it "should works" do upopts = {:scope => @bucket, :expires_in => 3600, :customer => "why404@gmail.com"} - uptoken = Qiniu::RS.generate_upload_token(upopts) - code, data = Qiniu::RS::UP.upload_with_token(uptoken, @localfile4, @bucket, @key4) + uptoken = Qiniu.generate_upload_token(upopts) + code, data = Qiniu::UP.upload_with_token(uptoken, @localfile4, @bucket, @key4) puts data.inspect (code/100).should == 2 end @@ -133,7 +133,7 @@ module Qiniu context ".stat" do it "should exists" do - code, data = Qiniu::RS::RS.stat(@bucket, @key4) + code, data = Qiniu::RS.stat(@bucket, @key4) puts data.inspect code.should == 200 end @@ -141,12 +141,12 @@ module Qiniu context ".delete" do it "should works" do - code, data = Qiniu::RS::RS.delete(@bucket, @key4) + code, data = Qiniu::RS.delete(@bucket, @key4) puts data.inspect code.should == 200 end end end - end -end + end # module UP +end # module Qiniu diff --git a/spec/qiniu/rs/utils_spec.rb b/spec/qiniu/utils_spec.rb similarity index 98% rename from spec/qiniu/rs/utils_spec.rb rename to spec/qiniu/utils_spec.rb index 74606c8..fb00074 100755 --- a/spec/qiniu/rs/utils_spec.rb +++ b/spec/qiniu/utils_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' require 'fakeweb' -require 'qiniu/rs/utils' +require 'qiniu/utils' module Qiniu module RS diff --git a/spec/qiniu/version_spec.rb b/spec/qiniu/version_spec.rb new file mode 100755 index 0000000..654ae04 --- /dev/null +++ b/spec/qiniu/version_spec.rb @@ -0,0 +1,10 @@ +# -*- encoding: utf-8 -*- + +require 'spec_helper' +require 'qiniu/version' + +describe Qiniu::Version do + it "should has a VERSION" do + Qiniu::Version.to_s.should =~ /^\d+\.\d+\.\d+?$/ + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index e4a899f..e695e04 100755 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,11 +1,11 @@ # -*- encoding: utf-8 -*- require 'bundler/setup' -require 'qiniu/rs' +require 'qiniu' require 'rspec' RSpec.configure do |config| config.before :all do - Qiniu::RS.establish_connection! :access_key => ENV["QINIU_ACCESS_KEY"], :secret_key => ENV["QINIU_SECRET_KEY"] + Qiniu.establish_connection! :access_key => ENV["QINIU_ACCESS_KEY"], :secret_key => ENV["QINIU_SECRET_KEY"] end end From 51c719767f5c18c33938167b6aea8be65c12411e Mon Sep 17 00:00:00 2001 From: Liang Tao Date: Mon, 3 Mar 2014 14:18:45 +0800 Subject: [PATCH 12/21] =?UTF-8?q?=E4=BF=AE=E6=AD=A3gemspec=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- qiniu-rs.gemspec | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/qiniu-rs.gemspec b/qiniu-rs.gemspec index 21e5ffb..7558e5a 100755 --- a/qiniu-rs.gemspec +++ b/qiniu-rs.gemspec @@ -1,6 +1,6 @@ # -*- encoding: utf-8 -*- -require File.expand_path('../lib/qiniu/rs/version', __FILE__) +require File.expand_path('../lib/qiniu/version', __FILE__) Gem::Specification.new do |gem| gem.authors = ["why404"] @@ -14,7 +14,7 @@ Gem::Specification.new do |gem| gem.test_files = gem.files.grep(%r{^(test|spec|features)/}) gem.name = "qiniu-rs" gem.require_paths = ["lib"] - gem.version = Qiniu::RS::Version.to_s + gem.version = Qiniu::Version.to_s # specify any dependencies here; for example: gem.add_development_dependency "rake", ">= 0.9" From cd48eca8f62f123ef58e779ec0c99948ded698bc Mon Sep 17 00:00:00 2001 From: Liang Tao Date: Mon, 3 Mar 2014 16:13:43 +0800 Subject: [PATCH 13/21] =?UTF-8?q?=E4=BB=A5Storage=E4=B8=BA=E6=96=B0?= =?UTF-8?q?=E7=9A=84=E5=91=BD=E5=90=8D=E7=A9=BA=E9=97=B4=E6=9D=A5=E7=BB=84?= =?UTF-8?q?=E7=BB=87=E4=B8=8E=E5=AD=98=E5=82=A8=E7=9B=B8=E5=85=B3=E7=9A=84?= =?UTF-8?q?SDK=E5=87=BD=E6=95=B0=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/qiniu/resumable_upload.rb | 307 ++++++++++++++++++++++++++++++++++ lib/qiniu/storage.rb | 4 + lib/qiniu/upload.rb | 114 +++++++++++++ spec/qiniu/storage_spec.rb | 184 ++++++++++++++++++++ 4 files changed, 609 insertions(+) create mode 100755 lib/qiniu/resumable_upload.rb create mode 100755 lib/qiniu/storage.rb create mode 100755 lib/qiniu/upload.rb create mode 100755 spec/qiniu/storage_spec.rb diff --git a/lib/qiniu/resumable_upload.rb b/lib/qiniu/resumable_upload.rb new file mode 100755 index 0000000..290458f --- /dev/null +++ b/lib/qiniu/resumable_upload.rb @@ -0,0 +1,307 @@ +# -*- encoding: utf-8 -*- + +require 'zlib' +require 'yaml' +require 'tmpdir' +require 'fileutils' +require 'mime/types' +require 'digest/sha1' +require 'qiniu/abstract' +require 'qiniu/exceptions' +require 'qiniu/io' + +module Qiniu + module Storage + + module AbstractClass + class ChunkProgressNotifier + include Qiniu::Abstract + abstract_methods :notify + # def notify(block_index, block_put_progress); end + end + + class BlockProgressNotifier + include Qiniu::Abstract + abstract_methods :notify + # def notify(block_index, checksum); end + end + end # module AbstractClass + + class ChunkProgressNotifier < AbstractClass::ChunkProgressNotifier + def notify(index, progress) + logmsg = "chunk #{progress[:offset]/Config.settings[:chunk_size]} in block #{index} successfully uploaded.\n" + progress.to_s + Utils.debug(logmsg) + end + end # class ChunkProgressNotifier + + class BlockProgressNotifier < AbstractClass::BlockProgressNotifier + def notify(index, checksum) + Utils.debug "block #{index}: {ctx: #{checksum}} successfully uploaded." + Utils.debug "block #{index}: {checksum: #{checksum}} successfully uploaded." + end + end # class BlockProgressNotifier + + class << self + include Utils + + def resumable_upload_with_token(uptoken, + local_file, + bucket, + key = nil, + mime_type = nil, + custom_meta = nil, + customer = nil, + callback_params = nil, + rotate = nil) + begin + ifile = File.open(local_file, 'rb') + fh = FileData.new(ifile) + fsize = fh.data_size + key = Digest::SHA1.hexdigest(local_file + fh.mtime.to_s) if key.nil? + if mime_type.nil? || mime_type.empty? + mime = MIME::Types.type_for local_file + mime_type = mime.empty? ? 'application/octet-stream' : mime[0].content_type + end + code, data = _resumable_upload(uptoken, fh, fsize, bucket, key, mime_type, custom_meta, customer, callback_params, rotate) + [code, data] + ensure + ifile.close unless ifile.nil? + end + end # resumable_upload_with_token + + private + + class FileData + attr_accessor :fh + def initialize(fh) + @fh = fh + end + def data_size + @fh.stat.size + end + def get_data(offset, length) + @fh.seek(offset) + @fh.read(length) + end + def path + @fh.path + end + def mtime + @fh.mtime + end + #delegate :path, :mtime, :to => :fh + end # class FileData + + def _new_block_put_progress_data + {:ctx => nil, :offset => 0, :restsize => nil, :status_code => nil, :host => nil} + end # _new_block_put_progress_data + + def _call_binary_with_token(uptoken, url, data, content_type = nil, retry_times = 0) + options = { + :method => :post, + :content_type => 'application/octet-stream', + :upload_signature_token => uptoken + } + options[:content_type] = content_type if !content_type.nil? && !content_type.empty? + code, data = http_request url, data, options + unless Utils.is_response_ok?(code) + retry_times += 1 + if Config.settings[:auto_reconnect] && retry_times < Config.settings[:max_retry_times] + return _call_binary_with_token(uptoken, url, data, options[:content_type], retry_times) + end + end + [code, data] + end # _call_binary_with_token + + def _mkblock(uptoken, block_size, body) + url = Config.settings[:up_host] + "/mkblk/#{block_size}" + _call_binary_with_token(uptoken, url, body) + end # _mkblock + + def _putblock(uphost, uptoken, ctx, offset, body) + url = uphost + "/bput/#{ctx}/#{offset}" + _call_binary_with_token(uptoken, url, body) + end # _putblock + + 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. + if progress[:ctx] == nil || progress[:ctx].empty? + progress[:offset] = 0 + progress[:restsize] = block_size + # choose the smaller one + body_length = [block_size, chunk_size].min + for i in 1..retry_times + seek_pos = block_index*Config.settings[:block_size] + body = fh.get_data(seek_pos, body_length) + result_length = body.length + if result_length != body_length + raise FileSeekReadError.new(fpath, block_index, seek_pos, body_length, result_length) + end + code, data = _mkblock(uptoken, block_size, body) + body_crc32 = Zlib.crc32(body) + if Utils.is_response_ok?(code) && data["crc32"] == body_crc32 + progress[:ctx] = data["ctx"] + progress[:offset] = body_length + progress[:restsize] = block_size - body_length + progress[:status_code] = code + progress[:host] = data["host"] + if !notifier.nil? && notifier.respond_to?("notify") + notifier.notify(block_index, progress) + end + break + elsif i == retry_times && data["crc32"] != body_crc32 + Log.logger.error %Q(Uploading block error. Expected crc32: #{body_crc32}, but got: #{data["crc32"]}) + end + end + elsif progress[:offset] + progress[:restsize] != block_size + raise BlockSizeNotMathchError.new(fpath, block_index, progress[:offset], progress[:restsize], block_size) + end + + # loop uploading other chunks except the first one + while progress[:restsize].to_i > 0 && progress[:restsize] < block_size + # choose the smaller one + body_length = [progress[:restsize], chunk_size].min + for i in 1..retry_times + seek_pos = block_index*Config.settings[:block_size] + progress[:offset] + body = fh.get_data(seek_pos, body_length) + result_length = body.length + if result_length != body_length + raise FileSeekReadError.new(fpath, block_index, seek_pos, body_length, result_length) + end + code, data = _putblock(progress[:host], uptoken, progress[:ctx], progress[:offset], body) + body_crc32 = Zlib.crc32(body) + if Utils.is_response_ok?(code) && data["crc32"] == body_crc32 + progress[:ctx] = data["ctx"] + progress[:offset] += body_length + progress[:restsize] -= body_length + progress[:status_code] = code + progress[:host] = data["host"] + if !notifier.nil? && notifier.respond_to?("notify") + notifier.notify(block_index, progress) + end + break + elsif i == retry_times && data["crc32"] != body_crc32 + Log.logger.error %Q(Uploading block error. Expected crc32: #{body_crc32}, but got: #{data["crc32"]}) + end + end + end + # return + return [code, data] + end # _resumable_put_block + + def _block_count(fsize) + ((fsize + Config.settings[:block_size] - 1) / Config.settings[:block_size]).to_i + end # _block_count + + def _resumable_put(uptoken, + fh, + checksums, + progresses, + block_notifier = nil, + chunk_notifier = nil) + code, data = 0, {} + fsize = fh.data_size + block_count = _block_count(fsize) + checksum_count = checksums.length + progress_count = progresses.length + if checksum_count != block_count || progress_count != block_count + raise BlockCountNotMathchError.new(fh.path, block_count, checksum_count, progress_count) + end + 0.upto(block_count-1).each do |block_index| + if checksums[block_index].nil? || checksums[block_index].empty? + block_size = Config.settings[:block_size] + if block_index == block_count - 1 + block_size = fsize - block_index*Config.settings[:block_size] + end + if progresses[block_index].nil? + progresses[block_index] = _new_block_put_progress_data + end + #code, data = _resumable_put_block(uptoken, fh, block_index, block_size, Config.settings[:chunk_size], progresses[block_index], Config.settings[:max_retry_times], chunk_notifier) + # Put the whole block as a chunk + code, data = _resumable_put_block(uptoken, fh, block_index, block_size, block_size, progresses[block_index], Config.settings[:max_retry_times], chunk_notifier) + if Utils.is_response_ok?(code) + #checksums[block_index] = data["checksum"] + checksums[block_index] = data["ctx"] + if !block_notifier.nil? && block_notifier.respond_to?("notify") + block_notifier.notify(block_index, checksums[block_index]) + end + end + end + end + return [code, data] + end # _resumable_put + + def _mkfile(uphost, + uptoken, + entry_uri, + fsize, + checksums, + mime_type = nil, + custom_meta = nil, + customer = nil, + callback_params = nil, + rotate = nil) + path = '/rs-mkfile/' + Utils.urlsafe_base64_encode(entry_uri) + "/fsize/#{fsize}" + path += '/mimeType/' + Utils.urlsafe_base64_encode(mime_type) if !mime_type.nil? && !mime_type.empty? + path += '/meta/' + Utils.urlsafe_base64_encode(custom_meta) if !custom_meta.nil? && !custom_meta.empty? + path += '/customer/' + customer if !customer.nil? && !customer.empty? + callback_query_string = Utils.generate_query_string(callback_params) if !callback_params.nil? && !callback_params.empty? + path += '/params/' + Utils.urlsafe_base64_encode(callback_query_string) if !callback_query_string.nil? && !callback_query_string.empty? + path += '/rotate/' + rotate if !rotate.nil? && rotate.to_i >= 0 + url = uphost + path + #body = '' + #checksums.each do |checksum| + # body += Utils.urlsafe_base64_decode(checksum) + #end + body = checksums.join(',') + _call_binary_with_token(uptoken, url, body, 'text/plain') + end # _mkfile + + def _resumable_upload(uptoken, + fh, + fsize, + bucket, + key, + mime_type = nil, + custom_meta = nil, + customer = nil, + callback_params = nil, + rotate = nil) + + block_count = _block_count(fsize) + + chunk_notifier = ChunkProgressNotifier.new() + block_notifier = BlockProgressNotifier.new() + + progresses = [] + block_count.times{progresses << _new_block_put_progress_data} + checksums = [] + block_count.times{checksums << ''} + + code, data = _resumable_put(uptoken, fh, checksums, progresses, block_notifier, chunk_notifier) + + if Utils.is_response_ok?(code) + uphost = data["host"] + entry_uri = bucket + ':' + key + code, data = _mkfile(uphost, uptoken, entry_uri, fsize, checksums, mime_type, custom_meta, customer, callback_params, rotate) + end + + if Utils.is_response_ok?(code) + Utils.debug "File #{fh.path} {size: #{fsize}} successfully uploaded." + end + + [code, data] + end # _resumable_upload + end # self class + end # module Storage +end # module Qiniu diff --git a/lib/qiniu/storage.rb b/lib/qiniu/storage.rb new file mode 100755 index 0000000..c10da81 --- /dev/null +++ b/lib/qiniu/storage.rb @@ -0,0 +1,4 @@ +# -*- encoding: utf-8 -*- + +require 'qiniu/upload' +require 'qiniu/resumable_upload' diff --git a/lib/qiniu/upload.rb b/lib/qiniu/upload.rb new file mode 100755 index 0000000..73f9d98 --- /dev/null +++ b/lib/qiniu/upload.rb @@ -0,0 +1,114 @@ +# -*- encoding: utf-8 -*- + +module Qiniu + module Storage + class << self + include Utils + + def put_file(local_file, + bucket, + key = nil, + mime_type = nil, + custom_meta = nil, + enable_crc32_check = false) + action_params = _generate_action_params( + local_file, + bucket, + key, + mime_type, + custom_meta, + enable_crc32_check + ) + + url = Config.settings[:io_host] + action_params + options = {:content_type => 'application/octet-stream'} + + Auth.request url, ::IO.read(local_file), options + end # put_file + + def upload_with_token(uptoken, + local_file, + bucket, + key = nil, + mime_type = nil, + custom_meta = nil, + callback_params = nil, + enable_crc32_check = false, + rotate = nil) + action_params = _generate_action_params( + local_file, + bucket, + key, + mime_type, + custom_meta, + enable_crc32_check, + rotate + ) + + if callback_params.nil? + callback_params = {:bucket => bucket, :key => key, :mime_type => mime_type} + end + 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 # upload_with_token + + 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? + post_data[:token] = uptoken + end + if not key.nil? + post_data[:key] = key + end + if x_vars.is_a?(Hash) + 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) + + if key.nil? + key = Digest::SHA1.hexdigest(local_file + Time.now.to_s) + end + + entry_uri = bucket + ':' + key + if mime_type.nil? || mime_type.empty? + mime = MIME::Types.type_for local_file + mime_type = mime.empty? ? 'application/octet-stream' : mime[0].content_type + end + + action_params = '/rs-put/' + Utils.urlsafe_base64_encode(entry_uri) + '/mimeType/' + Utils.urlsafe_base64_encode(mime_type) + action_params += '/meta/' + Utils.urlsafe_base64_encode(custom_meta) unless custom_meta.nil? + action_params += '/crc32/' + Utils.crc32checksum(local_file).to_s if enable_crc32_check + action_params += '/rotate/' + rotate if !rotate.nil? && rotate.to_i >= 0 + action_params + end # _generate_action_params + + end # class << self + end # module Storage +end # module Qiniu diff --git a/spec/qiniu/storage_spec.rb b/spec/qiniu/storage_spec.rb new file mode 100755 index 0000000..412d109 --- /dev/null +++ b/spec/qiniu/storage_spec.rb @@ -0,0 +1,184 @@ +# -*- encoding: utf-8 -*- + +require 'spec_helper' +require 'qiniu/auth' +require 'qiniu/storage' +require 'digest/sha1' + +module Qiniu + module Storage + describe Storage do + + before :all do + @bucket = 'RubySdkTest' + (Time.now.to_i+rand(1000)).to_s + @key = Digest::SHA1.hexdigest((Time.now.to_i+rand(100)).to_s) + + @localfile1 = "bigfile.txt" + File.open(@localfile1, "w"){|f| 5242888.times{ f.write(rand(9).to_s) }} + @key1 = Digest::SHA1.hexdigest(@localfile1+Time.now.to_s) + + @localfile2 = "bigfile2.txt" + File.open(@localfile2, "w"){|f| (1 << 22).times{ f.write(rand(9).to_s) }} + @key2 = Digest::SHA1.hexdigest(@localfile2+Time.now.to_s) + + @localfile3 = "bigfile3.txt" + File.open(@localfile3, "w"){|f| (1 << 23).times{ f.write(rand(9).to_s) }} + @key3 = Digest::SHA1.hexdigest(@localfile3+Time.now.to_s) + + @localfile4 = "smallfile.txt" + File.open(@localfile4, "w"){|f| (1 << 20).times{ f.write(rand(9).to_s) }} + @key4 = Digest::SHA1.hexdigest(@localfile4+Time.now.to_s) + + result = Qiniu.mkbucket(@bucket) + puts result.inspect + result.should be_true + end + + after :all do + File.unlink(@localfile1) if File.exists?(@localfile1) + File.unlink(@localfile2) if File.exists?(@localfile2) + File.unlink(@localfile3) if File.exists?(@localfile3) + File.unlink(@localfile4) if File.exists?(@localfile4) + + result = Qiniu.drop(@bucket) + puts result.inspect + result.should_not be_false + end + + context ".put_file" do + it "should works" do + code, data = Qiniu::Storage.put_file(__FILE__, @bucket, @key, 'application/x-ruby', 'customMeta', true) + code.should == 200 + puts data.inspect + end + end + + context ".upload_with_token" do + it "should works" do + upopts = {:scope => @bucket, :expires_in => 3600, :customer => "why404@gmail.com"} + uptoken = Qiniu.generate_upload_token(upopts) + code, data = Qiniu::Storage.upload_with_token(uptoken, __FILE__, @bucket, @key, nil, nil, nil, true) + code.should == 200 + puts data.inspect + end + end + + context ".upload_with_token_2" do + it "should works" do + upopts = {:scope => @bucket, :expires_in => 3600, :endUser => "why404@gmail.com"} + uptoken = Qiniu.generate_upload_token(upopts) + + code, data = Qiniu::Storage.upload_with_token_2(uptoken, __FILE__, @key) + + code.should == 200 + puts data.inspect + end + end # .upload_with_token_2 + + context ".resumable_upload_with_token" do + it "should works" do + upopts = {:scope => @bucket, :expires_in => 3600, :customer => "why404@gmail.com"} + uptoken = Qiniu.generate_upload_token(upopts) + code, data = Qiniu::Storage.resumable_upload_with_token(uptoken, @localfile1, @bucket, @key1) + puts data.inspect + (code/100).should == 2 + end + end + + context ".stat" do + it "should exists" do + code, data = Qiniu::RS.stat(@bucket, @key1) + puts data.inspect + code.should == 200 + end + end + + context ".delete" do + it "should works" do + code, data = Qiniu::RS.delete(@bucket, @key1) + puts data.inspect + code.should == 200 + end + end + + context ".resumable_upload_with_token2" do + it "should works" do + upopts = {:scope => @bucket, :expires_in => 3600, :customer => "why404@gmail.com"} + uptoken = Qiniu.generate_upload_token(upopts) + code, data = Qiniu::Storage.resumable_upload_with_token(uptoken, @localfile2, @bucket, @key2) + puts data.inspect + (code/100).should == 2 + end + end + + context ".stat" do + it "should exists" do + code, data = Qiniu::RS.stat(@bucket, @key2) + puts data.inspect + code.should == 200 + end + end + + context ".delete" do + it "should works" do + code, data = Qiniu::RS.delete(@bucket, @key2) + puts data.inspect + code.should == 200 + end + end + + context ".resumable_upload_with_token3" do + it "should works" do + upopts = {:scope => @bucket, :expires_in => 3600, :customer => "why404@gmail.com"} + uptoken = Qiniu.generate_upload_token(upopts) + code, data = Qiniu::Storage.resumable_upload_with_token(uptoken, @localfile3, @bucket, @key3) + puts data.inspect + (code/100).should == 2 + end + end + + context ".stat" do + it "should exists" do + code, data = Qiniu::RS.stat(@bucket, @key3) + puts data.inspect + code.should == 200 + end + end + + context ".delete" do + it "should works" do + code, data = Qiniu::RS.delete(@bucket, @key3) + puts data.inspect + code.should == 200 + end + end + + context ".resumable_upload_with_token4" do + it "should works" do + upopts = {:scope => @bucket, :expires_in => 3600, :customer => "why404@gmail.com"} + uptoken = Qiniu.generate_upload_token(upopts) + code, data = Qiniu::Storage.resumable_upload_with_token(uptoken, @localfile4, @bucket, @key4) + puts data.inspect + (code/100).should == 2 + end + end + + context ".stat" do + it "should exists" do + code, data = Qiniu::RS.stat(@bucket, @key4) + puts data.inspect + code.should == 200 + end + end + + context ".delete" do + it "should works" do + code, data = Qiniu::RS.delete(@bucket, @key4) + puts data.inspect + code.should == 200 + end + end + + end + end # module Storage +end # module Qiniu From 36c492b9b9c9396e49cbe15d53dde4c3e71dfebe Mon Sep 17 00:00:00 2001 From: Liang Tao Date: Mon, 3 Mar 2014 16:40:04 +0800 Subject: [PATCH 14/21] =?UTF-8?q?=E5=8E=BB=E9=99=A4io/up=E6=A8=A1=E5=9D=97?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/qiniu/up.rb | 279 ------------------------------------------ spec/qiniu/io_spec.rb | 59 --------- spec/qiniu/up_spec.rb | 152 ----------------------- 3 files changed, 490 deletions(-) delete mode 100755 lib/qiniu/up.rb delete mode 100755 spec/qiniu/io_spec.rb delete mode 100755 spec/qiniu/up_spec.rb diff --git a/lib/qiniu/up.rb b/lib/qiniu/up.rb deleted file mode 100755 index 0ed2fe9..0000000 --- a/lib/qiniu/up.rb +++ /dev/null @@ -1,279 +0,0 @@ -# -*- encoding: utf-8 -*- - -require 'zlib' -require 'yaml' -require 'tmpdir' -require 'fileutils' -require 'mime/types' -require 'digest/sha1' -require 'qiniu/abstract' -require 'qiniu/exceptions' -require 'qiniu/io' - -module Qiniu - module UP - - module AbstractClass - class ChunkProgressNotifier - include Qiniu::Abstract - abstract_methods :notify - # def notify(block_index, block_put_progress); end - end - - class BlockProgressNotifier - include Qiniu::Abstract - abstract_methods :notify - # def notify(block_index, checksum); end - end - end - - class ChunkProgressNotifier < AbstractClass::ChunkProgressNotifier - def notify(index, progress) - logmsg = "chunk #{progress[:offset]/Config.settings[:chunk_size]} in block #{index} successfully uploaded.\n" + progress.to_s - Utils.debug(logmsg) - end - end - - class BlockProgressNotifier < AbstractClass::BlockProgressNotifier - def notify(index, checksum) - Utils.debug "block #{index}: {ctx: #{checksum}} successfully uploaded." - Utils.debug "block #{index}: {checksum: #{checksum}} successfully uploaded." - end - end - - class << self - include Utils - - def upload_with_token(uptoken, - local_file, - bucket, - key = nil, - mime_type = nil, - custom_meta = nil, - customer = nil, - callback_params = nil, - rotate = nil) - begin - ifile = File.open(local_file, 'rb') - fh = FileData.new(ifile) - fsize = fh.data_size - key = Digest::SHA1.hexdigest(local_file + fh.mtime.to_s) if key.nil? - if mime_type.nil? || mime_type.empty? - mime = MIME::Types.type_for local_file - mime_type = mime.empty? ? 'application/octet-stream' : mime[0].content_type - end - code, data = _resumable_upload(uptoken, fh, fsize, bucket, key, mime_type, custom_meta, customer, callback_params, rotate) - [code, data] - ensure - ifile.close unless ifile.nil? - end - end - - private - - class FileData - attr_accessor :fh - def initialize(fh) - @fh = fh - end - def data_size - @fh.stat.size - end - def get_data(offset, length) - @fh.seek(offset) - @fh.read(length) - end - def path - @fh.path - end - def mtime - @fh.mtime - end - #delegate :path, :mtime, :to => :fh - end - - def _new_block_put_progress_data - {:ctx => nil, :offset => 0, :restsize => nil, :status_code => nil, :host => nil} - end - - def _call_binary_with_token(uptoken, url, data, content_type = nil, retry_times = 0) - options = { - :method => :post, - :content_type => 'application/octet-stream', - :upload_signature_token => uptoken - } - options[:content_type] = content_type if !content_type.nil? && !content_type.empty? - code, data = http_request url, data, options - unless Utils.is_response_ok?(code) - retry_times += 1 - if Config.settings[:auto_reconnect] && retry_times < Config.settings[:max_retry_times] - return _call_binary_with_token(uptoken, url, data, options[:content_type], retry_times) - end - end - [code, data] - end - - def _mkblock(uptoken, block_size, body) - url = Config.settings[:up_host] + "/mkblk/#{block_size}" - _call_binary_with_token(uptoken, url, body) - end - - def _putblock(uphost, uptoken, ctx, offset, body) - url = uphost + "/bput/#{ctx}/#{offset}" - _call_binary_with_token(uptoken, url, body) - end - - 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. - if progress[:ctx] == nil || progress[:ctx].empty? - progress[:offset] = 0 - progress[:restsize] = block_size - # choose the smaller one - body_length = [block_size, chunk_size].min - for i in 1..retry_times - seek_pos = block_index*Config.settings[:block_size] - body = fh.get_data(seek_pos, body_length) - result_length = body.length - if result_length != body_length - raise FileSeekReadError.new(fpath, block_index, seek_pos, body_length, result_length) - end - code, data = _mkblock(uptoken, block_size, body) - body_crc32 = Zlib.crc32(body) - if Utils.is_response_ok?(code) && data["crc32"] == body_crc32 - progress[:ctx] = data["ctx"] - progress[:offset] = body_length - progress[:restsize] = block_size - body_length - progress[:status_code] = code - progress[:host] = data["host"] - if !notifier.nil? && notifier.respond_to?("notify") - notifier.notify(block_index, progress) - end - break - elsif i == retry_times && data["crc32"] != body_crc32 - Log.logger.error %Q(Uploading block error. Expected crc32: #{body_crc32}, but got: #{data["crc32"]}) - end - end - elsif progress[:offset] + progress[:restsize] != block_size - raise BlockSizeNotMathchError.new(fpath, block_index, progress[:offset], progress[:restsize], block_size) - end - - # loop uploading other chunks except the first one - while progress[:restsize].to_i > 0 && progress[:restsize] < block_size - # choose the smaller one - body_length = [progress[:restsize], chunk_size].min - for i in 1..retry_times - seek_pos = block_index*Config.settings[:block_size] + progress[:offset] - body = fh.get_data(seek_pos, body_length) - result_length = body.length - if result_length != body_length - raise FileSeekReadError.new(fpath, block_index, seek_pos, body_length, result_length) - end - code, data = _putblock(progress[:host], uptoken, progress[:ctx], progress[:offset], body) - body_crc32 = Zlib.crc32(body) - if Utils.is_response_ok?(code) && data["crc32"] == body_crc32 - progress[:ctx] = data["ctx"] - progress[:offset] += body_length - progress[:restsize] -= body_length - progress[:status_code] = code - progress[:host] = data["host"] - if !notifier.nil? && notifier.respond_to?("notify") - notifier.notify(block_index, progress) - end - break - elsif i == retry_times && data["crc32"] != body_crc32 - Log.logger.error %Q(Uploading block error. Expected crc32: #{body_crc32}, but got: #{data["crc32"]}) - end - end - end - # return - return [code, data] - end - - def _block_count(fsize) - ((fsize + Config.settings[:block_size] - 1) / Config.settings[:block_size]).to_i - end - - def _resumable_put(uptoken, fh, checksums, progresses, block_notifier = nil, chunk_notifier = nil) - code, data = 0, {} - fsize = fh.data_size - block_count = _block_count(fsize) - checksum_count = checksums.length - progress_count = progresses.length - if checksum_count != block_count || progress_count != block_count - raise BlockCountNotMathchError.new(fh.path, block_count, checksum_count, progress_count) - end - 0.upto(block_count-1).each do |block_index| - if checksums[block_index].nil? || checksums[block_index].empty? - block_size = Config.settings[:block_size] - if block_index == block_count - 1 - block_size = fsize - block_index*Config.settings[:block_size] - end - if progresses[block_index].nil? - progresses[block_index] = _new_block_put_progress_data - end - #code, data = _resumable_put_block(uptoken, fh, block_index, block_size, Config.settings[:chunk_size], progresses[block_index], Config.settings[:max_retry_times], chunk_notifier) - # Put the whole block as a chunk - code, data = _resumable_put_block(uptoken, fh, block_index, block_size, block_size, progresses[block_index], Config.settings[:max_retry_times], chunk_notifier) - if Utils.is_response_ok?(code) - #checksums[block_index] = data["checksum"] - checksums[block_index] = data["ctx"] - if !block_notifier.nil? && block_notifier.respond_to?("notify") - block_notifier.notify(block_index, checksums[block_index]) - end - end - end - end - return [code, data] - end - - def _mkfile(uphost, uptoken, entry_uri, fsize, checksums, mime_type = nil, custom_meta = nil, customer = nil, callback_params = nil, rotate = nil) - path = '/rs-mkfile/' + Utils.urlsafe_base64_encode(entry_uri) + "/fsize/#{fsize}" - path += '/mimeType/' + Utils.urlsafe_base64_encode(mime_type) if !mime_type.nil? && !mime_type.empty? - path += '/meta/' + Utils.urlsafe_base64_encode(custom_meta) if !custom_meta.nil? && !custom_meta.empty? - path += '/customer/' + customer if !customer.nil? && !customer.empty? - callback_query_string = Utils.generate_query_string(callback_params) if !callback_params.nil? && !callback_params.empty? - path += '/params/' + Utils.urlsafe_base64_encode(callback_query_string) if !callback_query_string.nil? && !callback_query_string.empty? - path += '/rotate/' + rotate if !rotate.nil? && rotate.to_i >= 0 - url = uphost + path - #body = '' - #checksums.each do |checksum| - # body += Utils.urlsafe_base64_decode(checksum) - #end - body = checksums.join(',') - _call_binary_with_token(uptoken, url, body, 'text/plain') - end - - def _resumable_upload(uptoken, fh, fsize, bucket, key, mime_type = nil, custom_meta = nil, customer = nil, callback_params = nil, rotate = nil) - - block_count = _block_count(fsize) - - chunk_notifier = ChunkProgressNotifier.new() - block_notifier = BlockProgressNotifier.new() - - progresses = [] - block_count.times{progresses << _new_block_put_progress_data} - checksums = [] - block_count.times{checksums << ''} - - code, data = _resumable_put(uptoken, fh, checksums, progresses, block_notifier, chunk_notifier) - - if Utils.is_response_ok?(code) - uphost = data["host"] - entry_uri = bucket + ':' + key - code, data = _mkfile(uphost, uptoken, entry_uri, fsize, checksums, mime_type, custom_meta, customer, callback_params, rotate) - end - - if Utils.is_response_ok?(code) - Utils.debug "File #{fh.path} {size: #{fsize}} successfully uploaded." - end - - [code, data] - end - - end - - end # module UP -end # module Qiniu diff --git a/spec/qiniu/io_spec.rb b/spec/qiniu/io_spec.rb deleted file mode 100755 index 9b596fb..0000000 --- a/spec/qiniu/io_spec.rb +++ /dev/null @@ -1,59 +0,0 @@ -# -*- encoding: utf-8 -*- - -require 'spec_helper' -require 'qiniu/auth' -require 'qiniu/io' -require 'digest/sha1' - -module Qiniu - module IO - describe IO do - - before :all do - @bucket = 'RubySdkTest' + (Time.now.to_i+rand(1000)).to_s - @key = Digest::SHA1.hexdigest((Time.now.to_i+rand(100)).to_s) - - result = Qiniu.mkbucket(@bucket) - puts result.inspect - result.should be_true - end - - after :all do - result = Qiniu.drop(@bucket) - puts result.inspect - result.should_not be_false - end - - context ".put_file" do - it "should works" do - code, data = Qiniu::IO.put_file(__FILE__, @bucket, @key, 'application/x-ruby', 'customMeta', true) - code.should == 200 - puts data.inspect - end - end - - context ".upload_with_token" do - it "should works" do - upopts = {:scope => @bucket, :expires_in => 3600, :customer => "why404@gmail.com"} - uptoken = Qiniu.generate_upload_token(upopts) - code, data = Qiniu::IO.upload_with_token(uptoken, __FILE__, @bucket, @key, nil, nil, nil, true) - code.should == 200 - puts data.inspect - end - end - - context ".upload_with_token_2" do - it "should works" do - upopts = {:scope => @bucket, :expires_in => 3600, :endUser => "why404@gmail.com"} - uptoken = Qiniu.generate_upload_token(upopts) - - code, data = Qiniu::IO.upload_with_token_2(uptoken, __FILE__, @key) - - code.should == 200 - puts data.inspect - end - end # .upload_with_token_2 - - end - end # module IO -end # module Qiniu diff --git a/spec/qiniu/up_spec.rb b/spec/qiniu/up_spec.rb deleted file mode 100755 index dd82c2e..0000000 --- a/spec/qiniu/up_spec.rb +++ /dev/null @@ -1,152 +0,0 @@ -# -*- encoding: utf-8 -*- - -require 'digest/sha1' -require 'spec_helper' -require 'qiniu' -require 'qiniu/up' - -module Qiniu - module UP - describe UP do - - before :all do - @localfile = "bigfile.txt" - File.open(@localfile, "w"){|f| 5242888.times{ f.write(rand(9).to_s) }} - @bucket = 'RubySdkTest' + (Time.now.to_i+rand(1000)).to_s - @key = Digest::SHA1.hexdigest(@localfile+Time.now.to_s) - - @localfile2 = "bigfile2.txt" - File.open(@localfile2, "w"){|f| (1 << 22).times{ f.write(rand(9).to_s) }} - @key2 = Digest::SHA1.hexdigest(@localfile2+Time.now.to_s) - - @localfile3 = "bigfile3.txt" - File.open(@localfile3, "w"){|f| (1 << 23).times{ f.write(rand(9).to_s) }} - @key3 = Digest::SHA1.hexdigest(@localfile3+Time.now.to_s) - - @localfile4 = "smallfile.txt" - File.open(@localfile4, "w"){|f| (1 << 20).times{ f.write(rand(9).to_s) }} - @key4 = Digest::SHA1.hexdigest(@localfile4+Time.now.to_s) - - code, data = Qiniu::RS.mkbucket(@bucket) - puts [code, data].inspect - code.should == 200 - end - - after :all do - File.unlink(@localfile) if File.exists?(@localfile) - File.unlink(@localfile2) if File.exists?(@localfile2) - File.unlink(@localfile3) if File.exists?(@localfile3) - File.unlink(@localfile4) if File.exists?(@localfile4) - - code, data = Qiniu::RS.drop(@bucket) - puts [code, data].inspect - code.should == 200 - end - - context ".upload_with_token" do - it "should works" do - upopts = {:scope => @bucket, :expires_in => 3600, :customer => "why404@gmail.com"} - uptoken = Qiniu.generate_upload_token(upopts) - code, data = Qiniu::UP.upload_with_token(uptoken, @localfile, @bucket, @key) - puts data.inspect - (code/100).should == 2 - end - end - - context ".stat" do - it "should exists" do - code, data = Qiniu::RS.stat(@bucket, @key) - puts data.inspect - code.should == 200 - end - end - - context ".delete" do - it "should works" do - code, data = Qiniu::RS.delete(@bucket, @key) - puts data.inspect - code.should == 200 - end - end - - context ".upload_with_token2" do - it "should works" do - upopts = {:scope => @bucket, :expires_in => 3600, :customer => "why404@gmail.com"} - uptoken = Qiniu.generate_upload_token(upopts) - code, data = Qiniu::UP.upload_with_token(uptoken, @localfile2, @bucket, @key2) - puts data.inspect - (code/100).should == 2 - end - end - - context ".stat" do - it "should exists" do - code, data = Qiniu::RS.stat(@bucket, @key2) - puts data.inspect - code.should == 200 - end - end - - context ".delete" do - it "should works" do - code, data = Qiniu::RS.delete(@bucket, @key2) - puts data.inspect - code.should == 200 - end - end - - context ".upload_with_token3" do - it "should works" do - upopts = {:scope => @bucket, :expires_in => 3600, :customer => "why404@gmail.com"} - uptoken = Qiniu.generate_upload_token(upopts) - code, data = Qiniu::UP.upload_with_token(uptoken, @localfile3, @bucket, @key3) - puts data.inspect - (code/100).should == 2 - end - end - - context ".stat" do - it "should exists" do - code, data = Qiniu::RS.stat(@bucket, @key3) - puts data.inspect - code.should == 200 - end - end - - context ".delete" do - it "should works" do - code, data = Qiniu::RS.delete(@bucket, @key3) - puts data.inspect - code.should == 200 - end - end - - context ".upload_with_token4" do - it "should works" do - upopts = {:scope => @bucket, :expires_in => 3600, :customer => "why404@gmail.com"} - uptoken = Qiniu.generate_upload_token(upopts) - code, data = Qiniu::UP.upload_with_token(uptoken, @localfile4, @bucket, @key4) - puts data.inspect - (code/100).should == 2 - end - end - - context ".stat" do - it "should exists" do - code, data = Qiniu::RS.stat(@bucket, @key4) - puts data.inspect - code.should == 200 - end - end - - context ".delete" do - it "should works" do - code, data = Qiniu::RS.delete(@bucket, @key4) - puts data.inspect - code.should == 200 - end - end - - end - end # module UP -end # module Qiniu From f1cb65d79a34a14c12cf8c0535ca820d7bb506a3 Mon Sep 17 00:00:00 2001 From: Liang Tao Date: Mon, 3 Mar 2014 17:17:42 +0800 Subject: [PATCH 15/21] =?UTF-8?q?=E8=B0=83=E6=95=B4=E6=89=80=E6=9C=89?= =?UTF-8?q?=E7=9A=84=E6=B5=8B=E8=AF=95=E7=94=A8=E4=BE=8B=EF=BC=8C=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E6=96=B0=E7=BB=84=E7=BB=87=E5=A5=BD=E7=9A=84=E5=87=BD?= =?UTF-8?q?=E6=95=B0=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Gemfile.lock | 2 +- lib/qiniu.rb | 48 ++++++------- lib/qiniu/io.rb | 70 ------------------- lib/qiniu/{rs.rb => management.rb} | 53 ++++++-------- lib/qiniu/resumable_upload.rb | 1 - lib/qiniu/storage.rb | 1 + lib/qiniu/version.rb | 4 +- spec/qiniu/image_spec.rb | 2 +- spec/qiniu/{rs_spec.rb => management_spec.rb} | 41 ++++++----- spec/qiniu/qiniu_spec.rb | 4 +- .../qiniu/{storage_spec.rb => upload_spec.rb} | 16 ++--- 11 files changed, 79 insertions(+), 163 deletions(-) delete mode 100755 lib/qiniu/io.rb rename lib/qiniu/{rs.rb => management.rb} (85%) rename spec/qiniu/{rs_spec.rb => management_spec.rb} (76%) rename spec/qiniu/{storage_spec.rb => upload_spec.rb} (91%) diff --git a/Gemfile.lock b/Gemfile.lock index 71d99f5..b952fb1 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - qiniu-rs (3.4.6) + qiniu-rs (3.5.0) json (~> 1.7) mime-types (~> 1.19) rest-client (~> 1.6) diff --git a/lib/qiniu.rb b/lib/qiniu.rb index c766a04..dd66f06 100755 --- a/lib/qiniu.rb +++ b/lib/qiniu.rb @@ -7,9 +7,6 @@ module Qiniu autoload :Config, 'qiniu/config' autoload :Log, 'qiniu/log' autoload :Exception, 'qiniu/exceptions' - autoload :IO, 'qiniu/io' - autoload :UP, 'qiniu/up' - autoload :RS, 'qiniu/rs' autoload :Pub, 'qiniu/pub' autoload :Image, 'qiniu/image' autoload :AccessToken, 'qiniu/tokens/access_token' @@ -17,6 +14,7 @@ module Qiniu autoload :UploadToken, 'qiniu/tokens/upload_token' autoload :DownloadToken, 'qiniu/tokens/download_token' autoload :Abstract, 'qiniu/abstract' + autoload :Storage, 'qiniu/storage' class << self @@ -27,12 +25,12 @@ module Qiniu end def mkbucket(bucket_name) - code, data = RS.mkbucket(bucket_name) + code, data = Storage.mkbucket(bucket_name) code == StatusOK end def buckets - code, data = RS.buckets + code, data = Storage.buckets code == StatusOK ? data : false end @@ -57,7 +55,7 @@ module Qiniu end def put_file opts = {} - code, data = IO.put_file(opts[:file], + code, data = Storage.put_file(opts[:file], opts[:bucket], opts[:key], opts[:mime_type], @@ -76,7 +74,7 @@ module Qiniu opts[:enable_resumable_upload] = true unless opts.has_key?(:enable_resumable_upload) if opts[:enable_resumable_upload] && File::size(source_file) > Config.settings[:block_size] - code, data = UP.upload_with_token(opts[:uptoken], + code, data = Storage.upload_with_token(opts[:uptoken], opts[:file], opts[:bucket], opts[:key], @@ -86,7 +84,7 @@ module Qiniu opts[:callback_params], opts[:rotate]) else - code, data = IO.upload_with_token(opts[:uptoken], + code, data = Storage.upload_with_token(opts[:uptoken], opts[:file], opts[:bucket], opts[:key], @@ -101,62 +99,62 @@ module Qiniu end def stat(bucket, key) - code, data = RS.stat(bucket, key) + code, data = Storage.stat(bucket, key) code == StatusOK ? data : false end def get(bucket, key, save_as = nil, expires_in = nil, version = nil) - code, data = RS.get(bucket, key, save_as, expires_in, version) + code, data = Storage.get(bucket, key, save_as, expires_in, version) code == StatusOK ? data : false end def download(bucket, key, save_as = nil, expires_in = nil, version = nil) - code, data = RS.get(bucket, key, save_as, expires_in, version) + code, data = Storage.get(bucket, key, save_as, expires_in, version) code == StatusOK ? data["url"] : false end def copy(source_bucket, source_key, target_bucket, target_key) - code, data = RS.copy(source_bucket, source_key, target_bucket, target_key) + code, data = Storage.copy(source_bucket, source_key, target_bucket, target_key) code == StatusOK end def move(source_bucket, source_key, target_bucket, target_key) - code, data = RS.move(source_bucket, source_key, target_bucket, target_key) + code, data = Storage.move(source_bucket, source_key, target_bucket, target_key) code == StatusOK end def delete(bucket, key) - code, data = RS.delete(bucket, key) + code, data = Storage.delete(bucket, key) code == StatusOK end def batch(command, bucket, keys) - code, data = RS.batch(command, bucket, keys) + code, data = Storage.batch(command, bucket, keys) code == StatusOK ? data : false end def batch_stat(bucket, keys) - code, data = RS.batch_stat(bucket, keys) + code, data = Storage.batch_stat(bucket, keys) code == StatusOK ? data : false end def batch_get(bucket, keys) - code, data = RS.batch_get(bucket, keys) + code, data = Storage.batch_get(bucket, keys) code == StatusOK ? data : false end def batch_copy(*args) - code, data = RS.batch_copy(args) + code, data = Storage.batch_copy(args) code == StatusOK end def batch_move(*args) - code, data = RS.batch_move(args) + code, data = Storage.batch_move(args) code == StatusOK end def batch_download(bucket, keys) - code, data = RS.batch_get(bucket, keys) + code, data = Storage.batch_get(bucket, keys) return false unless code == StatusOK links = [] data.each { |e| links << e["data"]["url"] } @@ -164,22 +162,22 @@ module Qiniu end def batch_delete(bucket, keys) - code, data = RS.batch_delete(bucket, keys) + code, data = Storage.batch_delete(bucket, keys) code == StatusOK ? data : false end def publish(domain, bucket) - code, data = RS.publish(domain, bucket) + code, data = Storage.publish(domain, bucket) code == StatusOK end def unpublish(domain) - code, data = RS.unpublish(domain) + code, data = Storage.unpublish(domain) code == StatusOK end def drop(bucket) - code, data = RS.drop(bucket) + code, data = Storage.drop(bucket) code == StatusOK end @@ -198,7 +196,7 @@ module Qiniu end def image_mogrify_save_as(bucket, key, source_image_url, options) - code, data = RS.image_mogrify_save_as(bucket, key, source_image_url, options) + code, data = Storage.image_mogrify_save_as(bucket, key, source_image_url, options) code == StatusOK ? data : false end diff --git a/lib/qiniu/io.rb b/lib/qiniu/io.rb deleted file mode 100755 index b8311b8..0000000 --- a/lib/qiniu/io.rb +++ /dev/null @@ -1,70 +0,0 @@ -# -*- encoding: utf-8 -*- - -require 'mime/types' -require 'digest/sha1' -require 'qiniu/exceptions' - -module Qiniu - module IO - class << self - include Utils - - def put_file(local_file, bucket, key = nil, mime_type = nil, custom_meta = nil, enable_crc32_check = false) - action_params = _generate_action_params(local_file, bucket, key, mime_type, custom_meta, enable_crc32_check) - url = Config.settings[:io_host] + action_params - options = {:content_type => 'application/octet-stream'} - 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, rotate = nil) - action_params = _generate_action_params(local_file, bucket, key, mime_type, custom_meta, enable_crc32_check, rotate) - 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 - - 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) - key = Digest::SHA1.hexdigest(local_file + Time.now.to_s) if key.nil? - entry_uri = bucket + ':' + key - if mime_type.nil? || mime_type.empty? - mime = MIME::Types.type_for local_file - mime_type = mime.empty? ? 'application/octet-stream' : mime[0].content_type - end - action_params = '/rs-put/' + Utils.urlsafe_base64_encode(entry_uri) + '/mimeType/' + Utils.urlsafe_base64_encode(mime_type) - action_params += '/meta/' + Utils.urlsafe_base64_encode(custom_meta) unless custom_meta.nil? - action_params += '/crc32/' + Utils.crc32checksum(local_file).to_s if enable_crc32_check - action_params += '/rotate/' + rotate if !rotate.nil? && rotate.to_i >= 0 - action_params - end - - end - end # module IO -end # module Qiniu diff --git a/lib/qiniu/rs.rb b/lib/qiniu/management.rb similarity index 85% rename from lib/qiniu/rs.rb rename to lib/qiniu/management.rb index 9ba9ff2..85d61ff 100755 --- a/lib/qiniu/rs.rb +++ b/lib/qiniu/management.rb @@ -1,21 +1,21 @@ # -*- encoding: utf-8 -*- module Qiniu - module RS + module Storage class << self include Utils def buckets Auth.request Config.settings[:rs_host] + '/buckets' - end + end # buckets def mkbucket(bucket_name) Auth.request Config.settings[:rs_host] + '/mkbucket/' + bucket_name - end + end # mkbucket def stat(bucket, key) Auth.request Config.settings[:rs_host] + '/stat/' + encode_entry_uri(bucket, key) - end + end # stat def get(bucket, key, save_as = nil, expires_in = nil, version = nil) url = Config.settings[:rs_host] + '/get/' + encode_entry_uri(bucket, key) @@ -23,35 +23,35 @@ module Qiniu url += '/attName/' + Utils.urlsafe_base64_encode(save_as) unless save_as.nil? url += '/expires/' + expires_in.to_s if !expires_in.nil? && expires_in > 0 Auth.request url - end + end # get def copy(source_bucket, source_key, target_bucket, target_key) uri = _generate_cp_or_mv_opstr('copy', source_bucket, source_key, target_bucket, target_key) Auth.request Config.settings[:rs_host] + uri - end + end # copy def move(source_bucket, source_key, target_bucket, target_key) uri = _generate_cp_or_mv_opstr('move', source_bucket, source_key, target_bucket, target_key) Auth.request Config.settings[:rs_host] + uri - end + end # move def delete(bucket, key) Auth.request Config.settings[:rs_host] + '/delete/' + encode_entry_uri(bucket, key) - end + end # delete def publish(domain, bucket) encoded_domain = Utils.urlsafe_base64_encode(domain) Auth.request Config.settings[:rs_host] + "/publish/#{encoded_domain}/from/#{bucket}" - end + end # publish def unpublish(domain) encoded_domain = Utils.urlsafe_base64_encode(domain) Auth.request Config.settings[:rs_host] + "/unpublish/#{encoded_domain}" - end + end # unpublish def drop(bucket) Auth.request Config.settings[:rs_host] + "/drop/#{bucket}" - end + end # drop def batch(command, bucket, keys) execs = [] @@ -60,39 +60,39 @@ module Qiniu execs << "op=/#{command}/#{encoded_uri}" end Auth.request Config.settings[:rs_host] + "/batch?" + execs.join("&") - end + end # batch def batch_get(bucket, keys) batch("get", bucket, keys) - end + end # batch_get def batch_stat(bucket, keys) batch("stat", bucket, keys) - end + end # batch_stat def batch_copy(*args) _batch_cp_or_mv('copy', args) - end + end # batch_copy def batch_move(*args) _batch_cp_or_mv('move', args) - end + end # batch_move def batch_delete(bucket, keys) batch("delete", bucket, keys) - end + end # batch_delete def save_as(bucket, key, source_url, op_params_string) encoded_uri = encode_entry_uri(bucket, key) save_as_string = '/save-as/' + encoded_uri new_url = source_url + '?' + op_params_string + save_as_string Auth.request new_url - end + end # save_as def image_mogrify_save_as(bucket, key, source_image_url, options) mogrify_params_string = Image.generate_mogrify_params_string(options) save_as(bucket, key, source_image_url, mogrify_params_string) - end + end # image_mogrify_save_as private @@ -100,18 +100,7 @@ module Qiniu source_encoded_entry_uri = encode_entry_uri(source_bucket, source_key) target_encoded_entry_uri = encode_entry_uri(target_bucket, target_key) %Q(/#{command}/#{source_encoded_entry_uri}/#{target_encoded_entry_uri}) - end - -=begin - 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("&") - end -=end - + end # _generate_cp_or_mv_opstr end - end # module RS + end # module Storage end # module Qiniu diff --git a/lib/qiniu/resumable_upload.rb b/lib/qiniu/resumable_upload.rb index 290458f..633fff6 100755 --- a/lib/qiniu/resumable_upload.rb +++ b/lib/qiniu/resumable_upload.rb @@ -8,7 +8,6 @@ require 'mime/types' require 'digest/sha1' require 'qiniu/abstract' require 'qiniu/exceptions' -require 'qiniu/io' module Qiniu module Storage diff --git a/lib/qiniu/storage.rb b/lib/qiniu/storage.rb index c10da81..cdae4b0 100755 --- a/lib/qiniu/storage.rb +++ b/lib/qiniu/storage.rb @@ -2,3 +2,4 @@ require 'qiniu/upload' require 'qiniu/resumable_upload' +require 'qiniu/management' diff --git a/lib/qiniu/version.rb b/lib/qiniu/version.rb index c6e5ea3..0196bfe 100755 --- a/lib/qiniu/version.rb +++ b/lib/qiniu/version.rb @@ -3,8 +3,8 @@ module Qiniu module Version MAJOR = 3 - MINOR = 4 - PATCH = 6 + MINOR = 5 + PATCH = 0 # Returns a version string by joining MAJOR, MINOR, and PATCH with '.' # # Example diff --git a/spec/qiniu/image_spec.rb b/spec/qiniu/image_spec.rb index d21d347..28b6bfb 100755 --- a/spec/qiniu/image_spec.rb +++ b/spec/qiniu/image_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' require 'qiniu/auth' -require 'qiniu/rs' +require 'qiniu' require 'qiniu/image' module Qiniu diff --git a/spec/qiniu/rs_spec.rb b/spec/qiniu/management_spec.rb similarity index 76% rename from spec/qiniu/rs_spec.rb rename to spec/qiniu/management_spec.rb index cc6bc31..a4a77b7 100755 --- a/spec/qiniu/rs_spec.rb +++ b/spec/qiniu/management_spec.rb @@ -3,33 +3,32 @@ require 'digest/sha1' require 'spec_helper' require 'qiniu/auth' -require 'qiniu/io' +require 'qiniu/management' require 'qiniu' module Qiniu - module RS - describe RS do + module Storage + describe Storage do before :all do @bucket = 'RubySdkTest' + (Time.now.to_i+rand(1000)).to_s @key = Digest::SHA1.hexdigest((Time.now.to_i+rand(100)).to_s) @key2 = @key + rand(100).to_s - #@domain = @bucket + '.dn.qbox.me' - code, data = Qiniu::RS.mkbucket(@bucket) + code, data = Storage.mkbucket(@bucket) puts [code, data].inspect code.should == 200 end after :all do - code, data = Qiniu::RS.drop(@bucket) + code, data = Storage.drop(@bucket) puts [code, data].inspect code.should == 200 end context ".put_file" do it "should works" do - code, data = Qiniu::IO.put_file(__FILE__, @bucket, @key, 'application/x-ruby', 'customMeta', true) + code, data = Storage.put_file(__FILE__, @bucket, @key, 'application/x-ruby', 'customMeta', true) code.should == 200 puts data.inspect end @@ -37,7 +36,7 @@ module Qiniu context ".buckets" do it "should works" do - code, data = Qiniu::RS.buckets + code, data = Storage.buckets code.should == 200 puts data.inspect end @@ -45,7 +44,7 @@ module Qiniu context ".stat" do it "should works" do - code, data = Qiniu::RS.stat(@bucket, @key) + code, data = Storage.stat(@bucket, @key) code.should == 200 puts data.inspect end @@ -53,7 +52,7 @@ module Qiniu context ".get" do it "should works" do - code, data = Qiniu::RS.get(@bucket, @key, "rs_spec.rb", 1) + code, data = Storage.get(@bucket, @key, "rs_spec.rb", 1) code.should == 200 puts data.inspect end @@ -61,7 +60,7 @@ module Qiniu context ".batch" do it "should works" do - code, data = Qiniu::RS.batch("stat", @bucket, [@key]) + code, data = Storage.batch("stat", @bucket, [@key]) code.should == 200 puts data.inspect end @@ -69,7 +68,7 @@ module Qiniu context ".batch_stat" do it "should works" do - code, data = Qiniu::RS.batch_stat(@bucket, [@key]) + code, data = Storage.batch_stat(@bucket, [@key]) code.should == 200 puts data.inspect end @@ -77,7 +76,7 @@ module Qiniu context ".batch_get" do it "should works" do - code, data = Qiniu::RS.batch_get(@bucket, [@key]) + code, data = Storage.batch_get(@bucket, [@key]) code.should == 200 puts data.inspect end @@ -133,15 +132,15 @@ module Qiniu context ".move" do it "should works" do - code, data = Qiniu::RS.move(@bucket, @key, @bucket, @key2) + code, data = Storage.move(@bucket, @key, @bucket, @key2) code.should == 200 puts data.inspect - code2, data2 = Qiniu::RS.stat(@bucket, @key2) + code2, data2 = Storage.stat(@bucket, @key2) code2.should == 200 puts data2.inspect - code3, data3 = Qiniu::RS.move(@bucket, @key2, @bucket, @key) + code3, data3 = Storage.move(@bucket, @key2, @bucket, @key) code3.should == 200 puts data3.inspect end @@ -149,7 +148,7 @@ module Qiniu context ".copy" do it "should works" do - code, data = Qiniu::RS.copy(@bucket, @key, @bucket, @key2) + code, data = Storage.copy(@bucket, @key, @bucket, @key2) code.should == 200 puts data.inspect @@ -161,7 +160,7 @@ module Qiniu context ".delete" do it "should works" do - code, data = Qiniu::RS.delete(@bucket, @key) + code, data = Storage.delete(@bucket, @key) code.should == 200 puts data.inspect end @@ -169,12 +168,12 @@ module Qiniu context ".drop" do it "should works" do - code, data = Qiniu::RS.drop(@bucket) + code, data = Storage.drop(@bucket) code.should == 200 puts data.inspect end end end - end -end + end # module Storage +end # module Qiniu diff --git a/spec/qiniu/qiniu_spec.rb b/spec/qiniu/qiniu_spec.rb index 5f39dfb..0c266b6 100755 --- a/spec/qiniu/qiniu_spec.rb +++ b/spec/qiniu/qiniu_spec.rb @@ -2,11 +2,11 @@ require 'digest/sha1' require 'spec_helper' -require 'qiniu/rs' +require 'qiniu' require 'qiniu/exceptions' module Qiniu - describe RS do + describe Qiniu do before :all do @bucket = 'RubySdkTest' + (Time.now.to_i+rand(1000)).to_s diff --git a/spec/qiniu/storage_spec.rb b/spec/qiniu/upload_spec.rb similarity index 91% rename from spec/qiniu/storage_spec.rb rename to spec/qiniu/upload_spec.rb index 412d109..4739c31 100755 --- a/spec/qiniu/storage_spec.rb +++ b/spec/qiniu/upload_spec.rb @@ -87,7 +87,7 @@ module Qiniu context ".stat" do it "should exists" do - code, data = Qiniu::RS.stat(@bucket, @key1) + code, data = Qiniu::Storage.stat(@bucket, @key1) puts data.inspect code.should == 200 end @@ -95,7 +95,7 @@ module Qiniu context ".delete" do it "should works" do - code, data = Qiniu::RS.delete(@bucket, @key1) + code, data = Qiniu::Storage.delete(@bucket, @key1) puts data.inspect code.should == 200 end @@ -113,7 +113,7 @@ module Qiniu context ".stat" do it "should exists" do - code, data = Qiniu::RS.stat(@bucket, @key2) + code, data = Qiniu::Storage.stat(@bucket, @key2) puts data.inspect code.should == 200 end @@ -121,7 +121,7 @@ module Qiniu context ".delete" do it "should works" do - code, data = Qiniu::RS.delete(@bucket, @key2) + code, data = Qiniu::Storage.delete(@bucket, @key2) puts data.inspect code.should == 200 end @@ -139,7 +139,7 @@ module Qiniu context ".stat" do it "should exists" do - code, data = Qiniu::RS.stat(@bucket, @key3) + code, data = Qiniu::Storage.stat(@bucket, @key3) puts data.inspect code.should == 200 end @@ -147,7 +147,7 @@ module Qiniu context ".delete" do it "should works" do - code, data = Qiniu::RS.delete(@bucket, @key3) + code, data = Qiniu::Storage.delete(@bucket, @key3) puts data.inspect code.should == 200 end @@ -165,7 +165,7 @@ module Qiniu context ".stat" do it "should exists" do - code, data = Qiniu::RS.stat(@bucket, @key4) + code, data = Qiniu::Storage.stat(@bucket, @key4) puts data.inspect code.should == 200 end @@ -173,7 +173,7 @@ module Qiniu context ".delete" do it "should works" do - code, data = Qiniu::RS.delete(@bucket, @key4) + code, data = Qiniu::Storage.delete(@bucket, @key4) puts data.inspect code.should == 200 end From ab81095c0cc9e943fefff6b00ea14b781a683836 Mon Sep 17 00:00:00 2001 From: Liang Tao Date: Thu, 6 Mar 2014 15:33:53 +0800 Subject: [PATCH 16/21] =?UTF-8?q?=E5=88=92=E5=88=86=E5=87=BAFop=E4=B8=8EMi?= =?UTF-8?q?sc=E5=91=BD=E5=90=8D=E7=A9=BA=E9=97=B4=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/qiniu.rb | 18 +++++++++--------- lib/qiniu/fop.rb | 3 +++ lib/qiniu/image.rb | 2 ++ lib/qiniu/management.rb | 2 +- lib/qiniu/{pub.rb => misc.rb} | 4 ++-- spec/qiniu/image_spec.rb | 14 +++++++------- spec/qiniu/{pub_spec.rb => misc_spec.rb} | 16 ++++++++-------- 7 files changed, 32 insertions(+), 27 deletions(-) create mode 100755 lib/qiniu/fop.rb rename lib/qiniu/{pub.rb => misc.rb} (96%) rename spec/qiniu/{pub_spec.rb => misc_spec.rb} (70%) diff --git a/lib/qiniu.rb b/lib/qiniu.rb index dd66f06..49c3658 100755 --- a/lib/qiniu.rb +++ b/lib/qiniu.rb @@ -7,14 +7,14 @@ module Qiniu autoload :Config, 'qiniu/config' autoload :Log, 'qiniu/log' autoload :Exception, 'qiniu/exceptions' - autoload :Pub, 'qiniu/pub' - autoload :Image, 'qiniu/image' 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/abstract' autoload :Storage, 'qiniu/storage' + autoload :Fop, 'qiniu/fop' + autoload :Misc, 'qiniu/misc' class << self @@ -35,22 +35,22 @@ module Qiniu end def set_protected(bucket, protected_mode) - code, data = Pub.set_protected(bucket, protected_mode) + code, data = Misc.set_protected(bucket, protected_mode) code == StatusOK end def set_separator(bucket, separator) - code, data = Pub.set_separator(bucket, separator) + code, data = Misc.set_separator(bucket, separator) code == StatusOK end def set_style(bucket, name, style) - code, data = Pub.set_style(bucket, name, style) + code, data = Misc.set_style(bucket, name, style) code == StatusOK end def unset_style(bucket, name) - code, data = Pub.unset_style(bucket, name) + code, data = Misc.unset_style(bucket, name) code == StatusOK end @@ -182,17 +182,17 @@ module Qiniu end def image_info(url) - code, data = Image.info(url) + code, data = Fop::Image.info(url) code == StatusOK ? data : false end def image_exif(url) - code, data = Image.exif(url) + code, data = Fop::Image.exif(url) code == StatusOK ? data : false end def image_mogrify_preview_url(source_image_url, options) - Image.mogrify_preview_url(source_image_url, options) + Fop::Image.mogrify_preview_url(source_image_url, options) end def image_mogrify_save_as(bucket, key, source_image_url, options) diff --git a/lib/qiniu/fop.rb b/lib/qiniu/fop.rb new file mode 100755 index 0000000..0abe87e --- /dev/null +++ b/lib/qiniu/fop.rb @@ -0,0 +1,3 @@ +# -*- encoding: utf-8 -*- + +require 'qiniu/image' diff --git a/lib/qiniu/image.rb b/lib/qiniu/image.rb index 0d7aa95..146dcc4 100755 --- a/lib/qiniu/image.rb +++ b/lib/qiniu/image.rb @@ -1,6 +1,7 @@ # -*- encoding: utf-8 -*- module Qiniu + module Fop module Image class << self include Utils @@ -33,4 +34,5 @@ module Qiniu end end # module Image + end # module Fop end # module Qiniu diff --git a/lib/qiniu/management.rb b/lib/qiniu/management.rb index 85d61ff..ce8cc97 100755 --- a/lib/qiniu/management.rb +++ b/lib/qiniu/management.rb @@ -90,7 +90,7 @@ module Qiniu end # save_as def image_mogrify_save_as(bucket, key, source_image_url, options) - mogrify_params_string = Image.generate_mogrify_params_string(options) + mogrify_params_string = Fop::Image.generate_mogrify_params_string(options) save_as(bucket, key, source_image_url, mogrify_params_string) end # image_mogrify_save_as diff --git a/lib/qiniu/pub.rb b/lib/qiniu/misc.rb similarity index 96% rename from lib/qiniu/pub.rb rename to lib/qiniu/misc.rb index b384d0c..d9d61b9 100755 --- a/lib/qiniu/pub.rb +++ b/lib/qiniu/misc.rb @@ -1,7 +1,7 @@ # -*- encoding: utf-8 -*- module Qiniu - module Pub + module Misc class << self include Utils @@ -30,6 +30,6 @@ module Qiniu end end - end # module Pub + end # module Misc end # module Qiniu diff --git a/spec/qiniu/image_spec.rb b/spec/qiniu/image_spec.rb index 28b6bfb..986c9c0 100755 --- a/spec/qiniu/image_spec.rb +++ b/spec/qiniu/image_spec.rb @@ -3,11 +3,11 @@ require 'spec_helper' require 'qiniu/auth' require 'qiniu' -require 'qiniu/image' +require 'qiniu/fop' module Qiniu - module Image - describe Image do + module Fop + describe Fop do before :all do @@ -60,7 +60,7 @@ module Qiniu context ".info" do it "should works" do - code, data = Qiniu::Image.info(@source_image_url) + code, data = Qiniu::Fop::Image.info(@source_image_url) code.should == 200 puts data.inspect end @@ -69,7 +69,7 @@ module Qiniu =begin context ".exif" do it "should works" do - code, data = Qiniu::Image.exif(@source_image_url) + code, data = Qiniu::Fop::Image.exif(@source_image_url) puts data.inspect end end @@ -77,11 +77,11 @@ module Qiniu context ".mogrify_preview_url" do it "should works" do - mogrify_preview_url = Qiniu::Image.mogrify_preview_url(@source_image_url, @mogrify_options) + mogrify_preview_url = Qiniu::Fop::Image.mogrify_preview_url(@source_image_url, @mogrify_options) puts mogrify_preview_url.inspect end end end - end # module Image + end # module Fop end # module Qiniu diff --git a/spec/qiniu/pub_spec.rb b/spec/qiniu/misc_spec.rb similarity index 70% rename from spec/qiniu/pub_spec.rb rename to spec/qiniu/misc_spec.rb index 03c2d60..f9fa2c5 100755 --- a/spec/qiniu/pub_spec.rb +++ b/spec/qiniu/misc_spec.rb @@ -3,11 +3,11 @@ require 'spec_helper' require 'qiniu/auth' require 'qiniu' -require 'qiniu/pub' +require 'qiniu/misc' module Qiniu - module Pub - describe Pub do + module Misc + describe Misc do before :all do @bucket = 'RubySdkTest' + (Time.now.to_i+rand(1000)).to_s @@ -24,7 +24,7 @@ module Qiniu context ".set_protected" do it "should works" do - code, data = Qiniu::Pub.set_protected(@bucket, 1) + code, data = Qiniu::Misc.set_protected(@bucket, 1) code.should == 200 puts data.inspect end @@ -32,7 +32,7 @@ module Qiniu context ".set_separator" do it "should works" do - code, data = Qiniu::Pub.set_separator(@bucket, "-") + code, data = Qiniu::Misc.set_separator(@bucket, "-") code.should == 200 puts data.inspect end @@ -40,7 +40,7 @@ module Qiniu context ".set_style" do it "should works" do - code, data = Qiniu::Pub.set_style(@bucket, "small.jpg", "imageMogr/auto-orient/thumbnail/!120x120r/gravity/center/crop/!120x120/quality/80") + code, data = Qiniu::Misc.set_style(@bucket, "small.jpg", "imageMogr/auto-orient/thumbnail/!120x120r/gravity/center/crop/!120x120/quality/80") code.should == 200 puts data.inspect end @@ -48,12 +48,12 @@ module Qiniu context ".unset_style" do it "should works" do - code, data = Qiniu::Pub.unset_style(@bucket, "small.jpg") + code, data = Qiniu::Misc.unset_style(@bucket, "small.jpg") code.should == 200 puts data.inspect end end end - end # module Pub + end # module Misc end # module Qiniu From d3ec623c3e9f346a6f32932a1abbc27abf3e1606 Mon Sep 17 00:00:00 2001 From: Liang Tao Date: Thu, 6 Mar 2014 15:40:29 +0800 Subject: [PATCH 17/21] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E4=BF=A1=E6=81=AF=EF=BC=8C=E4=BF=AE=E6=94=B9GemSpec=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Gemfile.lock | 4 ++-- lib/qiniu/version.rb | 6 +++--- qiniu-rs.gemspec => qiniu.gemspec | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) rename qiniu-rs.gemspec => qiniu.gemspec (92%) diff --git a/Gemfile.lock b/Gemfile.lock index b952fb1..dddcc76 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - qiniu-rs (3.5.0) + qiniu (6.0.1) json (~> 1.7) mime-types (~> 1.19) rest-client (~> 1.6) @@ -32,6 +32,6 @@ PLATFORMS DEPENDENCIES fakeweb (~> 1.3) - qiniu-rs! + qiniu! rake (>= 0.9) rspec (>= 2.11) diff --git a/lib/qiniu/version.rb b/lib/qiniu/version.rb index 0196bfe..8d779d8 100755 --- a/lib/qiniu/version.rb +++ b/lib/qiniu/version.rb @@ -2,9 +2,9 @@ module Qiniu module Version - MAJOR = 3 - MINOR = 5 - PATCH = 0 + MAJOR = 6 + MINOR = 0 + PATCH = 1 # Returns a version string by joining MAJOR, MINOR, and PATCH with '.' # # Example diff --git a/qiniu-rs.gemspec b/qiniu.gemspec similarity index 92% rename from qiniu-rs.gemspec rename to qiniu.gemspec index 7558e5a..6e5e0a5 100755 --- a/qiniu-rs.gemspec +++ b/qiniu.gemspec @@ -5,14 +5,14 @@ require File.expand_path('../lib/qiniu/version', __FILE__) Gem::Specification.new do |gem| gem.authors = ["why404"] gem.email = ["why404@gmail.com"] - gem.description = %q{Qiniu Resource (Cloud) Storage SDK for Ruby. See: http://docs.qiniu.com/ruby-sdk/} + gem.description = %q{Qiniu Resource (Cloud) Storage SDK for Ruby. See: http://developer.qiniu.com/docs/v6/sdk/ruby-sdk.html} gem.summary = %q{Qiniu Resource (Cloud) Storage SDK for Ruby} gem.homepage = "https://github.com/qiniu/ruby-sdk" gem.files = `git ls-files`.split($\) gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) } gem.test_files = gem.files.grep(%r{^(test|spec|features)/}) - gem.name = "qiniu-rs" + gem.name = "qiniu" gem.require_paths = ["lib"] gem.version = Qiniu::Version.to_s From 8f5c6c0b0a6ffa492f80eb9b92ec6876bb8fb76d Mon Sep 17 00:00:00 2001 From: Liang Tao Date: Thu, 6 Mar 2014 15:49:31 +0800 Subject: [PATCH 18/21] =?UTF-8?q?=E4=BF=AE=E6=AD=A3README=E4=B8=8ECHANGELO?= =?UTF-8?q?G=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 12 ++++++++---- README.md | 10 +++++----- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 50f0cd2..5568daa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ ## CHANGE LOG +### v6.0.1 + +- 重新划分命名空间,存储相关归入Qiniu::Storage,数据处理相关归入Qiniu::Fop,杂项相关归入Qiniu::Misc。 + ### v3.4.6 - `Qiniu::RS.generate_download_token()` 方法的 `:pattern` 调整为必选项。 @@ -7,13 +11,13 @@ ### v3.4.5 - `Qiniu::RS.generate_upload_token()` 方法新增 `:callback_body` 和 `return_url` 选项。 -- 选项含义参考: +- 选项含义参考: ### v3.4.2 - `Qiniu::RS.generate_upload_token()` 方法新增 `:return_body` 选项。 -该选项(`:return_body`)可设置文件上传成功后,执行七牛云存储规定的回调API,并以 JSON 响应格式返回其执行结果。参考 [uploadToken 之 returnBody 说明](http://docs.qiniu.com/api/v6/put.html#uploadToken-returnBody)。 +该选项(`:return_body`)可设置文件上传成功后,执行七牛云存储规定的回调API,并以 JSON 响应格式返回其执行结果。参考 [uploadToken 之 returnBody 说明](http://developer.qiniu.com/docs/v6/api/reference/security/put-policy.html#put-policy-return-body)。 ### v3.4.1 @@ -44,9 +48,9 @@ Ruby 宿主环境如下 ### v3.3.0 -私有资源下载新版实现,添加 Qiniu::RS.generate_download_token() 方法。参考 [downloadToken](http://docs.qiniutek.com/v3/api/io/#get) +私有资源下载新版实现,添加 Qiniu::RS.generate_download_token() 方法。参考 [downloadToken](http://developer.qiniu.com/docs/v6/api/reference/security/download-token.html) -详见 [Ruby SDK 使用文档之私有资源下载](http://docs.qiniutek.com/v3/sdk/ruby/#download-private-files) +详见 [Ruby SDK 使用文档之私有资源下载](http://developer.qiniu.com/docs/v6/api/overview/dn/security.html) ### v3.2.2 diff --git a/README.md b/README.md index 16ae209..787b85d 100755 --- a/README.md +++ b/README.md @@ -2,13 +2,13 @@ # 关于 -此 Ruby SDK 适用于 Ruby 1.8.x, 1.9.x, jruby, rbx, ree 版本,基于 [七牛云存储官方API](http://docs.qiniutek.com/v3/api/) 构建。使用此 SDK 构建您的网络应用程序,能让您以非常便捷地方式将数据安全地存储到七牛云存储上。无论您的网络应用是一个网站程序,还是包括从云端(服务端程序)到终端(手持设备应用)的架构的服务或应用,通过七牛云存储及其 SDK,都能让您应用程序的终端用户高速上传和下载,同时也让您的服务端更加轻盈。 +此 Ruby SDK 适用于 Ruby 1.8.x, 1.9.x, jruby, rbx, ree 版本,基于 [七牛云存储官方API](http://developer.qiniu.com/docs/v6/index.html) 构建。使用此 SDK 构建您的网络应用程序,能让您以非常便捷地方式将数据安全地存储到七牛云存储上。无论您的网络应用是一个网站程序,还是包括从云端(服务端程序)到终端(手持设备应用)的架构的服务或应用,通过七牛云存储及其 SDK,都能让您应用程序的终端用户高速上传和下载,同时也让您的服务端更加轻盈。 ## 安装 在您 Ruby 应用程序的 `Gemfile` 文件中,添加如下一行代码: - gem 'qiniu-rs' + gem 'qiniu' 然后,在应用程序所在的目录下,可以运行 `bundle` 安装依赖包: @@ -16,11 +16,11 @@ 或者,可以使用 Ruby 的包管理器 `gem` 进行安装: - $ gem install qiniu-rs + $ gem install qiniu ## 使用 -参考文档:[七牛云存储 Ruby SDK 使用指南](http://docs.qiniutek.com/v3/sdk/ruby/) +参考文档:[七牛云存储 Ruby SDK 使用指南](http://developer.qiniu.com/docs/v6/sdk/ruby-sdk.html) ## 贡献代码 @@ -32,7 +32,7 @@ ## 许可证 -Copyright (c) 2012-2013 qiniutek.com +Copyright (c) 2012-2014 qiniu.com 基于 MIT 协议发布: From ae0dbced1b0bc4a1858c1135471692e5852c23f5 Mon Sep 17 00:00:00 2001 From: Liang Tao Date: Mon, 10 Mar 2014 12:21:15 +0800 Subject: [PATCH 19/21] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E6=8E=88=E6=9D=83?= =?UTF-8?q?=E5=87=AD=E8=AF=81=E7=AE=97=E6=B3=95=E5=92=8C=E7=9B=B8=E5=85=B3?= =?UTF-8?q?=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 | 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 From cd66ce79ff63a5ec59222d5a9ab8dc02276974d4 Mon Sep 17 00:00:00 2001 From: Liang Tao Date: Mon, 10 Mar 2014 13:38:30 +0800 Subject: [PATCH 20/21] =?UTF-8?q?=E5=88=A0=E9=99=A4rbx=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E9=A1=B9=E7=9B=AE=EF=BC=8C=E6=B7=BB=E5=8A=A02.0.0=E5=92=8C2.1.?= =?UTF-8?q?1=E6=B5=8B=E8=AF=95=E9=A1=B9=E7=9B=AE=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .travis.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index c4dbe7b..c7863ad 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,10 +3,10 @@ rvm: - 1.8.7 - 1.9.2 - 1.9.3 + - 2.0.0 + - 2.1.1 - jruby-18mode - jruby-19mode - - rbx-18mode - - rbx-19mode - jruby-head - ree before_script: From 1a72e1055b8f801b1bac52b63bbde0b63ccbec14 Mon Sep 17 00:00:00 2001 From: Liang Tao Date: Thu, 13 Mar 2014 17:27:17 +0800 Subject: [PATCH 21/21] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=8A=B6=E6=80=81Badge?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 787b85d..4ce900e 100755 --- a/README.md +++ b/README.md @@ -1,8 +1,13 @@ # Qiniu Resource (Cloud) Storage SDK for Ruby -# 关于 +## 关于 -此 Ruby SDK 适用于 Ruby 1.8.x, 1.9.x, jruby, rbx, ree 版本,基于 [七牛云存储官方API](http://developer.qiniu.com/docs/v6/index.html) 构建。使用此 SDK 构建您的网络应用程序,能让您以非常便捷地方式将数据安全地存储到七牛云存储上。无论您的网络应用是一个网站程序,还是包括从云端(服务端程序)到终端(手持设备应用)的架构的服务或应用,通过七牛云存储及其 SDK,都能让您应用程序的终端用户高速上传和下载,同时也让您的服务端更加轻盈。 +此 Ruby SDK 适用于 Ruby 1.8.x, 1.9.x, 2.0.x, jruby, rbx, ree 版本,基于 [七牛云存储官方API](http://developer.qiniu.com/docs/v6/index.html) 构建。使用此 SDK 构建您的网络应用程序,能让您以非常便捷地方式将数据安全地存储到七牛云存储上。无论您的网络应用是一个网站程序,还是包括从云端(服务端程序)到终端(手持设备应用)的架构的服务或应用,通过七牛云存储及其 SDK,都能让您应用程序的终端用户高速上传和下载,同时也让您的服务端更加轻盈。 + +## 状态 + +集成测试:[![Build Status](https://api.travis-ci.org/qiniu/ruby-sdk.png?branch=master)](https://travis-ci.org/qiniu/ruby-sdk) +版本:[![Gem Version](https://badge.fury.io/rb/qiniu-rs.png)](http://badge.fury.io/rb/qiniu-rs) ## 安装