From 9a5a27429debe757e110ee664f81ca67596b7fde Mon Sep 17 00:00:00 2001 From: 404 Date: Mon, 8 Oct 2012 12:59:30 +0800 Subject: [PATCH] add resumable upload and all tests(63 examples) passed --- docs/README.md | 43 ++++++++++++++++++++++++++++++++++++++++++- lib/qiniu/rs.rb | 2 ++ spec/qiniu/rs_spec.rb | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+), 1 deletion(-) diff --git a/docs/README.md b/docs/README.md index 3768955..00f1e61 100644 --- a/docs/README.md +++ b/docs/README.md @@ -17,6 +17,7 @@ title: Ruby SDK 使用指南 | 七牛云存储 - [上传文件](#upload) - [获取用于上传文件的临时授权凭证](#generate-upload-token) - [服务端上传文件](#upload-server-side) + - [断点续上传](#resumable-upload) - [针对 NotFound 场景处理](#upload-file-for-not-found) - [客户端直传文件](#upload-client-side) - [查看文件属性信息](#stat) @@ -185,10 +186,50 @@ title: Ruby SDK 使用指南 | 七牛云存储 **返回值** -上传成功,返回如下一个 Hash,否则返回 `false`: +上传成功,返回如下一个 Hash: {"hash"=>"FgHk-_iqpnZji6PsNr4ghsK5qEwR"} +上传失败,会抛出 `UploadFailedError` 异常。 + + + +##### 断点续上传 + +无需任何额外改动,SDK 提供的 `Qiniu::RS.upload_file()` 方法缺省支持断点续上传。默认情况下,SDK 会自动启用断点续上传的方式来上传超过 4MB 大小的文件。您也可以在 [应用接入](/v3/sdk/ruby/#establish_connection!) 时通过修改缺省配置来设置该阀值: + + Qiniu::RS.establish_connection! :access_key => YOUR_APP_ACCESS_KEY, + :secret_key => YOUR_APP_SECRET_KEY, + :block_size => 1024*1024*4, + :chunk_size => 1024*256, + :tmpdir => Dir.tmpdir + File::SEPARATOR + 'Qiniu-RS-Ruby-SDK', + :enable_debug => true, + :auto_reconnect => true, + :max_retry_times => 3 + +**参数详解** + +应用接入初始化时,以下配置参数均为可选: + +:block_size +: 整型,指定断点续上传针对大文件所使用的分块大小,缺省为 4MB ,小于该阀值的文件不启用断点续上传。 + +:chunk_size +: 整型,指定断点续上传每次http请求上传的数据块大小,缺省为 256KB。该设置尽量不要超过实际使用的上行带宽,且不能超过 `:block_size` 定义的值。 + +:tmpdir +: 字符串类型,指定持久化保存断点续上传进度状态临时文件的目录,缺省放置于操作系统的临时目录中。 + +:enable_debug +: 布尔值,是否启用调试模式,缺省启用(true),启用后会打印相关日志。该参数 SDK 全局有效。 + +:auto_reconnect +: 布尔值,指定每次 http 若请求失败是否启用重试,缺省启用(true)。该参数 SDK 全局有效。 + +:max_retry_times +: 整型,指定每次 http 若请求失败最多可以重试的次数,缺省为3次。该参数 SDK 全局有效。 + + ##### 针对 NotFound 场景处理 diff --git a/lib/qiniu/rs.rb b/lib/qiniu/rs.rb index 1788afc..f826a44 100755 --- a/lib/qiniu/rs.rb +++ b/lib/qiniu/rs.rb @@ -9,6 +9,7 @@ module Qiniu 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' @@ -104,6 +105,7 @@ module Qiniu end 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], diff --git a/spec/qiniu/rs_spec.rb b/spec/qiniu/rs_spec.rb index 8f2a32a..8204b18 100755 --- a/spec/qiniu/rs_spec.rb +++ b/spec/qiniu/rs_spec.rb @@ -1,5 +1,6 @@ # -*- encoding: utf-8 -*- +require 'digest/sha1' require 'spec_helper' require 'qiniu/rs' @@ -129,6 +130,41 @@ module Qiniu 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(Random.rand(9).to_s)}} + key = Digest::SHA1.hexdigest(localfile+Time.now.to_s) + # generate the upload token + uptoken_opts = {:scope => @bucket, :expires_in => 3600, :customer => "awhy.xu@gmail.com"} + 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 + end + end + context ".stat" do it "should works" do result = Qiniu::RS.stat(@bucket, @key)