From f1cb65d79a34a14c12cf8c0535ca820d7bb506a3 Mon Sep 17 00:00:00 2001 From: Liang Tao Date: Mon, 3 Mar 2014 17:17:42 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E6=89=80=E6=9C=89=E7=9A=84?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E7=94=A8=E4=BE=8B=EF=BC=8C=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E6=96=B0=E7=BB=84=E7=BB=87=E5=A5=BD=E7=9A=84=E5=87=BD=E6=95=B0?= =?UTF-8?q?=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