Compare commits

..
71 Commits
Author SHA1 Message Date
404 f85ee1159b Merge pull request #54 from qiniu/release/v3.4.3
callbackBody + returnUrl supported
2013-07-23 10:52:29 -07:00
why404 804c7609ef returnUrl 2013-07-24 01:47:50 +08:00
404 49ab640493 Merge pull request #53 from GaiaMagic/master
Support callback_body option
2013-07-23 10:37:48 -07:00
HungYuHei 85f8e68399 Support callback_body option 2013-07-22 23:18:54 +08:00
LI Daobing 7dbec75553 Merge pull request #50 from qiniu/develop
merge develop to master
2013-07-19 02:26:59 -07:00
LI Daobing 3299d2bd02 Merge remote-tracking branch 'origin/master' into develop
Conflicts:
	docs/README.md
2013-07-19 15:59:42 +08:00
LI Daobing ec8c50383b disable ruby-head on travis 2013-07-19 15:54:30 +08:00
LI Daobing 9cc157c21d Merge pull request #51 from longshanksmo/fix/test_account
use new account & correct test code
2013-07-18 22:33:25 -07:00
longshanksmo 3d40d4e967 use new account & correct test code 2013-07-19 13:23:45 +08:00
hantuo fa85e838fc Merge pull request #49 from longshanksmo/fix/lost_link
Fix/lost link
2013-07-17 22:02:33 -07:00
longshanksmo 38970b66e3 Update README.md 2013-07-17 20:33:46 +08:00
longshanksmo 63ccf3f2dc Update README.md
correct the title
2013-07-17 20:32:32 +08:00
longshanksmo 5c011aa816 fix links 2013-07-17 20:29:56 +08:00
xushiwei 7b507f15c6 Merge pull request #41 from why404/release/v3.4.2
Release/v3.4.2 - uploadToken[:return_body]
2013-02-05 03:45:03 -08:00
404 aab7277b50 update README.md 2013-02-05 14:59:26 +08:00
404 eaed72eeaa release v3.4.2 for uploadToken[:return_body] #40 . 2013-02-04 15:38:11 +08:00
404 60cc7274b1 Merge pull request #38 from why404/feature/fix_env_appkey
rm encrypted appkey
2013-01-24 18:38:37 -08:00
xushiwei 466a4ba07e Merge pull request #39 from why404/feature/return_body
add :return_body option for Qiniu::RS.generate_upload_token()
2013-01-23 06:23:49 -08:00
404 444956f161 add :return_body option for Qiniu::RS.generate_upload_token() 2013-01-23 21:30:15 +08:00
404 0e4b5d00bc used before_script instead of env vars in .travis.yml 2013-01-23 16:55:41 +08:00
404 5d2745d148 fix travis env syntax 2013-01-23 16:49:15 +08:00
404 3026a40d99 rm encrypted appkey 2013-01-23 16:38:42 +08:00
xushiwei 84f7cc6205 Merge pull request #36 from why404/feature/encrypt_appkey
mv appkey outside and encrypted
2013-01-22 23:58:50 -08:00
404 5562df4b28 mv appkey outside and encrypted 2013-01-23 15:37:13 +08:00
qiniu 70c4664e0d Merge pull request #35 from qiniu/master
v3.4.1 - add :async_options for uploadToken
2013-01-14 17:58:45 -08:00
404 2096bff7a7 Merge pull request #34 from why404/feature/upload_async_options
v3.4.1 - add :async_options for uploadToken
2013-01-14 09:20:44 -08:00
404 b0af3c1814 v3.4.1 - add :async_options for uploadToken 2013-01-15 00:10:19 +08:00
404 9bda6845a0 Merge pull request #33 from why404/feature/add_cp_and_mv_support
v3.4.0 - added copy and move methods
2013-01-08 09:59:13 -08:00
404 d9422cbcf4 added copy and move methods 2013-01-09 01:56:05 +08:00
404 38c054f24c Merge pull request #32 from why404/feature/uniq_bucket_for_multi_test_concurrency
using unique bucket for multi concurrency testing
2013-01-06 07:48:34 -08:00
404 f85c4fab41 updated CHANGELOG.md for v3.3.1 2013-01-06 23:39:28 +08:00
404 f9bbcfeec6 using unique bucket for multi concurrency testing 2013-01-06 23:29:23 +08:00
xushiwei e3d4a80b54 Merge pull request #31 from why404/feature/download_token
Feature/download token
2013-01-05 23:53:54 -08:00
404 61aae133cf updated Gemfile.lock 2013-01-06 15:20:50 +08:00
404 c3e2a3d44b rewrite docs 2013-01-06 14:48:35 +08:00
404 06b019008a fix encoding comments 2013-01-06 11:07:12 +08:00
404 957b5d1499 docs updated 2012-12-20 17:16:17 +08:00
404 5b0a561cc1 add Qiniu::RS.generate_download_token() 2012-12-19 17:53:26 +08:00
404 d1e60697c3 disabled wm 2012-12-18 10:57:16 +08:00
404 089b6bc7c6 Merge pull request #29 from zhouguangming/bug_fixed
refactor UP.upload_with_token
2012-12-09 17:53:22 -08:00
zhouguangming f1edde2e54 refactor UP.upload_with_token 2012-12-08 22:15:01 +08:00
404 8aca8bf33e Merge pull request #28 from zhouguangming/bug_fixed
MissingArgsError should get a key_list // very nice!
2012-12-07 00:07:43 -08:00
zhouguangming 69bb9cb913 MissingArgsError should get a key_list 2012-12-07 15:25:54 +08:00
404 cacdb4f926 Merge pull request #27 from zhouguangming/bug_fixed
感谢修正!不过由于历史遗留原因 lib/qiniu/tokens/qbox_token.rb 尚保留在sdk代码里边但并未实际使用。请求签名认证程序使用的是  
Qiniu::RS::Utils.generate_qbox_signature(url,params) , 所以不影响sdk正常使用。
2012-12-06 05:41:46 -08:00
zhouguangming 557b75f33f [bug_fixed] lib/qiniu/tokens/qbox_token.rb => method generate_token => undefined method signature 2012-12-06 17:19:14 +08:00
xushiwei 2cbb175423 Merge pull request #26 from why404/release/v3.2.2
fix up 701
2012-12-04 22:32:53 -08:00
404 a8a8f63887 fix up 701 2012-12-05 14:00:12 +08:00
404 64cf9b5d3f Merge pull request #24 from hayeah/master
Fixes Qiniu::RS::Exception Issue
2012-11-29 01:53:46 -08:00
Howard Yeh 1a73c39ffb Don't override to_s of Exception with inspect.
This causes infinite recursion.
2012-11-27 18:52:41 +08:00
xushiwei 864076b9ec Merge pull request #21 from why404/release/v3.2.0
Release/v3.2.0 - allow upload calback api
2012-11-08 03:01:27 -08:00
404 bb6efd4e0d upload auto-rorate supported 2012-11-07 17:28:48 +08:00
404 ef1739cb2a v3.2.0 2012-11-06 23:19:14 +08:00
404 611837b137 Merge branch 'feature/allow_upload_callback_api' into develop 2012-11-06 23:17:16 +08:00
404 000eef8870 update unit tests 2012-11-06 23:16:40 +08:00
404 bacf83dd3c allow upload callback api 2012-11-06 22:09:19 +08:00
xushiwei b8ad8bd7a1 Merge pull request #20 from why404/develop
v3.1.0 - resumable upload support
2012-10-15 08:51:52 -07:00
404 38a0877b66 Merge branch 'release/v3.1.0' 2012-10-08 13:04:30 +08:00
404 241a16c40b Merge branch 'feature/resumable_upload' into develop 2012-10-08 13:00:56 +08:00
404 9a5a27429d add resumable upload and all tests(63 examples) passed 2012-10-08 12:59:30 +08:00
404 8011d6d837 all resumable uploading tests are passed 2012-10-03 12:02:38 +08:00
404 2451b5b51f update qiniu/rs/exceptions 2012-10-02 23:40:26 +08:00
404 bd34ea428e add Qiniu::RS::UP module 2012-10-02 18:47:19 +08:00
404 fa40449b12 added UploadFailedError 2012-09-27 20:22:51 +08:00
404 cf8a38faa0 Merge branch 'master' into develop 2012-09-25 13:44:54 +08:00
404 1072b1ebab Merge branch 'hotfix/v3.0.6' into develop 2012-09-12 16:19:55 +08:00
404 f304a2cb67 Merge branch 'master' into develop 2012-09-03 18:14:51 +08:00
404 7432857c5a Merge branch 'hotfix/v3.0.5' into develop 2012-09-03 18:12:25 +08:00
404 7c1fd4d207 Merge branch 'master' into develop 2012-08-19 02:03:33 +08:00
404 263ea64367 Merge remote-tracking branch 'qiniu/master' into develop 2012-08-19 01:59:21 +08:00
404 9c719cc8e5 Merge branch 'hotfix/v3.0.3' into develop 2012-08-18 00:00:18 +08:00
404 64ddbbe831 Merge branch 'hotfix/v3.0.2' into develop 2012-08-17 09:44:12 +08:00
31 changed files with 1389 additions and 648 deletions
+3 -1
View File
@@ -7,6 +7,8 @@ rvm:
- jruby-19mode
- rbx-18mode
- rbx-19mode
- ruby-head
- jruby-head
- ree
before_script:
- export QINIU_ACCESS_KEY=LGRqJsjX7LAvtdtX6hhyxs861lS57HwnOQmJsOuX
- export QINIU_SECRET_KEY=g_OxhuN5iJ1O7BoA6YdXyF6zSszk34UZFKDfdIIW
+70
View File
@@ -0,0 +1,70 @@
## CHANGE LOG
### v3.4.2
- `Qiniu::RS.generate_upload_token()` 方法新增 `:return_body` 选项。
该选项(`:return_body`)可设置文件上传成功后,执行七牛云存储规定的回调API,并以 JSON 响应格式返回其执行结果。参考 [uploadToken 之 returnBody 说明](http://docs.qiniutek.com/v3/api/io/#uploadToken-returnBody)。
### v3.4.1
增加为上传文件进行预转的选项,参见 [uploadToken 之 asyncOps 说明](http://docs.qiniutek.com/v3/api/io/#uploadToken-asyncOps)
- `Qiniu::RS.generate_upload_token()` 方法新增 `:async_options` 选项用于进行预转操作。
### 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 全局唯一
使得10多种不通的 Ruby 宿主环境能隔离互不影响地执行单元/集成测试
Ruby 宿主环境如下
1.8.7, 1.9.2, 1.9.3, jruby-18mode, jruby-19mode, rbx-18mode, rbx-19mode, ruby-head, jruby-head, ree
详见 <https://travis-ci.org/qiniu/ruby-sdk>
### v3.3.0
私有资源下载新版实现,添加 Qiniu::RS.generate_download_token() 方法。参考 [downloadToken](http://docs.qiniutek.com/v3/api/io/#get)
详见 [Ruby SDK 使用文档之私有资源下载](http://docs.qiniutek.com/v3/sdk/ruby/#download-private-files)
### v3.2.2
fixed E701 error
断点续上传根据 mkblk 返回的 host 字段进行 bput 和 mkfile ,规避由于DNS智能解析造成的分布式并行块上传会出现上下文不连贯导致的 E701 问题。
### v3.2.1
allow images uploaded auto-orient.
允许图片上传成功后自动旋转。
参考:
1. [[API] multipart/form-data 上传文件之 action 字段详解](http://docs.qiniutek.com/v3/api/io/#upload-action)
2. [[SDK] Qiniu::RS.upload_file() 方法中的参数增加了 :rotate 选项](http://docs.qiniutek.com/v3/sdk/ruby/#upload-server-side)
### v3.2.0
2012-11-06
allow files uploaded auto callback some APIs (like imageInfo, exif, etc…), and add those APIs callback results as part of the custom data for POST biz-server.
允许上传文件(图片)成功后执行回调指定的 API (比如 imageInfo, exif 接口等),并将指定API的回调结果一并 POST 发送给客户方的业务服务器。
参考:
1. [[API] 生成上传授权凭证 uploadToken 之 escape 参数详解](http://docs.qiniutek.com/v3/api/io/#escape-expression)
2. [[SDK] Qiniu::RS.generate_upload_token() 方法中的参数增加了 :escape 选项](http://docs.qiniutek.com/v3/sdk/ruby/#generate-upload-token)
+13 -13
View File
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
qiniu-rs (3.0.6)
qiniu-rs (3.4.2)
json (~> 1.7)
mime-types (~> 1.19)
rest-client (~> 1.6)
@@ -12,19 +12,19 @@ GEM
specs:
diff-lcs (1.1.3)
fakeweb (1.3.0)
json (1.7.5)
mime-types (1.19)
rake (0.9.2.2)
json (1.7.6)
mime-types (1.20.1)
rake (10.0.3)
rest-client (1.6.7)
mime-types (>= 1.16)
rspec (2.11.0)
rspec-core (~> 2.11.0)
rspec-expectations (~> 2.11.0)
rspec-mocks (~> 2.11.0)
rspec-core (2.11.1)
rspec-expectations (2.11.3)
rspec (2.12.0)
rspec-core (~> 2.12.0)
rspec-expectations (~> 2.12.0)
rspec-mocks (~> 2.12.0)
rspec-core (2.12.2)
rspec-expectations (2.12.1)
diff-lcs (~> 1.1.3)
rspec-mocks (2.11.2)
rspec-mocks (2.12.2)
ruby-hmac (0.4.0)
PLATFORMS
@@ -33,5 +33,5 @@ PLATFORMS
DEPENDENCIES
fakeweb (~> 1.3)
qiniu-rs!
rake (~> 0.9)
rspec (~> 2.11)
rake (>= 0.9)
rspec (>= 2.11)
+1 -1
View File
@@ -1,4 +1,4 @@
Copyright (c) 2012 why404
Copyright (c) 2012-2013 qiniutek.com
MIT License
+2 -2
View File
@@ -1,4 +1,4 @@
# Qiniu Resource (Cloud) Storage SDK for Ruby - [![Build Status](https://secure.travis-ci.org/why404/qiniu-rs-for-ruby.png?branch=master)](http://travis-ci.org/why404/qiniu-rs-for-ruby) [![Dependency Status](https://gemnasium.com/why404/qiniu-rs-for-ruby.png)](https://gemnasium.com/why404/qiniu-rs-for-ruby)
# Qiniu Resource (Cloud) Storage SDK for Ruby - [![Build Status](https://api.travis-ci.org/qiniu/ruby-sdk.png?branch=master)](https://travis-ci.org/qiniu/ruby-sdk) [![Dependency Status](https://gemnasium.com/why404/qiniu-rs-for-ruby.png)](https://gemnasium.com/why404/qiniu-rs-for-ruby)
# 关于
@@ -32,7 +32,7 @@
## 许可证
Copyright (c) 2012 qiniutek.com
Copyright (c) 2012-2013 qiniutek.com
基于 MIT 协议发布:
+305 -503
View File
File diff suppressed because it is too large Load Diff
+69 -11
View File
@@ -1,15 +1,15 @@
# -*- encoding: utf-8 -*-
require 'qiniu/rs/version'
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'
@@ -17,6 +17,8 @@ module Qiniu
autoload :AccessToken, 'qiniu/tokens/access_token'
autoload :QboxToken, 'qiniu/tokens/qbox_token'
autoload :UploadToken, 'qiniu/tokens/upload_token'
autoload :DownloadToken, 'qiniu/tokens/download_token'
autoload :Abstract, 'qiniu/rs/abstract'
class << self
@@ -61,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
@@ -70,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)
@@ -99,15 +103,37 @@ module Qiniu
end
def upload_file opts = {}
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])
code == StatusOK ? data : false
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)
@@ -125,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
@@ -145,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
@@ -205,6 +251,18 @@ module Qiniu
#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
+24
View File
@@ -0,0 +1,24 @@
# -*- encoding: utf-8 -*-
module Qiniu
module RS
module Abstract
def self.included(base)
base.extend(ClassMethods)
end
module ClassMethods
def abstract_methods(*args)
args.each do |name|
class_eval <<-END
def #{name}(*args)
errmsg = %Q(class \#{self.class.name} must implement abstract method #{self.name}##{name}().)
raise NotImplementedError.new(errmsg)
end
END
end
end
end
end
end
end
+3 -3
View File
@@ -18,7 +18,7 @@ module Qiniu
:password => password
}
code, data = http_request Config.settings[:auth_url], post_data
reset_token(data["access_token"], data["refresh_token"]) if code == 200
reset_token(data["access_token"], data["refresh_token"]) if Utils.is_response_ok?(code)
[code, data]
end
@@ -29,7 +29,7 @@ module Qiniu
:refresh_token => refresh_token
}
code, data = http_request Config.settings[:auth_url], post_data
reset_token(data["access_token"], data["refresh_token"]) if code == 200
reset_token(data["access_token"], data["refresh_token"]) if Utils.is_response_ok?(code)
[code, data]
end
@@ -48,7 +48,7 @@ module Qiniu
raise MissingUsernameOrPassword if (@username.nil? || @password.nil?)
code, data = exchange_by_password!(@username, @password)
end
if code == 200
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)
+9 -5
View File
@@ -8,7 +8,7 @@
# Qbox::Config.load "path/to/your_project/config/qiniu.yml"
#
require "qiniu/rs/version"
require 'tmpdir'
module Qiniu
module RS
@@ -16,11 +16,11 @@ module Qiniu
class << self
DEFAULT_OPTIONS = {
:user_agent => 'Qiniu-RS-Ruby-SDK-' + VERSION + '()',
:user_agent => 'Qiniu-RS-Ruby-SDK-' + Version.to_s + '()',
:method => :post,
:content_type => 'application/x-www-form-urlencoded',
:auth_url => "https://acc.qbox.me/oauth2/token",
:rs_host => "http://rs.qbox.me:10100",
:rs_host => "http://rs.qbox.me",
:io_host => "http://iovip.qbox.me",
:up_host => "http://up.qbox.me",
:pub_host => "http://pu.qbox.me:10200",
@@ -30,10 +30,14 @@ module Qiniu
:access_key => "",
:secret_key => "",
:auto_reconnect => true,
:max_retry_times => 5
:max_retry_times => 3,
:block_size => 1024*1024*4,
:chunk_size => 1024*256,
:enable_debug => true,
:tmpdir => Dir.tmpdir + File::SEPARATOR + 'Qiniu-RS-Ruby-SDK'
}
REQUIRED_OPTION_KEYS = [:client_id, :client_secret, :auth_url, :rs_host, :io_host]
REQUIRED_OPTION_KEYS = [:access_key, :secret_key]
attr_reader :settings, :default_params
+38 -4
View File
@@ -4,9 +4,6 @@ module Qiniu
module RS
class Exception < RuntimeError
def to_s
inspect
end
end
class ResponseError < Exception
@@ -32,7 +29,7 @@ module Qiniu
class RequestFailed < ResponseError
def message
"HTTP status code #{http_code}"
"HTTP status code: #{http_code}. Response body: #{http_body}"
end
def to_s
@@ -40,6 +37,43 @@ module Qiniu
end
end
class UploadFailedError < Exception
def initialize(status_code, response_data)
data_string = response_data.map { |key, value| %Q(:#{key.to_s} => #{value.to_s}) }
msg = %Q(Uploading Failed. HTTP Status Code: #{status_code}. HTTP response body: #{data_string.join(', ')}.)
super(msg)
end
end
class FileSeekReadError < Exception
def initialize(fpath, block_index, seek_pos, read_length, result_length)
msg = "Reading file: #{fpath}, "
msg += "at block index: #{block_index}. "
msg += "Expected seek_pos:#{seek_pos} and read_length:#{read_length}, "
msg += "but got result_length: #{result_length}."
super(msg)
end
end
class BlockSizeNotMathchError < Exception
def initialize(fpath, block_index, offset, restsize, block_size)
msg = "Reading file: #{fpath}, "
msg += "at block index: #{block_index}. "
msg += "Expected offset: #{offset}, restsize: #{restsize} and block_size: #{block_size}, "
msg += "but got offset+restsize=#{offset+restsize}."
super(msg)
end
end
class BlockCountNotMathchError < Exception
def initialize(fpath, block_count, checksum_count, progress_count)
msg = "Reading file: #{fpath}, "
msg += "Expected block_count, checksum_count, progress_count is: #{block_count}, "
msg += "but got checksum_count: #{checksum_count}, progress_count: #{progress_count}."
super(msg)
end
end
class MissingArgsError < Exception
def initialize(missing_keys)
key_list = missing_keys.map {|key| key.to_s}.join(' and the ')
+4 -3
View File
@@ -34,8 +34,8 @@ module Qiniu
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)
action_params = _generate_action_params(local_file, bucket, key, mime_type, custom_meta, enable_crc32_check)
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'
@@ -43,7 +43,7 @@ module Qiniu
end
private
def _generate_action_params(local_file, bucket, key = nil, mime_type = nil, custom_meta = nil, enable_crc32_check = false)
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
@@ -54,6 +54,7 @@ module Qiniu
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
+34
View File
@@ -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
+324
View File
@@ -0,0 +1,324 @@
# -*- encoding: utf-8 -*-
require 'zlib'
require 'yaml'
require 'tmpdir'
require 'fileutils'
require 'mime/types'
require 'digest/sha1'
require 'qiniu/rs/abstract'
require 'qiniu/rs/exceptions'
require 'qiniu/rs/io'
module Qiniu
module RS
module UP
PROGRESS_TMP_FILE = 'progresses'
CHECKSUM_TMP_FILE = 'ctxes'
module AbstractClass
class ChunkProgressNotifier
include Qiniu::RS::Abstract
abstract_methods :notify
# def notify(block_index, block_put_progress); end
end
class BlockProgressNotifier
include Qiniu::RS::Abstract
abstract_methods :notify
# def notify(block_index, checksum); end
end
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
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)
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(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
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
end
end
end
end
+18 -4
View File
@@ -32,6 +32,20 @@ module Qiniu
{}
end
def is_response_ok?(status_code)
status_code/100 == 2
end
def response_error(status_code, errmsg)
[status_code, {"error" => errmsg}]
end
def debug(msg)
if Config.settings[:enable_debug]
Log.logger.debug(msg)
end
end
def send_request_with url, data = nil, options = {}
options[:method] = Config.settings[:method] unless options[:method]
options[:content_type] = Config.settings[:content_type] unless options[:content_type]
@@ -42,8 +56,8 @@ module Qiniu
auth_token = nil
if !options[:qbox_signature_token].nil? && !options[:qbox_signature_token].empty?
auth_token = 'QBox ' + options[:qbox_signature_token]
#elsif !options[:upload_signature_token].nil? && !options[:upload_signature_token].empty?
# auth_token = 'UpToken ' + options[:upload_signature_token]
elsif !options[:upload_signature_token].nil? && !options[:upload_signature_token].empty?
auth_token = 'UpToken ' + options[:upload_signature_token]
elsif options[:access_token]
auth_token = 'Bearer ' + options[:access_token]
end
@@ -56,8 +70,8 @@ module Qiniu
response = RestClient.post(url, data, header_options)
end
code = response.respond_to?(:code) ? response.code.to_i : 0
if code != 200
raise RequestFailed.new(response)
unless is_response_ok?(code)
raise RequestFailed.new("Request Failed", response)
else
data = {}
body = response.respond_to?(:body) ? response.body : {}
+13 -1
View File
@@ -2,6 +2,18 @@
module Qiniu
module RS
VERSION = "3.0.6"
module Version
MAJOR = 3
MINOR = 4
PATCH = 2
# Returns a version string by joining <tt>MAJOR</tt>, <tt>MINOR</tt>, and <tt>PATCH</tt> with <tt>'.'</tt>
#
# Example
#
# Version.to_s # '1.0.2'
def self.to_s
[MAJOR, MINOR, PATCH].join('.')
end
end
end
end
+33
View File
@@ -0,0 +1,33 @@
# -*- encoding: utf-8 -*-
require 'json'
require 'qiniu/tokens/access_token'
require 'qiniu/rs/utils'
module Qiniu
module RS
class DownloadToken < AccessToken
include Utils
attr_accessor :pattern, :expires_in
def initialize(opts = {})
@pattern = opts[:pattern] || "*"
@expires_in = opts[:expires_in] || 3600
end
def generate_signature
params = {"S" => @pattern, "E" => Time.now.to_i + @expires_in}
Utils.urlsafe_base64_encode(params.to_json)
end
def generate_token
signature = generate_signature
encoded_digest = generate_encoded_digest(signature)
%Q(#{@access_key}:#{encoded_digest}:#{signature})
end
end
end
end
+6 -4
View File
@@ -8,6 +8,8 @@ module Qiniu
module RS
class QboxToken < AccessToken
include Utils
attr_accessor :url, :params
def initialize(opts = {})
@@ -20,16 +22,16 @@ module Qiniu
signature = uri.path
query_string = uri.query
signature += '?' + query_string if !query_string.nil? && !query_string.empty?
signature += "\n";
signature += "\n"
if @params.is_a?(Hash)
total_param = @params.map { |key, value| %Q(#{CGI.escape(key.to_s)}=#{CGI.escape(value.to_s).gsub('+', '%20')}) }
signature += total_param.join("&")
params_string = Utils.generate_query_string(@params)
signature += params_string
end
signature
end
def generate_token
encoded_digest = generate_encoded_digest(signature)
encoded_digest = generate_encoded_digest(generate_signature)
%Q(#{@access_key}:#{encoded_digest})
end
+12 -2
View File
@@ -10,22 +10,32 @@ module Qiniu
include Utils
attr_accessor :scope, :expires_in, :callback_url, :callback_body_type, :customer
attr_accessor :scope, :expires_in, :callback_url, :callback_body, :callback_body_type, :customer, :escape, :async_options, :return_body, :return_url
def initialize(opts = {})
@scope = opts[:scope]
@expires_in = opts[:expires_in] || 3600
@callback_url = opts[:callback_url]
@callback_body = opts[:callback_body]
@callback_body_type = opts[:callback_body_type]
@customer = opts[:customer]
@escape = opts[:escape]
@async_options = opts[:async_options]
@return_body = opts[:return_body]
@return_url = opts[:return_url]
end
def generate_signature
params = {:scope => @scope, :deadline => Time.now.to_i + @expires_in}
params[:callbackUrl] = @callback_url if !@callback_url.nil? && !@callback_url.empty?
params[:callbackBody] = @callback_body if !@callback_body.nil? && !@callback_body.empty?
params[:callbackBodyType] = @callback_body_type if !@callback_body_type.nil? && !@callback_body_type.empty?
params[:customer] = @customer if !@customer.nil? && !@customer.empty?
urlsafe_base64_encode(params.to_json)
params[:escape] = 1 if @escape == 1 || @escape == true
params[:asyncOps] = @async_options if !@async_options.nil? && !@async_options.empty?
params[:returnBody] = @return_body if !@return_body.nil? && !@return_body.empty?
params[:returnUrl] = @return_url if !@return_url.nil? && !@return_url.empty?
Utils.urlsafe_base64_encode(params.to_json)
end
def generate_token
+3 -3
View File
@@ -14,11 +14,11 @@ 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
gem.version = Qiniu::RS::Version.to_s
# specify any dependencies here; for example:
gem.add_development_dependency "rake", "~> 0.9"
gem.add_development_dependency "rspec", "~> 2.11"
gem.add_development_dependency "rake", ">= 0.9"
gem.add_development_dependency "rspec", ">= 2.11"
gem.add_development_dependency "fakeweb", "~> 1.3"
gem.add_runtime_dependency "json", "~> 1.7"
gem.add_runtime_dependency "rest-client", "~> 1.6"
+30
View File
@@ -0,0 +1,30 @@
# -*- encoding: utf-8 -*-
require 'spec_helper'
require 'qiniu/rs/abstract'
describe Qiniu::RS::Abstract do
before(:each) do
@klass = Class.new do
include Qiniu::RS::Abstract
abstract_methods :foo, :bar
end
end
it "raises NotImplementedError" do
proc {
@klass.new.foo
}.should raise_error(NotImplementedError)
end
it "can be overridden" do
subclass = Class.new(@klass) do
def foo
:overridden
end
end
subclass.new.foo.should == :overridden
end
end
+15 -9
View File
@@ -1,7 +1,7 @@
# -*- encoding: utf-8 -*-
require 'spec_helper'
require 'qiniu/rs/io'
require 'qiniu/rs'
require 'qiniu/rs/pub'
require 'qiniu/rs/eu'
@@ -10,16 +10,19 @@ module Qiniu
describe EU do
before :all do
@customer_id = "awhy.xu@gmail.com"
@bucket = "wm_test_bucket"
@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)
code, data = Qiniu::RS::IO.upload_with_token(uptoken, local_file, @bucket, @key, nil, nil, nil, true)
code.should == 200
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, "-")
@@ -31,11 +34,13 @@ module Qiniu
puts data3.inspect
end
#after :all do
# result = Qiniu::RS.drop(@bucket)
# result.should_not be_false
#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 = {
@@ -54,6 +59,7 @@ module Qiniu
puts data.inspect
end
end
=end
end
end
+25 -23
View File
@@ -10,35 +10,31 @@ module Qiniu
describe Image do
before :all do
=begin
code, data = Qiniu::RS::Auth.exchange_by_password!("test@qbox.net", "test")
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
puts data.inspect
=end
@bucket = "test_images_12345"
@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__)
result = Qiniu::RS.drop(@bucket)
result.should_not be_false
puts result.inspect
put_url = Qiniu::RS.put_auth(10)
put_url.should_not be_false
put_url.should_not be_empty
result = Qiniu::RS.upload :url => put_url,
:file => local_file,
:bucket => @bucket,
:key => @key,
:enable_crc32_check => true
result.should be_true
upopts = {
:scope => @bucket,
:expires_in => 3600,
:customer => "why404@gmail.com",
: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
puts data.inspect
data["size"].should_not be_zero
data["hash"].should_not be_empty
data["width"].should_not be_zero
data["height"].should_not be_zero
result = Qiniu::RS.get(@bucket, @key)
result["url"].should_not be_empty
@@ -56,6 +52,12 @@ module Qiniu
}
end
after :all do
result = Qiniu::RS.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)
+14 -13
View File
@@ -1,4 +1,4 @@
# Utils.-*- encoding: utf-8 -*-
# -*- encoding: utf-8 -*-
require 'spec_helper'
require 'qiniu/rs/auth'
@@ -10,17 +10,18 @@ module Qiniu
describe IO do
before :all do
=begin
code, data = Qiniu::RS::Auth.exchange_by_password!("test@qbox.net", "test")
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
puts data.inspect
=end
@bucket = "test"
@key = Digest::SHA1.hexdigest (Time.now.to_i+rand(100)).to_s
@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)
puts result.inspect
result.should be_true
end
after :all do
result = Qiniu::RS.drop(@bucket)
puts result.inspect
result.should_not be_false
end
context ".upload_file" do
@@ -46,7 +47,7 @@ module Qiniu
context ".upload_with_token" do
it "should works" do
upopts = {:scope => @bucket, :expires_in => 3600, :customer => "awhy.xu@gmail.com"}
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)
code.should == 200
+5 -14
View File
@@ -10,24 +10,15 @@ module Qiniu
describe Pub do
before :all do
@bucket = "test123"
code, data = Qiniu::RS::RS.mkbucket(@bucket)
code.should == 200
puts data.inspect
@bucket = 'RubySdkTest' + (Time.now.to_i+rand(1000)).to_s
result = Qiniu::RS.mkbucket(@bucket)
puts result.inspect
result.should_not be_false
end
=begin
context ".mkbucket" do
it "should works" do
code, data = Qiniu::RS::RS.mkbucket(@bucket)
code.should == 200
puts data.inspect
end
end
=end
after :all do
result = Qiniu::RS.drop(@bucket)
puts result.inspect
result.should_not be_false
end
+71 -7
View File
@@ -11,28 +11,34 @@ module Qiniu
describe RS do
before :all do
@bucket = "test_bucket_1234"
@key = Digest::SHA1.hexdigest (Time.now.to_i+rand(100)).to_s
@domain = 'iovip.qbox.me/test'
@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
puts data.inspect
end
context "IO.upload_file" do
it "should works" do
code, data = Qiniu::RS::IO.put_auth()
puts data.inspect
code.should == 200
data["url"].should_not be_empty
data["expiresIn"].should_not be_zero
puts data.inspect
@put_url = data["url"]
code2, data2 = Qiniu::RS::IO.upload_file(@put_url, __FILE__, @bucket, @key)
code2.should == 200
puts data2.inspect
code2.should == 200
end
end
@@ -84,6 +90,35 @@ 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
code, data = Qiniu::RS::RS.publish(@domain, @bucket)
@@ -99,6 +134,35 @@ module Qiniu
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
+59
View File
@@ -0,0 +1,59 @@
# -*- encoding: utf-8 -*-
require 'digest/sha1'
require 'spec_helper'
require 'qiniu/rs/rs'
require 'qiniu/rs/up'
module Qiniu
module RS
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)
code, data = Qiniu::RS::RS.mkbucket(@bucket)
puts [code, data].inspect
code.should == 200
end
after :all do
File.unlink(@localfile) if File.exists?(@localfile)
code, data = Qiniu::RS::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::RS.generate_upload_token(upopts)
code, data = Qiniu::RS::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::RS.stat(@bucket, @key)
puts data.inspect
code.should == 200
end
end
context ".delete" do
it "should works" do
code, data = Qiniu::RS::RS.delete(@bucket, @key)
puts data.inspect
code.should == 200
end
end
end
end
end
+2 -2
View File
@@ -3,8 +3,8 @@
require 'spec_helper'
require 'qiniu/rs/version'
describe Qiniu::RS do
describe Qiniu::RS::Version do
it "should has a VERSION" do
Qiniu::RS::VERSION.should =~ /^\d+\.\d+\.\d+?$/
Qiniu::RS::Version.to_s.should =~ /^\d+\.\d+\.\d+?$/
end
end
+154 -12
View File
@@ -1,21 +1,47 @@
# -*- encoding: utf-8 -*-
require 'digest/sha1'
require 'spec_helper'
require 'qiniu/rs'
require 'qiniu/rs/exceptions'
module Qiniu
describe RS do
before :all do
@bucket = 'qiniu_rs_test'
@bucket = 'RubySdkTest' + (Time.now.to_i+rand(1000)).to_s
@key = Digest::SHA1.hexdigest Time.now.to_s
@domain = 'cdn.example.com'
@test_image_bucket = 'test_images_12345'
@test_image_key = 'image_logo_for_test.png'
@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 ".buckets" do
@@ -58,6 +84,7 @@ module Qiniu
end
end
=begin
context ".set_watermark" do
it "should works" do
options = {
@@ -76,6 +103,7 @@ module Qiniu
puts result.inspect
end
end
=end
context ".put_auth" do
it "should works" do
@@ -115,7 +143,7 @@ module Qiniu
context ".upload_file" do
it "should works" do
uptoken_opts = {:scope => @bucket}
uptoken_opts = {:scope => @bucket, :escape => 0}
upload_opts = {
:uptoken => Qiniu::RS.generate_upload_token(uptoken_opts),
:file => __FILE__,
@@ -127,6 +155,64 @@ module Qiniu
result.should_not be_false
puts result.inspect
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)
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)
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
end
end
context ".stat" do
@@ -192,19 +278,69 @@ module Qiniu
end
end
=begin
context ".publish" do
it "should works" do
result = Qiniu::RS.publish(@domain, @bucket)
result.should_not be_false
end
end
=end
=begin
context ".unpublish" do
it "should works" do
result = Qiniu::RS.unpublish(@domain)
result.should_not be_false
end
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 ".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 ".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 ".delete" do
it "should works" do
@@ -251,10 +387,6 @@ module Qiniu
data.should_not be_empty
puts data.inspect
dest_bucket = "test_thumbnails_bucket"
result = Qiniu::RS.mkbucket(dest_bucket)
result.should_not be_false
dest_key = "cropped-" + @test_image_key
src_img_url = data["url"]
mogrify_options = {
@@ -266,7 +398,7 @@ module Qiniu
:format => "jpg",
:auto_orient => true
}
result2 = Qiniu::RS.image_mogrify_save_as(dest_bucket, dest_key, src_img_url, mogrify_options)
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
@@ -275,9 +407,19 @@ module Qiniu
context ".generate_upload_token" do
it "should works" do
data = Qiniu::RS.generate_upload_token({:scope => 'test_bucket', :expires_in => 3600})
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
+29
View File
@@ -0,0 +1,29 @@
# -*- encoding: utf-8 -*-
require 'spec_helper'
require 'qiniu/tokens/qbox_token'
module Qiniu
module RS
describe QboxToken do
before :all do
@qbox_token = QboxToken.new(:url => 'www.qiniutek.com?key1=value1',
:params => { :key2 => 'value2' })
@qbox_token.access_key = 'access_key'
@qbox_token.secret_key = 'secret_key'
end
context "#generate_token" do
it "should generate token" do
@qbox_token.generate_token.should_not be_empty
end
end
context "#generate_signature" do
it "should generate signature" do
@qbox_token.generate_signature.should == "www.qiniutek.com?key1=value1\nkey2=value2"
end
end
end
end
end
+1 -8
View File
@@ -6,13 +6,6 @@ require 'rspec'
RSpec.configure do |config|
config.before :all do
Qiniu::RS.establish_connection! :access_key => "dFX_wMGVrRzwdWaraW-Qe5ZCDT-kcSmIAGKQOkXh",
:secret_key => "VllxxDfkn_h2ZIqeKYTnHJiN4LVODfDBlJHy_KsW",
:auth_url => "http://m1.qbox.me:13001/oauth2/token",
:rs_host => "http://m1.qbox.me:13003",
:io_host => "http://m1.qbox.me:13004",
:up_host => "http://m1.qbox.me:13019",
:pub_host => "http://m1.qbox.me:13012",
:eu_host => "http://m1.qbox.me:13050"
Qiniu::RS.establish_connection! :access_key => ENV["QINIU_ACCESS_KEY"], :secret_key => ENV["QINIU_SECRET_KEY"]
end
end