From d9422cbcf4c1c7ca120427222e8212a16f7b597e Mon Sep 17 00:00:00 2001 From: 404 Date: Wed, 9 Jan 2013 01:56:05 +0800 Subject: [PATCH] added copy and move methods --- CHANGELOG.md | 10 +++- Gemfile.lock | 2 +- LICENSE | 2 +- docs/README.md | 108 +++++++++++++++++++++++++++++++++++---- lib/qiniu/rs.rb | 22 ++++++++ lib/qiniu/rs/rs.rb | 34 ++++++++++++ lib/qiniu/rs/version.rb | 4 +- spec/qiniu/rs/rs_spec.rb | 57 +++++++++++++++++++++ spec/qiniu/rs_spec.rb | 47 +++++++++++++++++ 9 files changed, 272 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c4843c0..7d77227 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,15 @@ ## CHANGE LOG -### v3.3.1 +### v3.4.0 +增加文件复制/移动方法,包括批量复制/移动文件 + +- `Qiniu::RS.copy(source_bucket, source_key, target_bucket, target_key)` +- `Qiniu::RS.move(source_bucket, source_key, target_bucket, target_key)` +- `Qiniu::RS.batch_copy [source_bucket, source_key, target_bucket, target_key], ...` +- `Qiniu::RS.batch_move [source_bucket, source_key, target_bucket, target_key], ...` + +### v3.3.1 确保单元测试里边用到的测试 Bucket 全局唯一 diff --git a/Gemfile.lock b/Gemfile.lock index f88b6d9..f1b4455 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - qiniu-rs (3.3.1) + qiniu-rs (3.4.0) json (~> 1.7) mime-types (~> 1.19) rest-client (~> 1.6) diff --git a/LICENSE b/LICENSE index ef215d7..f28ae42 100755 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,4 @@ -Copyright (c) 2012 why404 +Copyright (c) 2012-2013 qiniutek.com MIT License diff --git a/docs/README.md b/docs/README.md index 8862c2b..7fa8af2 100644 --- a/docs/README.md +++ b/docs/README.md @@ -6,7 +6,7 @@ title: Ruby SDK 使用指南 | 七牛云存储 此 Ruby SDK 适用于 Ruby 1.8.x, 1.9.x, jruby, rbx, ree 版本,基于 [七牛云存储官方API](/v3/api/) 构建。使用此 SDK 构建您的网络应用程序,能让您以非常便捷地方式将数据安全地存储到七牛云存储上。无论您的网络应用是一个网站程序,还是包括从云端(服务端程序)到终端(手持设备应用)的架构的服务或应用,通过七牛云存储及其 SDK,都能让您应用程序的终端用户高速上传和下载,同时也让您的服务端更加轻盈。 -七牛云存储 Ruby SDK 源码地址: +七牛云存储 Ruby SDK 源码地址: [![Build Status](https://api.travis-ci.org/qiniu/ruby-sdk.png?branch=master)](https://travis-ci.org/qiniu/ruby-sdk) [![Dependency Status](https://gemnasium.com/why404/qiniu-rs-for-ruby.png)](https://gemnasium.com/why404/qiniu-rs-for-ruby) **目录** @@ -186,10 +186,10 @@ title: Ruby SDK 使用指南 | 七牛云存储 : 必须,字符串类型(String),本地文件可被读取的有效路径 :bucket -: 必须,字符串类型(String),类似传统数据库里边的表名称,我们暂且将其叫做“资源表”,指定将该数据属性信息存储到具体的资源表中 。 +: 必须,字符串类型(String),空间名称。 :key -: 必须,字符串类型(String),类似传统数据库里边某个表的主键ID,给每一个文件一个UUID用于进行标示。 +: 必须,字符串类型(String),若把 Bucket 理解为关系性数据库的某个表,那么 key 类似数据库里边某个表的主键ID,需给每一个文件一个UUID用于进行标示。 :mime_type : 可选,字符串类型(String),文件的 mime-type 值。如若不传入,SDK 会自行计算得出,若计算失败缺省使用 application/octet-stream 代替之。 @@ -364,10 +364,10 @@ pattern **参数** bucket -: 必须,字符串类型(String),类似传统数据库里边的表名称,我们暂且将其叫做“资源表”,每份数据是属性信息都存储到具体的 bucket(资源表)中 。 +: 必须,字符串类型(String),空间名称。 key -: 必须,字符串类型(String),类似传统数据库里边某个表的主键ID,每一个文件最终都用一个唯一 `key` 进行标示。 +: 必须,字符串类型(String),若把 Bucket 理解为关系性数据库的某个表,那么 key 类似数据库里边某个表的主键ID,需给每一个文件一个UUID用于进行标示。 **返回值** @@ -392,6 +392,61 @@ mimeType putTime : 上传时间,单位是 百纳秒 + + + +#### 复制单个文件 + + Qiniu::RS.copy(source_bucket, source_key, target_bucket, target_key) + +可以通过 SDK 提供的 `Qiniu::RS.copy` 函数进行文件复制操作。 + +**参数** + +source_bucket +: 必须,字符串类型(String),指定源空间。 + +source_key +: 必须,字符串类型(String),指定源文件。 + +target_bucket +: 必须,字符串类型(String),指定要复制到的目标空间。 + +target_key +: 必须,字符串类型(String),指定要复制到目标空间的目标文件名。 + +**返回值** + +如果请求失败,返回 `false`;否则返回 `true` 。 + + + + +#### 移动单个文件 + + Qiniu::RS.move(source_bucket, source_key, target_bucket, target_key) + +可以通过 SDK 提供的 `Qiniu::RS.move` 函数进行文件移动操作。 + +**参数** + +source_bucket +: 必须,字符串类型(String),指定源空间。 + +source_key +: 必须,字符串类型(String),指定源文件。 + +target_bucket +: 必须,字符串类型(String),指定要移动到的目标空间。 + +target_key +: 必须,字符串类型(String),指定要移动到目标空间的目标文件名。 + +**返回值** + +如果请求失败,返回 `false`;否则返回 `true` 。 + + ### 删除单个文件 @@ -403,10 +458,10 @@ putTime **参数** bucket -: 必须,字符串类型(String),类似传统数据库里边的表名称,我们暂且将其叫做“资源表”,每份数据是属性信息都存储到具体的 bucket(资源表)中 。 +: 必须,字符串类型(String),空间名称。 key -: 必须,字符串类型(String),类似传统数据库里边某个表的主键ID,每一个文件最终都用一个唯一 `key` 进行标示。 +: 必须,字符串类型(String),若把 Bucket 理解为关系性数据库的某个表,那么 key 类似数据库里边某个表的主键ID,需给每一个文件一个UUID用于进行标示。 **返回值** @@ -427,7 +482,7 @@ command : 必须,字符串类型(String),其值可以是 `stat`, `get`, `delete` 中的一种 bucket -: 必须,字符串类型(String),类似传统数据库里边的表名称,我们暂且将其叫做“资源表”,每份数据是属性信息都存储到具体的 bucket(资源表)中 。 +: 必须,字符串类型(String),空间名称。 keys : 必须,数组类型(Array),所要操作 `key` 的集合。 @@ -461,7 +516,7 @@ keys **参数** bucket -: 必须,字符串类型(String),类似传统数据库里边的表名称,我们暂且将其叫做“资源表”,每份数据是属性信息都存储到具体的 bucket(资源表)中 。 +: 必须,字符串类型(String),空间名称。 keys : 必须,数组类型(Array),所要操作 `key` 的集合。 @@ -484,6 +539,41 @@ keys ... ] + + + +#### 批量复制文件 + + Qiniu::RS.batch_copy(*args) + +等价于, + + Qiniu::RS.batch_copy [source_bucket, source_key, target_bucket, target_key], ... + +`Qiniu::RS.batch_copy()` 的参数是一个可变长数组,其中每个数组其元素含义同 `Qiniu::RS.copy` 的参数一样。 + +**返回值** + +如果批量删除成功,返回 `true` ,否则为 `false` 。 + + + + +#### 批量移动文件 + + Qiniu::RS.batch_move(*args) + +等价于, + + Qiniu::RS.batch_move [source_bucket, source_key, target_bucket, target_key], ... + +`Qiniu::RS.batch_move()` 的参数是一个可变长数组,其中每个数组其元素含义同 `Qiniu::RS.move` 的参数一样。 + +**返回值** + +如果批量删除成功,返回 `true` ,否则为 `false` 。 + + #### 批量删除文件 diff --git a/lib/qiniu/rs.rb b/lib/qiniu/rs.rb index fd58d19..dd744fa 100755 --- a/lib/qiniu/rs.rb +++ b/lib/qiniu/rs.rb @@ -63,6 +63,7 @@ module Qiniu code == StatusOK end +=begin def set_watermark(customer_id, options = {}) code, data = EU.set_watermark(customer_id, options) code == StatusOK @@ -72,6 +73,7 @@ module Qiniu code, data = EU.get_watermark(customer_id) code == StatusOK ? data : false end +=end def put_auth(expires_in = nil, callback_url = nil) code, data = IO.put_auth(expires_in, callback_url) @@ -149,6 +151,16 @@ module Qiniu 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 @@ -169,6 +181,16 @@ module Qiniu 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 diff --git a/lib/qiniu/rs/rs.rb b/lib/qiniu/rs/rs.rb index 3e4903d..79bbd1e 100755 --- a/lib/qiniu/rs/rs.rb +++ b/lib/qiniu/rs/rs.rb @@ -26,6 +26,16 @@ module Qiniu 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 @@ -61,6 +71,14 @@ module Qiniu 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 @@ -77,6 +95,22 @@ module Qiniu 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 + + 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 diff --git a/lib/qiniu/rs/version.rb b/lib/qiniu/rs/version.rb index 0ddef51..af38870 100755 --- a/lib/qiniu/rs/version.rb +++ b/lib/qiniu/rs/version.rb @@ -4,8 +4,8 @@ module Qiniu module RS module Version MAJOR = 3 - MINOR = 3 - PATCH = 1 + MINOR = 4 + PATCH = 0 # Returns a version string by joining MAJOR, MINOR, and PATCH with '.' # # Example diff --git a/spec/qiniu/rs/rs_spec.rb b/spec/qiniu/rs/rs_spec.rb index 123c990..4ae3cad 100755 --- a/spec/qiniu/rs/rs_spec.rb +++ b/spec/qiniu/rs/rs_spec.rb @@ -13,6 +13,7 @@ module Qiniu 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) @@ -89,6 +90,34 @@ module Qiniu end end + 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 + =begin context ".publish" do it "should works" do @@ -107,6 +136,34 @@ module Qiniu 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 ".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 ".delete" do it "should works" do code, data = Qiniu::RS::RS.delete(@bucket, @key) diff --git a/spec/qiniu/rs_spec.rb b/spec/qiniu/rs_spec.rb index d6fdab1..6242c89 100755 --- a/spec/qiniu/rs_spec.rb +++ b/spec/qiniu/rs_spec.rb @@ -11,6 +11,7 @@ module Qiniu 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::RS.mkbucket(@bucket) @@ -295,6 +296,52 @@ module Qiniu end =end + context ".batch_copy" do + it "should works" do + result = Qiniu::RS.batch_copy [@bucket, @key, @bucket, @key2] + result.should_not be_false + + #result2 = Qiniu::RS.stat(@bucket, @key2) + #result2.should_not be_false + 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 + + #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 + 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 + 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 + end + end + context ".delete" do it "should works" do result = Qiniu::RS.delete(@bucket, @key)