mirror of
https://github.com/wahyd4/ruby-sdk.git
synced 2026-08-10 05:18:37 +10:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7e66297bf9 | ||
|
|
ba91ee2377 | ||
|
|
5d07bc517c | ||
|
|
ad579f402b | ||
|
|
34fc65bd99 | ||
|
|
ecbeb51339 | ||
|
|
ef0d79eb17 | ||
|
|
edc86e230e | ||
|
|
5718b720df | ||
|
|
5cc4db2231 | ||
|
|
f823ff0659 | ||
|
|
5a05fd9f8f | ||
|
|
aedb459b08 | ||
|
|
1b464029f7 | ||
|
|
1d39aeec05 | ||
|
|
efe6b50c50 | ||
|
|
544d1069e2 | ||
|
|
0ccda7cdb1 | ||
|
|
3f4c79b78f | ||
|
|
26b46859a1 | ||
|
|
1bc0476671 | ||
|
|
c91c5e4246 | ||
|
|
fe70576624 | ||
|
|
0c8a8e98e0 | ||
|
|
e2b3162f1f | ||
|
|
b99ff384ef | ||
|
|
fc5afc332a | ||
|
|
5c9169b516 | ||
|
|
fee579a5ba | ||
|
|
bc1b8acea5 | ||
|
|
11188ff682 | ||
|
|
17c961da1c | ||
|
|
476937bc60 | ||
|
|
cfad4ef53f | ||
|
|
d9ea68efa8 | ||
|
|
6aaacbe2e8 | ||
|
|
8ffee73524 | ||
|
|
fa36f09765 | ||
|
|
8173b31b18 | ||
|
|
bcb8ad7392 | ||
|
|
9539c665bb | ||
|
|
27eb25fc11 | ||
|
|
9374a451cd | ||
|
|
c49ea097b8 | ||
|
|
3753e759dd | ||
|
|
97d2c23b25 | ||
|
|
4b7c5a2574 | ||
|
|
3cae9276b2 | ||
|
|
8bd052e309 | ||
|
|
e97dc07ba3 | ||
|
|
128e6f0fd7 | ||
|
|
345fafd609 | ||
|
|
5d4785c17d | ||
|
|
2890272015 | ||
|
|
eb3461a6bb | ||
|
|
1a72e1055b | ||
|
|
cd66ce79ff | ||
|
|
ae0dbced1b | ||
|
|
8f5c6c0b0a | ||
|
|
d3ec623c3e | ||
|
|
ab81095c0c | ||
|
|
f1cb65d79a | ||
|
|
36c492b9b9 | ||
|
|
cd48eca8f6 | ||
|
|
51c719767f | ||
|
|
de61097fde | ||
|
|
3a3d026812 | ||
|
|
5e8e69a339 | ||
|
|
1a1affc3d3 | ||
|
|
cb748d8857 | ||
|
|
08f36221b7 | ||
|
|
ef9c90c7c5 | ||
|
|
3302ecde6a | ||
|
|
b0335577e5 | ||
|
|
0f3704c60f | ||
|
|
818c4bb5b3 | ||
|
|
f1461c338e | ||
|
|
67280b714e | ||
|
|
8f86e6903a | ||
|
|
47cf54069d | ||
|
|
0bb4edcade | ||
|
|
b1e6235b2e | ||
|
|
e5d2367a27 | ||
|
|
3186ba40df | ||
|
|
c5708424b9 | ||
|
|
f85ee1159b | ||
|
|
804c7609ef | ||
|
|
49ab640493 | ||
|
|
85f8e68399 | ||
|
|
7dbec75553 | ||
|
|
3299d2bd02 | ||
|
|
ec8c50383b | ||
|
|
9cc157c21d | ||
|
|
3d40d4e967 | ||
|
|
fa85e838fc | ||
|
|
38970b66e3 | ||
|
|
63ccf3f2dc | ||
|
|
5c011aa816 | ||
|
|
7b507f15c6 | ||
|
|
aab7277b50 | ||
|
|
eaed72eeaa | ||
|
|
60cc7274b1 | ||
|
|
466a4ba07e | ||
|
|
444956f161 | ||
|
|
0e4b5d00bc | ||
|
|
5d2745d148 | ||
|
|
3026a40d99 | ||
|
|
84f7cc6205 | ||
|
|
5562df4b28 | ||
|
|
70c4664e0d | ||
|
|
2096bff7a7 | ||
|
|
b0af3c1814 | ||
|
|
9bda6845a0 | ||
|
|
d9422cbcf4 | ||
|
|
38c054f24c | ||
|
|
f85c4fab41 | ||
|
|
f9bbcfeec6 | ||
|
|
e3d4a80b54 | ||
|
|
61aae133cf | ||
|
|
c3e2a3d44b | ||
|
|
06b019008a | ||
|
|
957b5d1499 | ||
|
|
5b0a561cc1 | ||
|
|
d1e60697c3 | ||
|
|
089b6bc7c6 | ||
|
|
f1edde2e54 | ||
|
|
8aca8bf33e | ||
|
|
69bb9cb913 | ||
|
|
cacdb4f926 | ||
|
|
557b75f33f | ||
|
|
2cbb175423 | ||
|
|
a8a8f63887 | ||
|
|
64cf9b5d3f | ||
|
|
1a73c39ffb |
+5
-8
@@ -1,12 +1,9 @@
|
||||
language: ruby
|
||||
rvm:
|
||||
- 1.8.7
|
||||
- 1.9.2
|
||||
- 1.9.3
|
||||
- jruby-18mode
|
||||
- 2.0.0
|
||||
- 2.1.1
|
||||
- jruby-19mode
|
||||
- rbx-18mode
|
||||
- rbx-19mode
|
||||
- ruby-head
|
||||
- jruby-head
|
||||
- ree
|
||||
before_script:
|
||||
- export QINIU_ACCESS_KEY=LGRqJsjX7LAvtdtX6hhyxs861lS57HwnOQmJsOuX
|
||||
- export QINIU_SECRET_KEY=g_OxhuN5iJ1O7BoA6YdXyF6zSszk34UZFKDfdIIW
|
||||
|
||||
+105
@@ -0,0 +1,105 @@
|
||||
## CHANGE LOG
|
||||
|
||||
### v6.2.1
|
||||
|
||||
- 去除已废弃的publish/unpublish接口。 [https://github.com/qiniu/ruby-sdk/pull/90](https://github.com/qiniu/ruby-sdk/pull/90) (#8143)
|
||||
|
||||
### v6.2.0
|
||||
|
||||
- 重写与授权相关的函数并归入Qiniu::Auth空间,原授权凭证生成类维持不变。
|
||||
|
||||
- 添加Qiniu::Storage::PutPolicy类和Qiniu::Storage#upload_with_put_policy方法,并推荐使用两者组合实现单文件上传。
|
||||
|
||||
### v6.1.0
|
||||
|
||||
- Qiniu::Storage所有上传接口返回第三个值raw_headers,类型为Hash,包含已解析的HTTP响应报文中的所有Header信息。
|
||||
|
||||
该返回值主要用于调试。当遇到难以理解或解释的错误时,请将其中的X-Log和X-Reqid两项信息[通过邮件反馈](mailto:support@qiniu.com?subject=Ruby-SDK-Bug-Report)给我们。
|
||||
|
||||
- 更新Qiniu::Storage所有上传接口的测试用例,打印HTTP响应Header信息。
|
||||
|
||||
- 删除过时的Qiniu::Storage#put_file方法和相关测试用例。
|
||||
|
||||
该方法调用的API已过时并逐步废弃,建议用户尽快迁移到Qiniu::Storage#upload_with_token_2方法上。
|
||||
|
||||
### v6.0.1
|
||||
|
||||
- 重新划分命名空间,存储相关归入Qiniu::Storage,数据处理相关归入Qiniu::Fop,杂项相关归入Qiniu::Misc。
|
||||
|
||||
### v3.4.6
|
||||
|
||||
- `Qiniu::RS.generate_download_token()` 方法的 `:pattern` 调整为必选项。
|
||||
|
||||
### v3.4.5
|
||||
|
||||
- `Qiniu::RS.generate_upload_token()` 方法新增 `:callback_body` 和 `return_url` 选项。
|
||||
- 选项含义参考: <http://developer.qiniu.com/docs/v6/api/reference/security/put-policy.html#put-policy-callback-body>
|
||||
|
||||
### v3.4.2
|
||||
|
||||
- `Qiniu::RS.generate_upload_token()` 方法新增 `:return_body` 选项。
|
||||
|
||||
该选项(`: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
|
||||
|
||||
增加为上传文件进行预转的选项,参见 [uploadToken 之 asyncOps 说明](http://docs.qiniu.com/api/v6/put.html#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://developer.qiniu.com/docs/v6/api/reference/security/download-token.html)
|
||||
|
||||
详见 [Ruby SDK 使用文档之私有资源下载](http://developer.qiniu.com/docs/v6/api/overview/dn/security.html)
|
||||
|
||||
### 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)
|
||||
+14
-14
@@ -1,7 +1,7 @@
|
||||
PATH
|
||||
remote: .
|
||||
specs:
|
||||
qiniu-rs (3.2.1)
|
||||
qiniu (6.2.1)
|
||||
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.8.1)
|
||||
mime-types (1.23)
|
||||
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.3)
|
||||
rspec-mocks (2.12.2)
|
||||
ruby-hmac (0.4.0)
|
||||
|
||||
PLATFORMS
|
||||
@@ -32,6 +32,6 @@ PLATFORMS
|
||||
|
||||
DEPENDENCIES
|
||||
fakeweb (~> 1.3)
|
||||
qiniu-rs!
|
||||
rake (~> 0.9)
|
||||
rspec (~> 2.11)
|
||||
qiniu!
|
||||
rake (>= 0.9)
|
||||
rspec (>= 2.11)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
Copyright (c) 2012 why404
|
||||
Copyright (c) 2012-2013 qiniutek.com
|
||||
|
||||
MIT License
|
||||
|
||||
|
||||
@@ -1,14 +1,21 @@
|
||||
# Qiniu Resource (Cloud) Storage SDK for Ruby - [](http://travis-ci.org/why404/qiniu-rs-for-ruby) [](https://gemnasium.com/why404/qiniu-rs-for-ruby)
|
||||
# Qiniu Resource (Cloud) Storage SDK for Ruby
|
||||
|
||||
# 关于
|
||||
## 关于
|
||||
|
||||
此 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, 2.0.x, jruby, rbx, ree 版本,基于 [七牛云存储官方API](http://developer.qiniu.com/docs/v6/index.html) 构建。使用此 SDK 构建您的网络应用程序,能让您以非常便捷地方式将数据安全地存储到七牛云存储上。无论您的网络应用是一个网站程序,还是包括从云端(服务端程序)到终端(手持设备应用)的架构的服务或应用,通过七牛云存储及其 SDK,都能让您应用程序的终端用户高速上传和下载,同时也让您的服务端更加轻盈。
|
||||
|
||||
## 状态
|
||||
|
||||
[](https://travis-ci.org/qiniu/ruby-sdk)
|
||||
[](http://badge.fury.io/rb/qiniu)
|
||||
[](https://gemnasium.com/qiniu/ruby-sdk)
|
||||
[](https://codeclimate.com/github/qiniu/ruby-sdk)
|
||||
|
||||
## 安装
|
||||
|
||||
在您 Ruby 应用程序的 `Gemfile` 文件中,添加如下一行代码:
|
||||
|
||||
gem 'qiniu-rs'
|
||||
gem 'qiniu', '~> 6.1.0'
|
||||
|
||||
然后,在应用程序所在的目录下,可以运行 `bundle` 安装依赖包:
|
||||
|
||||
@@ -16,11 +23,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 +39,7 @@
|
||||
|
||||
## 许可证
|
||||
|
||||
Copyright (c) 2012 qiniutek.com
|
||||
Copyright (c) 2012-2014 qiniu.com
|
||||
|
||||
基于 MIT 协议发布:
|
||||
|
||||
|
||||
@@ -1,25 +0,0 @@
|
||||
## CHANGE LOG
|
||||
|
||||
### 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)
|
||||
+250
-507
File diff suppressed because it is too large
Load Diff
+1
-1
@@ -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')
|
||||
|
||||
+73
-100
@@ -1,23 +1,20 @@
|
||||
# -*- 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 :Abstract, 'qiniu/rs/abstract'
|
||||
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 :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
|
||||
|
||||
@@ -27,87 +24,47 @@ 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, data = Storage.mkbucket(bucket_name)
|
||||
code == StatusOK
|
||||
end
|
||||
|
||||
def buckets
|
||||
code, data = RS.buckets
|
||||
code, data = Storage.buckets
|
||||
code == StatusOK ? data : false
|
||||
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 == StatusOK
|
||||
end
|
||||
|
||||
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
|
||||
|
||||
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],
|
||||
opts[:key],
|
||||
opts[:mime_type],
|
||||
opts[:note],
|
||||
opts[:enable_crc32_check])
|
||||
code, data = Misc.unset_style(bucket, name)
|
||||
code == StatusOK
|
||||
end
|
||||
|
||||
def upload_file opts = {}
|
||||
[:uptoken, :file, :bucket, :key].each do |opt|
|
||||
raise MissingArgsError, [opt] unless opts.has_key?(opt)
|
||||
end
|
||||
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],
|
||||
code, data, raw_headers = Storage.upload_with_token(opts[:uptoken],
|
||||
opts[:file],
|
||||
opts[:bucket],
|
||||
opts[:key],
|
||||
@@ -117,7 +74,7 @@ module Qiniu
|
||||
opts[:callback_params],
|
||||
opts[:rotate])
|
||||
else
|
||||
code, data = IO.upload_with_token(opts[:uptoken],
|
||||
code, data, raw_headers = Storage.upload_with_token(opts[:uptoken],
|
||||
opts[:file],
|
||||
opts[:bucket],
|
||||
opts[:key],
|
||||
@@ -132,42 +89,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 = 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 = 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 = Storage.batch_copy(args)
|
||||
code == StatusOK
|
||||
end
|
||||
|
||||
def 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"] }
|
||||
@@ -175,45 +152,31 @@ 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 == StatusOK
|
||||
end
|
||||
|
||||
def unpublish(domain)
|
||||
code, data = RS.unpublish(domain)
|
||||
code == StatusOK
|
||||
end
|
||||
|
||||
def drop(bucket)
|
||||
code, data = RS.drop(bucket)
|
||||
code, data = Storage.drop(bucket)
|
||||
code == StatusOK
|
||||
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_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)
|
||||
Fop::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, data = Storage.image_mogrify_save_as(bucket, key, source_image_url, options)
|
||||
code == StatusOK ? data : false
|
||||
end
|
||||
|
||||
@@ -227,10 +190,20 @@ module Qiniu
|
||||
#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 Qiniu
|
||||
@@ -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
|
||||
@@ -0,0 +1,46 @@
|
||||
# -*- encoding: utf-8 -*-
|
||||
# vim: sw=2 ts=2
|
||||
|
||||
require 'uri'
|
||||
|
||||
module Qiniu
|
||||
module ADT
|
||||
|
||||
class ApiSpecification
|
||||
public
|
||||
def to_str; return ""; end
|
||||
end # class ApiSpecification
|
||||
|
||||
module Policy
|
||||
public
|
||||
def to_json
|
||||
args = {}
|
||||
|
||||
self.params.each_pair do |key, fld|
|
||||
val = self.__send__(key)
|
||||
if !val.nil? then
|
||||
args[fld] = val
|
||||
end
|
||||
end
|
||||
|
||||
return args.to_json
|
||||
end # to_json
|
||||
|
||||
def to_query_string
|
||||
args = []
|
||||
|
||||
self.params.each_pair do |key, fld|
|
||||
val = self.__send__(key)
|
||||
if !val.nil? then
|
||||
new_fld = CGI.escape(fld.to_s)
|
||||
new_val = CGI.escape(val.to_s).gsub('+', '%20')
|
||||
args.push("#{new_fld}=#{new_val}")
|
||||
end
|
||||
end
|
||||
|
||||
return args.join("&")
|
||||
end # to_query_string
|
||||
end # module Policy
|
||||
|
||||
end # module ADT
|
||||
end # module Qiniu
|
||||
Executable
+220
@@ -0,0 +1,220 @@
|
||||
# -*- encoding: utf-8 -*-
|
||||
# vim: sw=2 ts=2
|
||||
|
||||
require 'hmac-sha1'
|
||||
require 'uri'
|
||||
|
||||
require 'qiniu/exceptions'
|
||||
|
||||
module Qiniu
|
||||
module Auth
|
||||
DEFAULT_AUTH_SECONDS = 3600
|
||||
|
||||
class << self
|
||||
def calculate_deadline(expires_in, deadline = nil)
|
||||
### 授权期计算
|
||||
if expires_in.is_a?(Integer) && expires_in > 0 then
|
||||
# 指定相对时间,单位:秒
|
||||
return Time.now.to_i + expires_in
|
||||
elsif deadline.is_a?(Integer) then
|
||||
# 指定绝对时间,常用于调试和单元测试
|
||||
return deadline
|
||||
end
|
||||
|
||||
# 默认授权期1小时
|
||||
return Time.now.to_i + DEFAULT_AUTH_SECONDS
|
||||
end # calculate_deadline
|
||||
end # class << self
|
||||
|
||||
class PutPolicy
|
||||
private
|
||||
def initialize(bucket,
|
||||
key = nil,
|
||||
expires_in = DEFAULT_AUTH_SECONDS,
|
||||
deadline = nil)
|
||||
### 设定scope参数(必填项目)
|
||||
self.scope!(bucket, key)
|
||||
|
||||
### 设定deadline参数(必填项目)
|
||||
@expires_in = expires_in
|
||||
@deadline = Auth.calculate_deadline(expires_in, deadline)
|
||||
end # initialize
|
||||
|
||||
PARAMS = {
|
||||
# 字符串类型参数
|
||||
:scope => "scope" ,
|
||||
:save_key => "saveKey" ,
|
||||
:end_user => "endUser" ,
|
||||
:return_url => "returnUrl" ,
|
||||
:return_body => "returnBody" ,
|
||||
:callback_url => "callbackUrl" ,
|
||||
:callback_body => "callbackBody" ,
|
||||
:persistent_ops => "persistentOps" ,
|
||||
:persistent_notify_url => "persistentNotifyUrl" ,
|
||||
|
||||
# 数值类型参数
|
||||
:deadline => "deadline" ,
|
||||
:insert_only => "insertOnly" ,
|
||||
:fsize_limit => "fsizeLimit" ,
|
||||
:detect_mime => "detectMime" ,
|
||||
:mime_limit => "mimeLimit"
|
||||
} # PARAMS
|
||||
|
||||
public
|
||||
attr_reader :bucket, :key
|
||||
|
||||
def scope!(bucket, key = nil)
|
||||
@bucket = bucket
|
||||
@key = key
|
||||
|
||||
if key.nil? then
|
||||
# 新增语义,文件已存在则失败
|
||||
@scope = bucket
|
||||
else
|
||||
# 覆盖语义,文件已存在则直接覆盖
|
||||
@scope = "#{bucket}:#{key}"
|
||||
end
|
||||
end # scope!
|
||||
|
||||
def expires_in!(seconds)
|
||||
if !seconds.nil? then
|
||||
return @expires_in
|
||||
end
|
||||
|
||||
@epires_in = seconds
|
||||
@deadline = Auth.calculate_deadline(seconds)
|
||||
|
||||
return @expires_in
|
||||
end # expires_in!
|
||||
|
||||
def expires_in=(seconds)
|
||||
return expires_in!(seconds)
|
||||
end # expires_in=
|
||||
|
||||
def expires_in
|
||||
return @expires_in
|
||||
end # expires_in
|
||||
|
||||
def allow_mime_list! (list)
|
||||
@mime_limit = list
|
||||
end # allow_mime_list!
|
||||
|
||||
def deny_mime_list! (list)
|
||||
@mime_limit = "!#{list}"
|
||||
end # deny_mime_list!
|
||||
|
||||
def insert_only!
|
||||
@insert_only = 1
|
||||
end # insert_only!
|
||||
|
||||
def detect_mime!
|
||||
@detect_mime = 1
|
||||
end # detect_mime!
|
||||
|
||||
def to_json
|
||||
args = {}
|
||||
|
||||
PARAMS.each_pair do |key, fld|
|
||||
val = self.__send__(key)
|
||||
if !val.nil? then
|
||||
args[fld] = val
|
||||
end
|
||||
end
|
||||
|
||||
return args.to_json
|
||||
end # to_json
|
||||
|
||||
PARAMS.each_pair do |key, fld|
|
||||
attr_accessor key
|
||||
end
|
||||
end # class PutPolicy
|
||||
|
||||
class << self
|
||||
EMPTY_ARGS = {}
|
||||
|
||||
### 生成下载授权URL
|
||||
def authorize_download_url(url, args = EMPTY_ARGS)
|
||||
### 提取AK/SK信息
|
||||
access_key = Config.settings[:access_key]
|
||||
secret_key = Config.settings[:secret_key]
|
||||
|
||||
### 授权期计算
|
||||
e = Auth.calculate_deadline(args[:expires_in], args[:deadline])
|
||||
|
||||
### URL变换:追加授权期参数
|
||||
if url.index('?').is_a?(Fixnum) then
|
||||
# 已有参数
|
||||
download_url = "#{url}&e=#{e}"
|
||||
else
|
||||
# 尚无参数
|
||||
download_url = "#{url}?e=#{e}"
|
||||
end
|
||||
|
||||
### 生成数字签名
|
||||
sign = HMAC::SHA1.new(secret_key).update(download_url).digest
|
||||
encoded_sign = Utils.urlsafe_base64_encode(sign)
|
||||
|
||||
### 生成下载授权凭证
|
||||
dntoken = "#{access_key}:#{encoded_sign}"
|
||||
|
||||
### 返回下载授权URL
|
||||
return "#{download_url}&token=#{dntoken}"
|
||||
end # authorize_download_url
|
||||
|
||||
def generate_acctoken(url, body = '')
|
||||
### 提取AK/SK信息
|
||||
access_key = Config.settings[:access_key]
|
||||
secret_key = Config.settings[:secret_key]
|
||||
|
||||
### 解析URL,生成待签名字符串
|
||||
uri = URI.parse(url)
|
||||
signing_str = uri.path
|
||||
|
||||
# 如有QueryString部分,则需要加上
|
||||
query_string = uri.query
|
||||
if query_string.is_a?(String) && !query_string.empty?
|
||||
signing_str += '?' + query_string
|
||||
end
|
||||
|
||||
# 追加换行符
|
||||
signing_str += "\n"
|
||||
|
||||
# 如果有Body,则也加上
|
||||
# (仅限于mime == "application/x-www-form-urlencoded"的情况)
|
||||
if body.is_a?(String) && !body.empty?
|
||||
signing_str += body
|
||||
end
|
||||
|
||||
### 生成数字签名
|
||||
sign = HMAC::SHA1.new(secret_key).update(signing_str).digest
|
||||
encoded_sign = Utils.urlsafe_base64_encode(sign)
|
||||
|
||||
### 生成管理授权凭证
|
||||
acctoken = "#{access_key}:#{encoded_sign}"
|
||||
|
||||
### 返回管理授权凭证
|
||||
return acctoken
|
||||
end # generate_acctoken
|
||||
|
||||
def generate_uptoken(put_policy)
|
||||
### 提取AK/SK信息
|
||||
access_key = Config.settings[:access_key]
|
||||
secret_key = Config.settings[:secret_key]
|
||||
|
||||
### 生成待签名字符串
|
||||
encoded_put_policy = Utils.urlsafe_base64_encode(put_policy.to_json)
|
||||
|
||||
### 生成数字签名
|
||||
sign = HMAC::SHA1.new(secret_key).update(encoded_put_policy).digest
|
||||
encoded_sign = Utils.urlsafe_base64_encode(sign)
|
||||
|
||||
### 生成上传授权凭证
|
||||
uptoken = "#{access_key}:#{encoded_sign}:#{encoded_put_policy}"
|
||||
|
||||
### 返回上传授权凭证
|
||||
return uptoken
|
||||
end # generate_uptoken
|
||||
end # class << self
|
||||
|
||||
end # module Auth
|
||||
end # module Qiniu
|
||||
@@ -11,7 +11,6 @@
|
||||
require 'tmpdir'
|
||||
|
||||
module Qiniu
|
||||
module RS
|
||||
module Config
|
||||
class << self
|
||||
|
||||
@@ -20,9 +19,8 @@ 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",
|
||||
:io_host => "http://iovip.qbox.me",
|
||||
:up_host => "http://up.qbox.me",
|
||||
:rs_host => "http://rs.qiniu.com",
|
||||
:up_host => "http://up.qiniu.com",
|
||||
:pub_host => "http://pu.qbox.me:10200",
|
||||
:eu_host => "http://eu.qbox.me",
|
||||
:client_id => "a75604760c4da4caaa456c0c5895c061c3065c5a",
|
||||
@@ -58,6 +56,5 @@ module Qiniu
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end # module Config
|
||||
end # module Qiniu
|
||||
@@ -1,12 +1,8 @@
|
||||
# -*- encoding: utf-8 -*-
|
||||
|
||||
module Qiniu
|
||||
module RS
|
||||
|
||||
class Exception < RuntimeError
|
||||
def to_s
|
||||
inspect
|
||||
end
|
||||
end
|
||||
|
||||
class ResponseError < Exception
|
||||
@@ -121,5 +117,4 @@ module Qiniu
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end # module Qiniu
|
||||
Executable
+4
@@ -0,0 +1,4 @@
|
||||
# -*- encoding: utf-8 -*-
|
||||
|
||||
require 'qiniu/image'
|
||||
require 'qiniu/pfop'
|
||||
Executable
+137
@@ -0,0 +1,137 @@
|
||||
# -*- encoding: utf-8 -*-
|
||||
# vim: sw=2 ts=2
|
||||
|
||||
module Qiniu
|
||||
module HTTP
|
||||
|
||||
class << self
|
||||
public
|
||||
def is_response_ok?(http_code)
|
||||
return 200 <= http_code && http_code <= 299
|
||||
end # is_response_ok?
|
||||
|
||||
def generate_query_string(params)
|
||||
if params.is_a?(Hash)
|
||||
total_param = params.map { |key, value| %Q(#{CGI.escape(key.to_s)}=#{CGI.escape(value.to_s).gsub('+', '%20')}) }
|
||||
return total_param.join("&")
|
||||
end
|
||||
|
||||
return params
|
||||
end # generate_query_string
|
||||
|
||||
def get (url, opts = {})
|
||||
### 配置请求Header
|
||||
req_headers = {
|
||||
:connection => 'close',
|
||||
:accept => '*/*',
|
||||
:user_agent => Config.settings[:user_agent]
|
||||
}
|
||||
|
||||
# 优先使用外部Header,覆盖任何特定Header
|
||||
if opts[:headers].is_a?(Hash) then
|
||||
req_headers.merge!(opts[:headers])
|
||||
end
|
||||
|
||||
### 发送请求
|
||||
response = RestClient.get(url, req_headers)
|
||||
return response.code.to_i, response.body, response.raw_headers
|
||||
rescue => e
|
||||
Log.logger.warn "#{e.message} => Qiniu::HTTP.get('#{url}')"
|
||||
if e.respond_to?(:response) && e.response.respond_to?(:code) then
|
||||
return e.response.code, e.response.body, e.response.raw_headers
|
||||
end
|
||||
return nil, nil, nil
|
||||
end # get
|
||||
|
||||
API_RESULT_MIMETYPE = 'application/json'
|
||||
|
||||
def api_get (url, opts = {})
|
||||
### 配置请求Header
|
||||
headers = {
|
||||
:accept => API_RESULT_MIMETYPE
|
||||
}
|
||||
|
||||
# 将特定Header混入外部Header中
|
||||
if opts[:headers].is_a?(Hash) then
|
||||
opts[:headers] = opts[:headers].dup.merge!(headers)
|
||||
else
|
||||
opts[:headers] = headers
|
||||
end
|
||||
|
||||
### 发送请求,然后转换返回值
|
||||
resp_code, resp_body, resp_headers = get(url, opts)
|
||||
if resp_code.nil? then
|
||||
return 0, {}, {}
|
||||
end
|
||||
|
||||
content_type = resp_headers["content-type"][0]
|
||||
if !content_type.nil? && content_type == API_RESULT_MIMETYPE then
|
||||
# 如果是JSON格式,则反序列化
|
||||
resp_body = Utils.safe_json_parse(resp_body)
|
||||
end
|
||||
|
||||
return resp_code, resp_body, resp_headers
|
||||
end # api_get
|
||||
|
||||
def post (url, req_body = nil, opts = {})
|
||||
### 配置请求Header
|
||||
req_headers = {
|
||||
:connection => 'close',
|
||||
:accept => '*/*',
|
||||
:user_agent => Config.settings[:user_agent]
|
||||
}
|
||||
|
||||
# 优先使用外部Header,覆盖任何特定Header
|
||||
if opts[:headers].is_a?(Hash) then
|
||||
req_headers.merge!(opts[:headers])
|
||||
end
|
||||
|
||||
### 发送请求
|
||||
response = RestClient.post(url, req_body, req_headers)
|
||||
return response.code.to_i, response.body, response.raw_headers
|
||||
rescue => e
|
||||
Log.logger.warn "#{e.message} => Qiniu::HTTP.post('#{url}')"
|
||||
if e.respond_to?(:response) && e.response.respond_to?(:code) then
|
||||
return e.response.code, e.response.body, e.response.raw_headers
|
||||
end
|
||||
return nil, nil, nil
|
||||
end # post
|
||||
|
||||
def api_post (url, req_body = nil, opts = {})
|
||||
### 配置请求Header
|
||||
headers = {
|
||||
:accept => API_RESULT_MIMETYPE
|
||||
}
|
||||
|
||||
# 将特定Header混入外部Header中
|
||||
if opts[:headers].is_a?(Hash) then
|
||||
opts[:headers] = opts[:headers].dup.merge!(headers)
|
||||
else
|
||||
opts[:headers] = headers
|
||||
end
|
||||
|
||||
### 发送请求,然后转换返回值
|
||||
resp_code, resp_body, resp_headers = post(url, req_body, opts)
|
||||
if resp_code.nil? then
|
||||
return 0, {}, {}
|
||||
end
|
||||
|
||||
content_type = resp_headers["content-type"][0]
|
||||
if !content_type.nil? && content_type == API_RESULT_MIMETYPE then
|
||||
# 如果是JSON格式,则反序列化
|
||||
resp_body = Utils.safe_json_parse(resp_body)
|
||||
end
|
||||
|
||||
return resp_code, resp_body, resp_headers
|
||||
end # api_post
|
||||
|
||||
def management_post (url, body = '')
|
||||
### 授权并执行管理操作
|
||||
return HTTP.api_post(url, body, {
|
||||
:headers => { 'Authorization' => 'QBox ' + Auth.generate_acctoken(url, body) }
|
||||
})
|
||||
end # management_post
|
||||
end # class << self
|
||||
|
||||
end # module HTTP
|
||||
end # module Qiniu
|
||||
@@ -1,22 +1,18 @@
|
||||
# -*- encoding: utf-8 -*-
|
||||
|
||||
module Qiniu
|
||||
module RS
|
||||
module Fop
|
||||
module Image
|
||||
class << self
|
||||
include Utils
|
||||
|
||||
def info(url)
|
||||
Utils.http_request url + '/imageInfo', nil, {:method => :get}
|
||||
end
|
||||
return HTTP.api_get(url + '?imageInfo')
|
||||
end # info
|
||||
|
||||
def exif(url)
|
||||
Utils.http_request url + '?exif', nil, {:method => :get}
|
||||
end
|
||||
|
||||
def preivew_url(url, spec)
|
||||
url + '/imagePreview/' + spec.to_s
|
||||
end
|
||||
return HTTP.api_get(url + '?exif')
|
||||
end # exif
|
||||
|
||||
def mogrify_preview_url(source_image_url, options)
|
||||
source_image_url + '?' + generate_mogrify_params_string(options)
|
||||
@@ -37,6 +33,6 @@ module Qiniu
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end # module Image
|
||||
end # module Fop
|
||||
end # module Qiniu
|
||||
@@ -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
|
||||
Executable
+128
@@ -0,0 +1,128 @@
|
||||
# -*- encoding: utf-8 -*-
|
||||
# vim: sw=2 ts=2
|
||||
|
||||
require 'qiniu/http'
|
||||
|
||||
module Qiniu
|
||||
module Storage
|
||||
class << self
|
||||
include Utils
|
||||
|
||||
public
|
||||
def buckets
|
||||
url = Config.settings[:rs_host] + '/buckets'
|
||||
return HTTP.management_post(url)
|
||||
end # buckets
|
||||
|
||||
PRIVATE_BUCKET = 0
|
||||
PUBLIC_BUCKET = 1
|
||||
|
||||
def mkbucket(bucket_name, is_public = PUBLIC_BUCKET)
|
||||
url = Config.settings[:rs_host] + '/mkbucket2/' + bucket_name + '/public/' + is_public.to_s
|
||||
return HTTP.management_post(url)
|
||||
end # mkbucket
|
||||
|
||||
def make_a_private_bucket(bucket_name)
|
||||
return mkbucket(bucket_name, PRIVATE_BUCKET)
|
||||
end # make_a_private_bucket
|
||||
|
||||
def make_a_public_bucket(bucket_name)
|
||||
return mkbucket(bucket_name, PUBLIC_BUCKET)
|
||||
end # make_a_public_bucket
|
||||
|
||||
def stat(bucket, key)
|
||||
url = Config.settings[:rs_host] + '/stat/' + encode_entry_uri(bucket, key)
|
||||
return HTTP.management_post(url)
|
||||
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)
|
||||
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
|
||||
return HTTP.management_post(url)
|
||||
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)
|
||||
url = Config.settings[:rs_host] + uri
|
||||
return HTTP.management_post(url)
|
||||
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)
|
||||
url = Config.settings[:rs_host] + uri
|
||||
return HTTP.management_post(url)
|
||||
end # move
|
||||
|
||||
def delete(bucket, key)
|
||||
url = Config.settings[:rs_host] + '/delete/' + encode_entry_uri(bucket, key)
|
||||
return HTTP.management_post(url)
|
||||
end # delete
|
||||
|
||||
def drop(bucket)
|
||||
url = Config.settings[:rs_host] + "/drop/#{bucket}"
|
||||
return HTTP.management_post(url)
|
||||
end # drop
|
||||
|
||||
def batch(command, bucket, keys)
|
||||
execs = []
|
||||
keys.each do |key|
|
||||
encoded_uri = encode_entry_uri(bucket, key)
|
||||
execs << "op=/#{command}/#{encoded_uri}"
|
||||
end
|
||||
url = Config.settings[:rs_host] + "/batch"
|
||||
return HTTP.management_post(url, execs.join("&"))
|
||||
end # batch
|
||||
|
||||
def batch_get(bucket, keys)
|
||||
batch("get", bucket, keys)
|
||||
end # batch_get
|
||||
|
||||
def batch_stat(bucket, keys)
|
||||
batch("stat", bucket, keys)
|
||||
end # batch_stat
|
||||
|
||||
def batch_copy(*args)
|
||||
_batch_cp_or_mv('copy', args)
|
||||
end # batch_copy
|
||||
|
||||
def batch_move(*args)
|
||||
_batch_cp_or_mv('move', args)
|
||||
end # batch_move
|
||||
|
||||
def batch_delete(bucket, keys)
|
||||
batch("delete", bucket, keys)
|
||||
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
|
||||
return HTTP.management_post(new_url)
|
||||
end # save_as
|
||||
|
||||
def image_mogrify_save_as(bucket, key, source_image_url, 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
|
||||
|
||||
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 # _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
|
||||
url = Config.settings[:rs_host] + "/batch"
|
||||
return HTTP.management_post(url, execs.join("&"))
|
||||
end # _batch_cp_or_mv
|
||||
end
|
||||
end # module Storage
|
||||
end # module Qiniu
|
||||
Executable
+33
@@ -0,0 +1,33 @@
|
||||
# -*- encoding: utf-8 -*-
|
||||
|
||||
module Qiniu
|
||||
module Misc
|
||||
class << self
|
||||
def set_protected(bucket, protected_mode)
|
||||
url = Config.settings[:pub_host] + %Q(/accessMode/#{bucket}/mode/#{protected_mode})
|
||||
return HTTP.management_post(url)
|
||||
end # set_protected
|
||||
|
||||
def set_separator(bucket, separator)
|
||||
encoded_separator = Utils.urlsafe_base64_encode(separator)
|
||||
url = Config.settings[:pub_host] + %Q(/separator/#{bucket}/sep/#{encoded_separator})
|
||||
return HTTP.management_post(url)
|
||||
end # set_separator
|
||||
|
||||
def set_style(bucket, name, style)
|
||||
encoded_name = Utils.urlsafe_base64_encode(name)
|
||||
encoded_style = Utils.urlsafe_base64_encode(style)
|
||||
url = Config.settings[:pub_host] + %Q(/style/#{bucket}/name/#{encoded_name}/style/#{encoded_style})
|
||||
return HTTP.management_post(url)
|
||||
end # set_style
|
||||
|
||||
def unset_style(bucket, name)
|
||||
encoded_name = Utils.urlsafe_base64_encode(name)
|
||||
url = Config.settings[:pub_host] + %Q(/unstyle/#{bucket}/name/#{encoded_name})
|
||||
return HTTP.management_post(url)
|
||||
end # unset_style
|
||||
end # class << self
|
||||
|
||||
end # module Misc
|
||||
end # module Qiniu
|
||||
|
||||
Executable
+123
@@ -0,0 +1,123 @@
|
||||
# -*- encoding: utf-8 -*-
|
||||
# vim: sw=2 ts=2
|
||||
|
||||
require 'qiniu/adt'
|
||||
require 'qiniu/http'
|
||||
|
||||
module Qiniu
|
||||
module Fop
|
||||
module Persistance
|
||||
|
||||
class PfopPolicy
|
||||
include ADT::Policy
|
||||
|
||||
private
|
||||
def initialize(bucket,
|
||||
key,
|
||||
fops,
|
||||
notify_url)
|
||||
@bucket = bucket
|
||||
@key = key
|
||||
@notify_url = notify_url
|
||||
|
||||
self.fops!(fops)
|
||||
end # initialize
|
||||
|
||||
public
|
||||
PARAMS = {
|
||||
# 字符串类型参数
|
||||
:bucket => "bucket",
|
||||
:key => "key",
|
||||
:fops => "fops",
|
||||
:notify_url => "notifyURL",
|
||||
|
||||
# 数值类型参数
|
||||
:force => "force"
|
||||
} # PARAMS
|
||||
|
||||
PARAMS.each_pair do |key, fld|
|
||||
attr_accessor key
|
||||
end
|
||||
|
||||
def params
|
||||
return PARAMS
|
||||
end # params
|
||||
|
||||
def fops! (fops)
|
||||
if fops.is_a?(Hash) then
|
||||
fops = fops.values
|
||||
end
|
||||
|
||||
if fops.is_a?(Array) then
|
||||
new_fops = []
|
||||
fops.each do |v|
|
||||
if v.is_a?(ApiSpecification) then
|
||||
new_fops.push(v.to_s)
|
||||
end
|
||||
end
|
||||
|
||||
@fops = new_fops.join(";")
|
||||
else
|
||||
@fops = fops.to_s
|
||||
end
|
||||
end # fops!
|
||||
|
||||
def force!
|
||||
@force = 1
|
||||
end # force!
|
||||
|
||||
alias :to_s :to_json
|
||||
end # class PfopPolicy
|
||||
|
||||
class << self
|
||||
|
||||
API_HOST = 'http://api.qiniu.com'
|
||||
|
||||
PFOP_URL = API_HOST + '/pfop/'
|
||||
|
||||
def pfop (args)
|
||||
### 生成fop指令串
|
||||
if args.is_a?(PfopPolicy) then
|
||||
# PfopPolicy的各个字段按固定顺序组织
|
||||
body = args.to_query_string()
|
||||
elsif args.is_a?(Hash) then
|
||||
# 无法保证固定字段顺序
|
||||
body = HTTP.generate_query_string(args)
|
||||
else
|
||||
# 由调用者保证固定字段顺序
|
||||
body = args.to_s
|
||||
end
|
||||
|
||||
### 发送请求
|
||||
return HTTP.management_post(PFOP_URL, body)
|
||||
end # pfop
|
||||
|
||||
PREFOP_URL = API_HOST + '/status/get/prefop?id='
|
||||
|
||||
def prefop (persistent_id)
|
||||
### 抽取persistentId
|
||||
if persistent_id.is_a?(Hash) then
|
||||
pid = persistent_id['persistentId']
|
||||
else
|
||||
pid = persistent_id.to_s
|
||||
end
|
||||
|
||||
### 发送请求
|
||||
url = PREFOP_URL + pid
|
||||
return HTTP.api_get(url)
|
||||
end # prefop
|
||||
|
||||
def generate_p1_url (url, fop)
|
||||
# 如果fop是ApiSpecification,则各字段按固定顺序组织,保证一致性
|
||||
# 否则由调用者保证固定字段顺序
|
||||
fop = CGI.escape(fop.to_s).gsub('+', '%20')
|
||||
|
||||
### 生成url
|
||||
return url + '?p/1/' + fop
|
||||
end # generate_pl_url
|
||||
|
||||
end # class << self
|
||||
|
||||
end # module Persistance
|
||||
end # module Fop
|
||||
end # module Qiniu
|
||||
@@ -6,109 +6,44 @@ 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'
|
||||
|
||||
module Qiniu
|
||||
module RS
|
||||
module UP
|
||||
module Storage
|
||||
|
||||
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
|
||||
end
|
||||
end # module AbstractClass
|
||||
|
||||
class ChunkProgressNotifier < AbstractClass::ChunkProgressNotifier
|
||||
def initialize(id)
|
||||
@data = UP::ProgressData.new(id)
|
||||
end
|
||||
def notify(index, progress)
|
||||
@data.set_progresses(index, progress)
|
||||
logmsg = "chunk #{progress[:offset]/Config.settings[:chunk_size]} in block #{index} successfully uploaded.\n" +
|
||||
"{ctx:#{progress[:ctx]}, offset:#{progress[:offset]}, restsize:#{progress[:restsize]}, status_code:#{progress[:status_code]}}"
|
||||
logmsg = "chunk #{progress[:offset]/Config.settings[:chunk_size]} in block #{index} successfully uploaded.\n" + progress.to_s
|
||||
Utils.debug(logmsg)
|
||||
end
|
||||
end
|
||||
end # class ChunkProgressNotifier
|
||||
|
||||
class BlockProgressNotifier < AbstractClass::BlockProgressNotifier
|
||||
def initialize(id)
|
||||
@data = UP::ProgressData.new(id)
|
||||
end
|
||||
def notify(index, checksum)
|
||||
@data.set_checksums(index, checksum)
|
||||
Utils.debug "block #{index}: {ctx: #{checksum}} successfully uploaded."
|
||||
Utils.debug "block #{index}: {checksum: #{checksum}} successfully uploaded."
|
||||
end
|
||||
end
|
||||
|
||||
class ProgressData
|
||||
def initialize(id)
|
||||
@id = id
|
||||
@tmpdir = Config.settings[:tmpdir] + File::SEPARATOR + @id
|
||||
FileUtils.mkdir_p(@tmpdir) unless Dir.exists?(@tmpdir)
|
||||
@checksum_file = @tmpdir + File::SEPARATOR + 'checksums'
|
||||
@progress_file = @tmpdir + File::SEPARATOR + 'progresses'
|
||||
end
|
||||
|
||||
def get_checksums
|
||||
File.exist?(@checksum_file) ? YAML.load_file(@checksum_file) : []
|
||||
end
|
||||
|
||||
def get_progresses
|
||||
File.exist?(@progress_file) ? YAML.load_file(@progress_file) : []
|
||||
end
|
||||
|
||||
def set_checksums(index, checksum)
|
||||
checksums = get_checksums
|
||||
checksums[index] = checksum
|
||||
File.open(@checksum_file, "w") do |f|
|
||||
YAML::dump(checksums, f)
|
||||
Utils.debug %Q(Updating tmpfile: #{@checksum_file})
|
||||
end
|
||||
end
|
||||
|
||||
def set_progresses(index, progress)
|
||||
progresses = get_progresses
|
||||
progresses[index] = progress
|
||||
File.open(@progress_file, "w") do |f|
|
||||
YAML::dump(progresses, f)
|
||||
Utils.debug %Q(Updating tmpfile: #{@progress_file})
|
||||
end
|
||||
end
|
||||
|
||||
def init_checksums(checksums)
|
||||
File.open(@checksum_file, "w") do |f|
|
||||
YAML::dump(checksums, f)
|
||||
Utils.debug %Q(Initializing tmpfile: #{@checksum_file})
|
||||
end
|
||||
end
|
||||
|
||||
def init_progresses(progresses)
|
||||
File.open(@progress_file, "w") do |f|
|
||||
YAML::dump(progresses, f)
|
||||
Utils.debug %Q(Initializing tmpfile: #{@progress_file})
|
||||
end
|
||||
end
|
||||
|
||||
def sweep!
|
||||
FileUtils.rm_r(@tmpdir)
|
||||
end
|
||||
end
|
||||
|
||||
end # class BlockProgressNotifier
|
||||
|
||||
class << self
|
||||
include Utils
|
||||
|
||||
def upload_with_token(uptoken,
|
||||
def resumable_upload_with_token(uptoken,
|
||||
local_file,
|
||||
bucket,
|
||||
key = nil,
|
||||
@@ -117,7 +52,6 @@ module Qiniu
|
||||
customer = nil,
|
||||
callback_params = nil,
|
||||
rotate = nil)
|
||||
raise NoSuchFileError, local_file unless File.exist?(local_file)
|
||||
begin
|
||||
ifile = File.open(local_file, 'rb')
|
||||
fh = FileData.new(ifile)
|
||||
@@ -127,16 +61,12 @@ module Qiniu
|
||||
mime = MIME::Types.type_for local_file
|
||||
mime_type = mime.empty? ? 'application/octet-stream' : mime[0].content_type
|
||||
end
|
||||
if fsize > Config.settings[:block_size]
|
||||
code, data = _resumable_upload(uptoken, fh, fsize, bucket, key, mime_type, custom_meta, customer, callback_params, rotate)
|
||||
else
|
||||
code, data = IO.upload_with_token(uptoken, local_file, bucket, key, mime_type, custom_meta, callback_params, true, rotate)
|
||||
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
|
||||
end # resumable_upload_with_token
|
||||
|
||||
private
|
||||
|
||||
@@ -159,41 +89,54 @@ module Qiniu
|
||||
@fh.mtime
|
||||
end
|
||||
#delegate :path, :mtime, :to => :fh
|
||||
end
|
||||
end # class FileData
|
||||
|
||||
def _new_block_put_progress_data
|
||||
{:ctx => nil, :offset => 0, :restsize => nil, :status_code => nil}
|
||||
end
|
||||
{: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, retry_times = 0)
|
||||
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
|
||||
:headers => {
|
||||
:content_type => 'application/octet-stream',
|
||||
'Authorization' => 'UpToken ' + uptoken
|
||||
}
|
||||
}
|
||||
code, data = http_request url, data, options
|
||||
unless Utils.is_response_ok?(code)
|
||||
if !content_type.nil? && !content_type.empty? then
|
||||
options[:headers][:content_type] = content_type
|
||||
end
|
||||
|
||||
code, data, raw_headers = HTTP.api_post(url, data, options)
|
||||
unless HTTP.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, retry_times)
|
||||
return _call_binary_with_token(uptoken, url, data, options[:content_type], retry_times)
|
||||
end
|
||||
end
|
||||
[code, data]
|
||||
end
|
||||
return code, data, raw_headers
|
||||
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
|
||||
end # _mkblock
|
||||
|
||||
def _putblock(uptoken, ctx, offset, body)
|
||||
url = Config.settings[:up_host] + "/bput/#{ctx}/#{offset}"
|
||||
def _putblock(uphost, uptoken, ctx, offset, body)
|
||||
url = uphost + "/bput/#{ctx}/#{offset}"
|
||||
_call_binary_with_token(uptoken, url, body)
|
||||
end
|
||||
end # _putblock
|
||||
|
||||
def _resumable_put_block(uptoken, fh, block_index, block_size, chunk_size, progress, retry_times = 1, notifier)
|
||||
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
|
||||
@@ -207,24 +150,30 @@ module Qiniu
|
||||
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)
|
||||
|
||||
code, data, raw_headers = _mkblock(uptoken, block_size, body)
|
||||
Utils.debug "Mkblk : #{code.inspect} #{data.inspect} #{raw_headers.inspect}"
|
||||
|
||||
body_crc32 = Zlib.crc32(body)
|
||||
if Utils.is_response_ok?(code) && data["crc32"] == body_crc32
|
||||
if HTTP.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"]})
|
||||
return code, data, raw_headers
|
||||
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
|
||||
@@ -236,31 +185,41 @@ module Qiniu
|
||||
if result_length != body_length
|
||||
raise FileSeekReadError.new(fpath, block_index, seek_pos, body_length, result_length)
|
||||
end
|
||||
code, data = _putblock(uptoken, progress[:ctx], progress[:offset], body)
|
||||
|
||||
code, data, raw_headers = _putblock(progress[:host], uptoken, progress[:ctx], progress[:offset], body)
|
||||
Utils.debug "Bput : #{code.inspect} #{data.inspect} #{raw_headers.inspect}"
|
||||
|
||||
body_crc32 = Zlib.crc32(body)
|
||||
if Utils.is_response_ok?(code) && data["crc32"] == body_crc32
|
||||
if HTTP.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"]})
|
||||
return code, data, raw_headers
|
||||
end
|
||||
end
|
||||
end
|
||||
# return
|
||||
return [code, data]
|
||||
end
|
||||
return code, data, raw_headers
|
||||
end # _resumable_put_block
|
||||
|
||||
def _block_count(fsize)
|
||||
((fsize + Config.settings[:block_size] - 1) / Config.settings[:block_size]).to_i
|
||||
end
|
||||
end # _block_count
|
||||
|
||||
def _resumable_put(uptoken, fh, checksums, progresses, block_notifier = nil, chunk_notifier = nil)
|
||||
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)
|
||||
@@ -278,9 +237,12 @@ 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)
|
||||
if Utils.is_response_ok?(code)
|
||||
checksums[block_index] = data["checksum"]
|
||||
#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 HTTP.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
|
||||
@@ -288,52 +250,70 @@ module Qiniu
|
||||
end
|
||||
end
|
||||
return [code, data]
|
||||
end
|
||||
end # _resumable_put
|
||||
|
||||
def _mkfile(uptoken, entry_uri, fsize, checksums, mime_type = nil, custom_meta = nil, customer = nil, callback_params = nil, rotate = nil)
|
||||
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?
|
||||
callback_query_string = HTTP.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 = Config.settings[:up_host] + path
|
||||
body = ''
|
||||
checksums.each do |checksum|
|
||||
body += Utils.urlsafe_base64_decode(checksum)
|
||||
end
|
||||
_call_binary_with_token(uptoken, url, body)
|
||||
end
|
||||
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)
|
||||
|
||||
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)
|
||||
progress_data = ProgressData.new(key)
|
||||
checksums = progress_data.get_checksums
|
||||
progresses = progress_data.get_progresses
|
||||
if checksums.empty?
|
||||
block_count.times{checksums << ''}
|
||||
progress_data.init_checksums(checksums)
|
||||
end
|
||||
if progresses.empty?
|
||||
block_count.times{progresses << _new_block_put_progress_data}
|
||||
progress_data.init_progresses(progresses)
|
||||
end
|
||||
chunk_notifier = ChunkProgressNotifier.new(key)
|
||||
block_notifier = BlockProgressNotifier.new(key)
|
||||
code, data = _resumable_put(uptoken, fh, checksums, progresses, block_notifier, chunk_notifier)
|
||||
if Utils.is_response_ok?(code)
|
||||
entry_uri = bucket + ':' + key
|
||||
code, data = _mkfile(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."
|
||||
progress_data.sweep!
|
||||
end
|
||||
[code, data]
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
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, raw_headers = _resumable_put(uptoken, fh, checksums, progresses, block_notifier, chunk_notifier)
|
||||
|
||||
if HTTP.is_response_ok?(code)
|
||||
uphost = data["host"]
|
||||
entry_uri = bucket + ':' + key
|
||||
code, data, raw_headers = _mkfile(uphost, uptoken, entry_uri, fsize, checksums, mime_type, custom_meta, customer, callback_params, rotate)
|
||||
Utils.debug "Mkfile : #{code.inspect} #{data.inspect} #{raw_headers.inspect}"
|
||||
end
|
||||
|
||||
if HTTP.is_response_ok?(code)
|
||||
Utils.debug "File #{fh.path} {size: #{fsize}} successfully uploaded."
|
||||
end
|
||||
|
||||
return code, data, raw_headers
|
||||
end # _resumable_upload
|
||||
end # self class
|
||||
end # module Storage
|
||||
end # module Qiniu
|
||||
@@ -1,89 +0,0 @@
|
||||
# -*- encoding: utf-8 -*-
|
||||
|
||||
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]
|
||||
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]
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -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
|
||||
|
||||
@@ -1,64 +0,0 @@
|
||||
# -*- encoding: utf-8 -*-
|
||||
|
||||
require 'mime/types'
|
||||
require 'digest/sha1'
|
||||
require 'qiniu/rs/exceptions'
|
||||
|
||||
module Qiniu
|
||||
module RS
|
||||
module IO
|
||||
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
|
||||
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
|
||||
|
||||
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
|
||||
end
|
||||
end
|
||||
@@ -1,37 +0,0 @@
|
||||
# -*- encoding: utf-8 -*-
|
||||
|
||||
module Qiniu
|
||||
module RS
|
||||
module Pub
|
||||
class << self
|
||||
include Utils
|
||||
|
||||
def set_protected(bucket, protected_mode)
|
||||
host = Config.settings[:pub_host]
|
||||
Auth.request %Q(#{host}/accessMode/#{bucket}/mode/#{protected_mode})
|
||||
end
|
||||
|
||||
def set_separator(bucket, separator)
|
||||
host = Config.settings[:pub_host]
|
||||
encoded_separator = Utils.urlsafe_base64_encode(separator)
|
||||
Auth.request %Q(#{host}/separator/#{bucket}/sep/#{encoded_separator})
|
||||
end
|
||||
|
||||
def set_style(bucket, name, style)
|
||||
host = Config.settings[:pub_host]
|
||||
encoded_name = Utils.urlsafe_base64_encode(name)
|
||||
encoded_style = Utils.urlsafe_base64_encode(style)
|
||||
Auth.request %Q(#{host}/style/#{bucket}/name/#{encoded_name}/style/#{encoded_style})
|
||||
end
|
||||
|
||||
def unset_style(bucket, name)
|
||||
host = Config.settings[:pub_host]
|
||||
encoded_name = Utils.urlsafe_base64_encode(name)
|
||||
Auth.request %Q(#{host}/unstyle/#{bucket}/name/#{encoded_name})
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,83 +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 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_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
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Executable
+5
@@ -0,0 +1,5 @@
|
||||
# -*- encoding: utf-8 -*-
|
||||
|
||||
require 'qiniu/upload'
|
||||
require 'qiniu/resumable_upload'
|
||||
require 'qiniu/management'
|
||||
@@ -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
|
||||
|
||||
Executable
+31
@@ -0,0 +1,31 @@
|
||||
# -*- encoding: utf-8 -*-
|
||||
|
||||
require 'json'
|
||||
require 'qiniu/tokens/access_token'
|
||||
require 'qiniu/utils'
|
||||
|
||||
module Qiniu
|
||||
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 # moidule DownloadToken
|
||||
end # module Qiniu
|
||||
@@ -5,9 +5,10 @@ require 'json'
|
||||
require 'qiniu/tokens/access_token'
|
||||
|
||||
module Qiniu
|
||||
module RS
|
||||
class QboxToken < AccessToken
|
||||
|
||||
include Utils
|
||||
|
||||
attr_accessor :url, :params
|
||||
|
||||
def initialize(opts = {})
|
||||
@@ -20,19 +21,18 @@ 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 = HTTP.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
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
end # module QboxToken
|
||||
end # module Qiniu
|
||||
|
||||
@@ -2,32 +2,39 @@
|
||||
|
||||
require 'json'
|
||||
require 'qiniu/tokens/access_token'
|
||||
require 'qiniu/rs/utils'
|
||||
require 'qiniu/utils'
|
||||
|
||||
module Qiniu
|
||||
module RS
|
||||
class UploadToken < AccessToken
|
||||
|
||||
include Utils
|
||||
|
||||
attr_accessor :scope, :expires_in, :callback_url, :callback_body_type, :customer, :escape
|
||||
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?
|
||||
params[:escape] = 1 if @escape == 1 || @escape == true
|
||||
urlsafe_base64_encode(params.to_json)
|
||||
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
|
||||
@@ -36,6 +43,5 @@ module Qiniu
|
||||
%Q(#{@access_key}:#{encoded_digest}:#{signature})
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
end # module UploadToken
|
||||
end # module Qiniu
|
||||
|
||||
Executable
+125
@@ -0,0 +1,125 @@
|
||||
# -*- encoding: utf-8 -*-
|
||||
# vim: sw=2 ts=2
|
||||
|
||||
module Qiniu
|
||||
module Storage
|
||||
class << self
|
||||
include Utils
|
||||
|
||||
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 = HTTP.generate_query_string(callback_params)
|
||||
|
||||
url = Config.settings[:up_host] + '/upload'
|
||||
post_data = {
|
||||
:params => callback_query_string,
|
||||
:action => action_params,
|
||||
:file => File.new(local_file, 'rb'),
|
||||
:multipart => true
|
||||
}
|
||||
if !uptoken.nil? then
|
||||
post_data[:auth] = uptoken unless uptoken.nil?
|
||||
end
|
||||
|
||||
return HTTP.api_post(url, post_data)
|
||||
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
|
||||
|
||||
### 发送请求
|
||||
HTTP.api_post(url, post_data)
|
||||
end # upload_with_token_2
|
||||
|
||||
### 授权举例
|
||||
# put_policy.bucket | put_policy.key | key | 语义 | 授权
|
||||
# :---------------- | :------------- | :------ | :--- | :---
|
||||
# trivial_bucket | <nil> | <nil> | 新增 | 允许,最终key为1)使用put_policy.save_key生成的值或2)资源内容的Hash值
|
||||
# trivial_bucket | <nil> | foo.txt | 新增 | 允许
|
||||
# trivial_bucket | <nil> | bar.jpg | 新增 | 允许
|
||||
# trivial_bucket | foo.txt | <nil> | 覆盖 | 允许,由SDK将put_policy.key赋值给key实现
|
||||
# trivial_bucket | foo.txt | foo.txt | 覆盖 | 允许
|
||||
# trivial_bucket | foo.txt | bar.jpg | 覆盖 | 禁止,put_policy.key与key不一致
|
||||
def upload_with_put_policy(put_policy,
|
||||
local_file,
|
||||
key = nil,
|
||||
x_vars = nil)
|
||||
uptoken = Auth.generate_uptoken(put_policy)
|
||||
if key.nil? then
|
||||
key = put_policy.key
|
||||
end
|
||||
|
||||
return upload_with_token_2(uptoken, local_file, key, x_vars)
|
||||
end # upload_with_put_policy
|
||||
|
||||
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
|
||||
@@ -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
|
||||
@@ -32,20 +31,13 @@ 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]
|
||||
@@ -70,16 +62,18 @@ module Qiniu
|
||||
response = RestClient.post(url, data, header_options)
|
||||
end
|
||||
code = response.respond_to?(:code) ? response.code.to_i : 0
|
||||
unless is_response_ok?(code)
|
||||
unless HTTP.is_response_ok?(code)
|
||||
raise RequestFailed.new("Request Failed", response)
|
||||
else
|
||||
data = {}
|
||||
body = response.respond_to?(:body) ? response.body : {}
|
||||
raw_headers = response.respond_to?(:raw_headers) ? response.raw_headers : {}
|
||||
data = safe_json_parse(body) unless body.empty?
|
||||
end
|
||||
[code, data]
|
||||
end
|
||||
[code, data, raw_headers]
|
||||
end # send_request_with
|
||||
|
||||
### 已过时,仅作为兼容接口保留
|
||||
def http_request url, data = nil, options = {}
|
||||
retry_times = 0
|
||||
begin
|
||||
@@ -100,51 +94,16 @@ module Qiniu
|
||||
res = e.response
|
||||
code = res.code.to_i if res.respond_to? :code
|
||||
body = res.respond_to?(:body) ? res.body : ""
|
||||
raw_headers = res.respond_to?(:raw_headers) ? res.raw_headers : {}
|
||||
data = safe_json_parse(body) unless body.empty?
|
||||
end
|
||||
[code, data]
|
||||
[code, data, raw_headers]
|
||||
end
|
||||
end
|
||||
|
||||
def upload_multipart_data(url, filepath, action_string, callback_query_string = '', uptoken = nil)
|
||||
post_data = {
|
||||
:params => callback_query_string,
|
||||
:action => action_string,
|
||||
:file => File.new(filepath, 'rb'),
|
||||
:multipart => true
|
||||
}
|
||||
post_data[:auth] = uptoken unless uptoken.nil?
|
||||
http_request url, post_data
|
||||
end
|
||||
|
||||
def generate_query_string(params)
|
||||
return params if params.is_a?(String)
|
||||
total_param = params.map { |key, value| %Q(#{CGI.escape(key.to_s)}=#{CGI.escape(value.to_s).gsub('+', '%20')}) }
|
||||
total_param.join("&")
|
||||
end
|
||||
|
||||
def crc32checksum(filepath)
|
||||
File.open(filepath, "rb") { |f| Zlib.crc32 f.read }
|
||||
end
|
||||
|
||||
def generate_qbox_signature(url, params)
|
||||
access_key = Config.settings[:access_key]
|
||||
secret_key = Config.settings[:secret_key]
|
||||
uri = URI.parse(url)
|
||||
signature = uri.path
|
||||
query_string = uri.query
|
||||
signature += '?' + query_string if !query_string.nil? && !query_string.empty?
|
||||
signature += "\n"
|
||||
if params.is_a?(Hash)
|
||||
params_string = generate_query_string(params)
|
||||
signature += params_string
|
||||
end
|
||||
hmac = HMAC::SHA1.new(secret_key)
|
||||
hmac.update(signature)
|
||||
encoded_digest = urlsafe_base64_encode(hmac.digest)
|
||||
%Q(#{access_key}:#{encoded_digest})
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
end # module Utils
|
||||
end # module Qiniu
|
||||
@@ -1,9 +1,8 @@
|
||||
# -*- encoding: utf-8 -*-
|
||||
|
||||
module Qiniu
|
||||
module RS
|
||||
module Version
|
||||
MAJOR = 3
|
||||
MAJOR = 6
|
||||
MINOR = 2
|
||||
PATCH = 1
|
||||
# Returns a version string by joining <tt>MAJOR</tt>, <tt>MINOR</tt>, and <tt>PATCH</tt> with <tt>'.'</tt>
|
||||
@@ -14,6 +13,5 @@ module Qiniu
|
||||
def self.to_s
|
||||
[MAJOR, MINOR, PATCH].join('.')
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end # Version
|
||||
end # module Qiniu
|
||||
@@ -1,24 +1,25 @@
|
||||
# -*- 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"]
|
||||
gem.email = ["why404@gmail.com"]
|
||||
gem.description = %q{Qiniu Resource (Cloud) Storage SDK for Ruby. See: http://docs.qiniutek.com/v3/sdk/ruby/}
|
||||
gem.authors = ["why404","BluntBlade"]
|
||||
gem.email = ["sdk@qiniu.com"]
|
||||
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::RS::Version.to_s
|
||||
gem.version = Qiniu::Version.to_s
|
||||
gem.license = "MIT"
|
||||
|
||||
# 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"
|
||||
@@ -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
|
||||
Executable
+75
@@ -0,0 +1,75 @@
|
||||
# -*- encoding: utf-8 -*-
|
||||
|
||||
require 'spec_helper'
|
||||
require 'qiniu/auth'
|
||||
require 'qiniu/storage'
|
||||
require 'digest/sha1'
|
||||
|
||||
module Qiniu
|
||||
module Auth
|
||||
describe Auth do
|
||||
|
||||
before :all do
|
||||
@bucket = 'RubySDK-Test-Private'
|
||||
@bucket = make_unique_bucket(@bucket)
|
||||
|
||||
### 尝试创建Bucket
|
||||
result = Qiniu::Storage.make_a_private_bucket(@bucket)
|
||||
puts result.inspect
|
||||
end
|
||||
|
||||
after :all do
|
||||
### 不删除Bucket以备下次使用
|
||||
end
|
||||
|
||||
### 测试私有资源下载
|
||||
context ".download_private_file" do
|
||||
it "should works" do
|
||||
### 生成Key
|
||||
key = 'a_private_file'
|
||||
key = make_unique_key_in_bucket(key)
|
||||
puts "key=#{key}"
|
||||
|
||||
### 上传测试文件
|
||||
pp = Auth::PutPolicy.new(@bucket, key)
|
||||
code, data, raw_headers = Qiniu::Storage.upload_with_put_policy(
|
||||
pp,
|
||||
__FILE__
|
||||
)
|
||||
code.should == 200
|
||||
puts data.inspect
|
||||
puts raw_headers.inspect
|
||||
|
||||
### 获取下载地址
|
||||
code, data = Qiniu::Storage.get(@bucket, key)
|
||||
code.should == 200
|
||||
puts data.inspect
|
||||
|
||||
url = data['url']
|
||||
|
||||
### 授权下载地址(不带参数)
|
||||
download_url = Qiniu::Auth.authorize_download_url(url)
|
||||
puts "download_url=#{download_url}"
|
||||
|
||||
result = RestClient.get(download_url)
|
||||
result.code.should == 200
|
||||
result.body.should_not be_empty
|
||||
|
||||
### 授权下载地址(带参数)
|
||||
download_url = Qiniu::Auth.authorize_download_url(url + '?download/a.m3u8')
|
||||
puts "download_url=#{download_url}"
|
||||
|
||||
result = RestClient.get(download_url)
|
||||
result.code.should == 200
|
||||
result.body.should_not be_empty
|
||||
|
||||
### 删除文件
|
||||
code, data = Qiniu::Storage.delete(@bucket, key)
|
||||
code.should == 200
|
||||
puts data.inspect
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end # module Storage
|
||||
end # module Qiniu
|
||||
|
Before Width: | Height: | Size: 13 KiB After Width: | Height: | Size: 13 KiB |
Executable
+89
@@ -0,0 +1,89 @@
|
||||
# -*- encoding: utf-8 -*-
|
||||
|
||||
require 'spec_helper'
|
||||
require 'qiniu/auth'
|
||||
require 'qiniu'
|
||||
require 'qiniu/fop'
|
||||
|
||||
module Qiniu
|
||||
module Fop
|
||||
describe Fop do
|
||||
|
||||
before :all do
|
||||
|
||||
### 复用RubySDK-Test-Storage空间
|
||||
@bucket = 'RubySDK-Test-Storage'
|
||||
@bucket = make_unique_bucket(@bucket)
|
||||
|
||||
### 尝试创建空间
|
||||
result = Qiniu.mkbucket(@bucket)
|
||||
puts result.inspect
|
||||
|
||||
pic_fname = "image_logo_for_test.png"
|
||||
@key = make_unique_key_in_bucket(pic_fname)
|
||||
|
||||
local_file = File.expand_path('../' + pic_fname, __FILE__)
|
||||
|
||||
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.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
|
||||
data["hash"].should_not be_empty
|
||||
data["width"].should_not be_zero
|
||||
data["height"].should_not be_zero
|
||||
|
||||
result = Qiniu.get(@bucket, @key)
|
||||
result["url"].should_not be_empty
|
||||
puts result.inspect
|
||||
@source_image_url = result["url"]
|
||||
|
||||
@mogrify_options = {
|
||||
:thumbnail => "!120x120>",
|
||||
:gravity => "center",
|
||||
:crop => "!120x120a0a0",
|
||||
:quality => 85,
|
||||
:rotate => 45,
|
||||
:format => "jpg",
|
||||
:auto_orient => true
|
||||
}
|
||||
end
|
||||
|
||||
after :all do
|
||||
### 不删除Bucket以备下次使用
|
||||
end
|
||||
|
||||
context ".info" do
|
||||
it "should works" do
|
||||
code, data = Qiniu::Fop::Image.info(@source_image_url)
|
||||
code.should == 200
|
||||
puts data.inspect
|
||||
end
|
||||
end
|
||||
|
||||
=begin
|
||||
context ".exif" do
|
||||
it "should works" do
|
||||
code, data = Qiniu::Fop::Image.exif(@source_image_url)
|
||||
puts data.inspect
|
||||
end
|
||||
end
|
||||
=end
|
||||
|
||||
context ".mogrify_preview_url" do
|
||||
it "should works" do
|
||||
mogrify_preview_url = Qiniu::Fop::Image.mogrify_preview_url(@source_image_url, @mogrify_options)
|
||||
puts mogrify_preview_url.inspect
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end # module Fop
|
||||
end # module Qiniu
|
||||
Executable
+156
@@ -0,0 +1,156 @@
|
||||
# -*- encoding: utf-8 -*-
|
||||
# vim: sw=2 ts=2
|
||||
|
||||
require 'digest/sha1'
|
||||
require 'spec_helper'
|
||||
require 'qiniu/auth'
|
||||
require 'qiniu/management'
|
||||
require 'qiniu'
|
||||
|
||||
module Qiniu
|
||||
module Storage
|
||||
describe Storage do
|
||||
|
||||
before :all do
|
||||
@bucket = 'RubySDK-Test-Management'
|
||||
@bucket = make_unique_bucket(@bucket)
|
||||
|
||||
### 尝试创建Bucket
|
||||
result = Qiniu.mkbucket(@bucket)
|
||||
puts result.inspect
|
||||
|
||||
@key = Digest::SHA1.hexdigest((Time.now.to_i+rand(100)).to_s)
|
||||
@key = make_unique_key_in_bucket(@key)
|
||||
|
||||
@key2 = @key + rand(100).to_s
|
||||
end
|
||||
|
||||
after :all do
|
||||
### 不删除Bucket以备下次使用
|
||||
end
|
||||
|
||||
### 准备数据
|
||||
context ".prepare_file" do
|
||||
it "should works" do
|
||||
pp = Auth::PutPolicy.new(@bucket, @key)
|
||||
code, data, raw_headers = Qiniu::Storage.upload_with_put_policy(
|
||||
pp,
|
||||
__FILE__
|
||||
)
|
||||
code.should == 200
|
||||
puts data.inspect
|
||||
end
|
||||
end
|
||||
|
||||
### 列举Bucket
|
||||
context ".buckets" do
|
||||
it "should works" do
|
||||
code, data = Storage.buckets
|
||||
code.should == 200
|
||||
puts data.inspect
|
||||
end
|
||||
end
|
||||
|
||||
context ".stat" do
|
||||
it "should works" do
|
||||
code, data = Storage.stat(@bucket, @key)
|
||||
code.should == 200
|
||||
puts data.inspect
|
||||
end
|
||||
end
|
||||
|
||||
context ".get" do
|
||||
it "should works" do
|
||||
code, data = Storage.get(@bucket, @key)
|
||||
code.should == 200
|
||||
puts data.inspect
|
||||
end
|
||||
end
|
||||
|
||||
context ".batch" do
|
||||
it "should works" do
|
||||
code, data = Storage.batch("stat", @bucket, [@key])
|
||||
code.should == 200
|
||||
puts data.inspect
|
||||
end
|
||||
end
|
||||
|
||||
context ".batch_stat" do
|
||||
it "should works" do
|
||||
code, data = Storage.batch_stat(@bucket, [@key])
|
||||
code.should == 200
|
||||
puts data.inspect
|
||||
end
|
||||
end
|
||||
|
||||
context ".batch_get" do
|
||||
it "should works" do
|
||||
code, data = Storage.batch_get(@bucket, [@key])
|
||||
code.should == 200
|
||||
puts data.inspect
|
||||
end
|
||||
end
|
||||
|
||||
context ".batch_copy" do
|
||||
it "should works" do
|
||||
code, data = Storage.batch_copy @bucket, @key, @bucket, @key2
|
||||
code.should == 200
|
||||
puts data.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 = Storage.batch_move @bucket, @key, @bucket, @key2
|
||||
code.should == 200
|
||||
puts data.inspect
|
||||
|
||||
code3, data3 = Storage.batch_move @bucket, @key2, @bucket, @key
|
||||
code3.should == 200
|
||||
puts data3.inspect
|
||||
end
|
||||
end
|
||||
|
||||
context ".move" do
|
||||
it "should works" do
|
||||
code, data = Storage.move(@bucket, @key, @bucket, @key2)
|
||||
code.should == 200
|
||||
puts data.inspect
|
||||
|
||||
code2, data2 = Storage.stat(@bucket, @key2)
|
||||
code2.should == 200
|
||||
puts data2.inspect
|
||||
|
||||
code3, data3 = Storage.move(@bucket, @key2, @bucket, @key)
|
||||
code3.should == 200
|
||||
puts data3.inspect
|
||||
end
|
||||
end
|
||||
|
||||
context ".copy" do
|
||||
it "should works" do
|
||||
code, data = Storage.copy(@bucket, @key, @bucket, @key2)
|
||||
code.should == 200
|
||||
puts data.inspect
|
||||
|
||||
code, data = Storage.delete(@bucket, @key2)
|
||||
code.should == 200
|
||||
puts data.inspect
|
||||
end
|
||||
end
|
||||
|
||||
context ".delete" do
|
||||
it "should works" do
|
||||
code, data = Storage.delete(@bucket, @key)
|
||||
code.should == 200
|
||||
puts data.inspect
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end # module Storage
|
||||
end # module Qiniu
|
||||
@@ -1,31 +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/misc'
|
||||
|
||||
module Qiniu
|
||||
module RS
|
||||
describe Pub do
|
||||
module Misc
|
||||
describe Misc do
|
||||
|
||||
before :all do
|
||||
@bucket = "pub_test_bucket"
|
||||
result = Qiniu::RS.mkbucket(@bucket)
|
||||
### 复用RubySDK-Test-Management空间
|
||||
@bucket = 'RubySDK-Test-Management'
|
||||
@bucket = make_unique_bucket(@bucket)
|
||||
|
||||
result = Qiniu.mkbucket(@bucket)
|
||||
puts result.inspect
|
||||
result.should_not be_false
|
||||
end
|
||||
|
||||
after :all do
|
||||
@bucket = "pub_test_bucket"
|
||||
result = Qiniu::RS.drop(@bucket)
|
||||
puts result.inspect
|
||||
result.should_not be_false
|
||||
### 不删除Bucket以备下次使用
|
||||
end
|
||||
|
||||
context ".set_protected" do
|
||||
it "should works" do
|
||||
code, data = Qiniu::RS::Pub.set_protected(@bucket, 1)
|
||||
code, data = Qiniu::Misc.set_protected(@bucket, 1)
|
||||
code.should == 200
|
||||
puts data.inspect
|
||||
end
|
||||
@@ -33,7 +32,7 @@ module Qiniu
|
||||
|
||||
context ".set_separator" do
|
||||
it "should works" do
|
||||
code, data = Qiniu::RS::Pub.set_separator(@bucket, "-")
|
||||
code, data = Qiniu::Misc.set_separator(@bucket, "-")
|
||||
code.should == 200
|
||||
puts data.inspect
|
||||
end
|
||||
@@ -41,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::Misc.set_style(@bucket, "small.jpg", "imageMogr/auto-orient/thumbnail/!120x120r/gravity/center/crop/!120x120/quality/80")
|
||||
code.should == 200
|
||||
puts data.inspect
|
||||
end
|
||||
@@ -49,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::Misc.unset_style(@bucket, "small.jpg")
|
||||
code.should == 200
|
||||
puts data.inspect
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
end # module Misc
|
||||
end # module Qiniu
|
||||
@@ -0,0 +1,89 @@
|
||||
# -*- encoding: utf-8 -*-
|
||||
# vim: sw=2 ts=2
|
||||
|
||||
require 'spec_helper'
|
||||
require 'qiniu/auth'
|
||||
require 'qiniu'
|
||||
require 'qiniu/fop'
|
||||
|
||||
module Qiniu
|
||||
module Fop
|
||||
module Persistance
|
||||
describe Persistance do
|
||||
|
||||
before :all do
|
||||
### 复用RubySDK-Test-Storage空间
|
||||
@bucket = 'RubySDK-Test-Storage'
|
||||
@bucket = make_unique_bucket(@bucket)
|
||||
|
||||
### 尝试创建空间
|
||||
code, data, headers = Qiniu::Storage.make_a_public_bucket(@bucket)
|
||||
puts code.inspect
|
||||
puts data.inspect
|
||||
puts headers.inspect
|
||||
|
||||
pic_fname = "image_logo_for_test.png"
|
||||
@key = make_unique_key_in_bucket(pic_fname)
|
||||
|
||||
local_file = File.expand_path('../' + pic_fname, __FILE__)
|
||||
|
||||
### 检查测试文件存在性
|
||||
code, body, headers = Qiniu::Storage.stat(@bucket, @key)
|
||||
if code == 404 || code == 612 then
|
||||
# 文件不存在,尝试上传
|
||||
pp = Qiniu::Auth.PutPolicy.new(@bucket, @key)
|
||||
code, body, headers = Qiniu::Storage.upload_with_put_policy(
|
||||
pp,
|
||||
local_file
|
||||
)
|
||||
puts "Put a test file for Persistance cases"
|
||||
puts code.inspect
|
||||
puts body.inspect
|
||||
puts header.inspect
|
||||
end
|
||||
end
|
||||
|
||||
after :all do
|
||||
### 不删除Bucket以备下次使用
|
||||
end
|
||||
|
||||
context ".pfop" do
|
||||
it "should works" do
|
||||
pp = Persistance::PfopPolicy.new(
|
||||
@bucket,
|
||||
@key,
|
||||
'imageView2/1/w/80/h/80', # fops
|
||||
'www.baidu.com' # notify_url
|
||||
)
|
||||
|
||||
code, data, headers = Qiniu::Fop::Persistance.pfop(pp)
|
||||
code.should == 200
|
||||
puts data.inspect
|
||||
end
|
||||
end
|
||||
|
||||
context ".prefop" do
|
||||
it "should works" do
|
||||
code, data, headers = Qiniu::Fop::Persistance.prefop('fakePersistentId')
|
||||
code.should == 612
|
||||
puts code.inspect
|
||||
puts data.inspect
|
||||
puts headers.inspect
|
||||
end
|
||||
end
|
||||
|
||||
context ".p1" do
|
||||
it "should works" do
|
||||
url = 'http://fake.qiniudn.com/fake.jpg'
|
||||
fop = 'imageView2/1/w/80/h/80'
|
||||
target_url = "#{url}?p/1/#{CGI.escape(fop).gsub('+', '%20')}"
|
||||
|
||||
p1_url = Qiniu::Fop::Persistance.generate_p1_url(url, fop)
|
||||
p1_url.should == target_url
|
||||
puts p1_url.inspect
|
||||
end
|
||||
end
|
||||
end
|
||||
end # module Persistance
|
||||
end # module Fop
|
||||
end # module Qiniu
|
||||
@@ -2,46 +2,44 @@
|
||||
|
||||
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 = 'qiniu_rs_test'
|
||||
@bucket = 'RubySDK-Test'
|
||||
@bucket = make_unique_bucket(@bucket)
|
||||
|
||||
@test_image_bucket = @bucket
|
||||
|
||||
### 尝试创建Bucket
|
||||
result = Qiniu.mkbucket(@bucket)
|
||||
puts result.inspect
|
||||
|
||||
@key = Digest::SHA1.hexdigest Time.now.to_s
|
||||
@domain = 'qiniu-rs-test.dn.qbox.me'
|
||||
@key = make_unique_key_in_bucket(@key)
|
||||
|
||||
result = Qiniu::RS.mkbucket(@bucket)
|
||||
result.should_not be_false
|
||||
|
||||
@test_image_bucket = 'test_images_12345'
|
||||
result2 = Qiniu::RS.mkbucket(@test_image_bucket)
|
||||
puts result2.inspect
|
||||
result2.should be_true
|
||||
@key2 = @key + rand(100).to_s
|
||||
#@domain = @bucket + '.dn.qbox.me'
|
||||
|
||||
@test_image_key = 'image_logo_for_test.png'
|
||||
local_file = File.expand_path('./rs/' + @test_image_key, File.dirname(__FILE__))
|
||||
local_file = File.expand_path('./' + @test_image_key, File.dirname(__FILE__))
|
||||
puts local_file.inspect
|
||||
upopts = {:scope => @test_image_bucket, :expires_in => 3600, :customer => "awhy.xu@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
|
||||
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::RS.drop(@bucket)
|
||||
puts result.inspect
|
||||
result.should_not be_false
|
||||
|
||||
result2 = Qiniu::RS.drop(@test_image_bucket)
|
||||
puts result2.inspect
|
||||
result2.should_not be_false
|
||||
### 不删除Bucket以备下次使用
|
||||
end
|
||||
|
||||
context ".buckets" do
|
||||
it "should works" do
|
||||
result = Qiniu::RS.buckets
|
||||
result = Qiniu.buckets
|
||||
result.should_not be_false
|
||||
puts result.inspect
|
||||
end
|
||||
@@ -49,7 +47,7 @@ module Qiniu
|
||||
|
||||
context ".set_protected" do
|
||||
it "should works" do
|
||||
result = Qiniu::RS.set_protected(@bucket, 1)
|
||||
result = Qiniu.set_protected(@bucket, 1)
|
||||
result.should_not be_false
|
||||
puts result.inspect
|
||||
end
|
||||
@@ -57,7 +55,7 @@ module Qiniu
|
||||
|
||||
context ".set_separator" do
|
||||
it "should works" do
|
||||
result = Qiniu::RS.set_separator(@bucket, "-")
|
||||
result = Qiniu.set_separator(@bucket, "-")
|
||||
result.should_not be_false
|
||||
puts result.inspect
|
||||
end
|
||||
@@ -65,7 +63,7 @@ module Qiniu
|
||||
|
||||
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 = 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
|
||||
@@ -73,92 +71,60 @@ module Qiniu
|
||||
|
||||
context ".unset_style" do
|
||||
it "should works" do
|
||||
result = Qiniu::RS.unset_style(@bucket, "small.jpg")
|
||||
result = Qiniu.unset_style(@bucket, "small.jpg")
|
||||
result.should_not be_false
|
||||
puts result.inspect
|
||||
end
|
||||
end
|
||||
|
||||
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
|
||||
|
||||
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}
|
||||
upload_opts = {
|
||||
:uptoken => Qiniu::RS.generate_upload_token(uptoken_opts),
|
||||
:uptoken => Qiniu.generate_upload_token(uptoken_opts),
|
||||
:file => __FILE__,
|
||||
:bucket => @bucket,
|
||||
:key => @key,
|
||||
:enable_crc32_check => true
|
||||
}
|
||||
result = Qiniu::RS.upload_file(upload_opts)
|
||||
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(Random.rand(9).to_s)}}
|
||||
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 => "awhy.xu@gmail.com", :escape => 0}
|
||||
uptoken = Qiniu::RS.generate_upload_token(uptoken_opts)
|
||||
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,
|
||||
@@ -167,7 +133,7 @@ module Qiniu
|
||||
:key => key
|
||||
}
|
||||
#uploading
|
||||
result1 = Qiniu::RS.upload_file(upload_opts)
|
||||
result1 = Qiniu.upload_file(upload_opts)
|
||||
#drop the bigfile
|
||||
File.unlink(localfile) if File.exists?(localfile)
|
||||
#expect
|
||||
@@ -175,11 +141,11 @@ module Qiniu
|
||||
result1.should_not be_false
|
||||
result1.should_not be_empty
|
||||
#stat
|
||||
result2 = Qiniu::RS.stat(@bucket, key)
|
||||
result2 = Qiniu.stat(@bucket, key)
|
||||
puts result2.inspect
|
||||
result2.should_not be_false
|
||||
#delete
|
||||
result3 = Qiniu::RS.delete(@bucket, key)
|
||||
result3 = Qiniu.delete(@bucket, key)
|
||||
puts result3.inspect
|
||||
result3.should_not be_false
|
||||
end
|
||||
@@ -187,7 +153,7 @@ module Qiniu
|
||||
|
||||
context ".stat" do
|
||||
it "should works" do
|
||||
result = Qiniu::RS.stat(@bucket, @key)
|
||||
result = Qiniu.stat(@bucket, @key)
|
||||
result.should_not be_false
|
||||
result.should_not be_empty
|
||||
puts result.inspect
|
||||
@@ -196,7 +162,7 @@ module Qiniu
|
||||
|
||||
context ".get" do
|
||||
it "should works" do
|
||||
result = Qiniu::RS.get(@bucket, @key, "rs_spec.rb", 10)
|
||||
result = Qiniu.get(@bucket, @key, "qiniu_spec.rb", 10)
|
||||
result.should_not be_false
|
||||
result.should_not be_empty
|
||||
puts result.inspect
|
||||
@@ -205,7 +171,7 @@ module Qiniu
|
||||
|
||||
context ".download" do
|
||||
it "should works" do
|
||||
result = Qiniu::RS.download(@bucket, @key, "rs_spec.rb", 10)
|
||||
result = Qiniu.download(@bucket, @key, "rs_spec.rb", 10)
|
||||
result.should_not be_false
|
||||
result.should_not be_empty
|
||||
puts result.inspect
|
||||
@@ -214,7 +180,7 @@ module Qiniu
|
||||
|
||||
context ".batch" do
|
||||
it "should works" do
|
||||
result = Qiniu::RS.batch("stat", @bucket, [@key])
|
||||
result = Qiniu.batch("stat", @bucket, [@key])
|
||||
result.should_not be_false
|
||||
result.should_not be_empty
|
||||
puts result.inspect
|
||||
@@ -223,7 +189,7 @@ module Qiniu
|
||||
|
||||
context ".batch_stat" do
|
||||
it "should works" do
|
||||
result = Qiniu::RS.batch_stat(@bucket, [@key])
|
||||
result = Qiniu.batch_stat(@bucket, [@key])
|
||||
result.should_not be_false
|
||||
result.should_not be_empty
|
||||
puts result.inspect
|
||||
@@ -232,7 +198,7 @@ module Qiniu
|
||||
|
||||
context ".batch_get" do
|
||||
it "should works" do
|
||||
result = Qiniu::RS.batch_get(@bucket, [@key])
|
||||
result = Qiniu.batch_get(@bucket, [@key])
|
||||
result.should_not be_false
|
||||
result.should_not be_empty
|
||||
puts result.inspect
|
||||
@@ -241,76 +207,88 @@ module Qiniu
|
||||
|
||||
context ".batch_download" do
|
||||
it "should works" do
|
||||
result = Qiniu::RS.batch_download(@bucket, [@key])
|
||||
result = Qiniu.batch_download(@bucket, [@key])
|
||||
result.should_not be_false
|
||||
result.should_not be_empty
|
||||
puts result.inspect
|
||||
end
|
||||
end
|
||||
|
||||
context ".publish" do
|
||||
=begin
|
||||
context ".batch_copy" do
|
||||
it "should works" do
|
||||
result = Qiniu::RS.publish(@domain, @bucket)
|
||||
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 ".unpublish" do
|
||||
context ".batch_move" do
|
||||
it "should works" do
|
||||
result = Qiniu::RS.unpublish(@domain)
|
||||
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
|
||||
|
||||
result3 = Qiniu.delete(@bucket, @key2)
|
||||
result3.should_not be_false
|
||||
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 = Qiniu.delete(@bucket, @key)
|
||||
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 = Qiniu.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 = Qiniu.image_info(data["url"])
|
||||
result.should_not be_false
|
||||
result.should_not be_empty
|
||||
puts result.inspect
|
||||
end
|
||||
end
|
||||
|
||||
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
|
||||
|
||||
context ".image_mogrify_save_as" do
|
||||
it "should works" do
|
||||
data = Qiniu::RS.get(@test_image_bucket, @test_image_key)
|
||||
data = Qiniu.get(@test_image_bucket, @test_image_key)
|
||||
data.should_not be_false
|
||||
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 = {
|
||||
@@ -322,7 +300,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.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
|
||||
@@ -331,11 +309,21 @@ module Qiniu
|
||||
|
||||
context ".generate_upload_token" do
|
||||
it "should works" do
|
||||
data = Qiniu::RS.generate_upload_token({:scope => 'test_bucket', :expires_in => 3600, :escape => 0})
|
||||
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
|
||||
end # module Qiniu
|
||||
@@ -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
|
||||
@@ -1,65 +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 = "awhy.xu@gmail.com"
|
||||
@bucket = "wm_test_bucket"
|
||||
@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
|
||||
@bucket = "wm_test_bucket"
|
||||
result = Qiniu::RS.drop(@bucket)
|
||||
puts result.inspect
|
||||
result.should_not be_false
|
||||
end
|
||||
|
||||
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
|
||||
@@ -1,75 +0,0 @@
|
||||
# -*- encoding: utf-8 -*-
|
||||
|
||||
require 'spec_helper'
|
||||
require 'qiniu/rs/auth'
|
||||
require 'qiniu/rs'
|
||||
require 'qiniu/rs/image'
|
||||
|
||||
module Qiniu
|
||||
module RS
|
||||
describe Image do
|
||||
|
||||
before :all do
|
||||
|
||||
@bucket = "test_images_12345"
|
||||
@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 => "awhy.xu@gmail.com"}
|
||||
uptoken = Qiniu::RS.generate_upload_token(upopts)
|
||||
data = Qiniu::RS.upload_file :uptoken => uptoken, :file => local_file, :bucket => @bucket, :key => @key
|
||||
puts data.inspect
|
||||
|
||||
result = Qiniu::RS.get(@bucket, @key)
|
||||
result["url"].should_not be_empty
|
||||
puts result.inspect
|
||||
@source_image_url = result["url"]
|
||||
|
||||
@mogrify_options = {
|
||||
:thumbnail => "!120x120>",
|
||||
:gravity => "center",
|
||||
:crop => "!120x120a0a0",
|
||||
:quality => 85,
|
||||
:rotate => 45,
|
||||
:format => "jpg",
|
||||
:auto_orient => true
|
||||
}
|
||||
end
|
||||
|
||||
after :all do
|
||||
@bucket = "test_images_12345"
|
||||
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)
|
||||
code.should == 200
|
||||
puts data.inspect
|
||||
end
|
||||
end
|
||||
|
||||
context ".exif" do
|
||||
it "should works" do
|
||||
code, data = Qiniu::RS::Image.exif(@source_image_url)
|
||||
puts data.inspect
|
||||
end
|
||||
end
|
||||
|
||||
context ".mogrify_preview_url" do
|
||||
it "should works" do
|
||||
mogrify_preview_url = Qiniu::RS::Image.mogrify_preview_url(@source_image_url, @mogrify_options)
|
||||
puts mogrify_preview_url.inspect
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,61 +0,0 @@
|
||||
# Utils.-*- encoding: utf-8 -*-
|
||||
|
||||
require 'spec_helper'
|
||||
require 'qiniu/rs/auth'
|
||||
require 'qiniu/rs/io'
|
||||
require 'digest/sha1'
|
||||
|
||||
module Qiniu
|
||||
module RS
|
||||
describe IO do
|
||||
|
||||
before :all do
|
||||
@bucket = "io_test_bucket"
|
||||
@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
|
||||
@bucket = "io_test_bucket"
|
||||
result = Qiniu::RS.drop(@bucket)
|
||||
puts result.inspect
|
||||
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)
|
||||
code.should == 200
|
||||
puts data.inspect
|
||||
end
|
||||
end
|
||||
|
||||
context ".upload_with_token" do
|
||||
it "should works" do
|
||||
upopts = {:scope => @bucket, :expires_in => 3600, :customer => "awhy.xu@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
|
||||
puts data.inspect
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,127 +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 = "rs_test_bucket"
|
||||
@key = Digest::SHA1.hexdigest (Time.now.to_i+rand(100)).to_s
|
||||
@domain = 'rs-test-bucket.dn.qbox.me'
|
||||
|
||||
code, data = Qiniu::RS::RS.mkbucket(@bucket)
|
||||
puts [code, data].inspect
|
||||
code.should == 200
|
||||
end
|
||||
|
||||
after :all do
|
||||
@bucket = "rs_test_bucket"
|
||||
code, data = Qiniu::RS::RS.drop(@bucket)
|
||||
puts [code, data].inspect
|
||||
code.should == 200
|
||||
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
|
||||
@put_url = data["url"]
|
||||
|
||||
code2, data2 = Qiniu::RS::IO.upload_file(@put_url, __FILE__, @bucket, @key)
|
||||
puts data2.inspect
|
||||
code2.should == 200
|
||||
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
|
||||
|
||||
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
|
||||
|
||||
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
|
||||
@@ -1,61 +0,0 @@
|
||||
# Utils.-*- 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| 9437184.times{f.write(Random.rand(9).to_s)}}
|
||||
@bucket = "up_test_bucket"
|
||||
@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
|
||||
@localfile = "bigfile.txt"
|
||||
File.unlink(@localfile) if File.exists?(@localfile)
|
||||
|
||||
@bucket = "up_test_bucket"
|
||||
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 => "awhy.xu@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
|
||||
@@ -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
|
||||
@@ -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
|
||||
Executable
+308
@@ -0,0 +1,308 @@
|
||||
# -*- encoding: utf-8 -*-
|
||||
# vim: sw=2 ts=2
|
||||
|
||||
require 'spec_helper'
|
||||
require 'qiniu/auth'
|
||||
require 'qiniu/storage'
|
||||
require 'digest/sha1'
|
||||
|
||||
module Qiniu
|
||||
module Storage
|
||||
describe Storage do
|
||||
|
||||
before :all do
|
||||
@bucket = 'RubySDK-Test-Storage'
|
||||
@bucket = make_unique_bucket(@bucket)
|
||||
|
||||
### 尝试创建Bucket
|
||||
result = Qiniu.mkbucket(@bucket)
|
||||
puts result.inspect
|
||||
|
||||
@key = Digest::SHA1.hexdigest((Time.now.to_i+rand(100)).to_s)
|
||||
@key = make_unique_key_in_bucket(@key)
|
||||
puts "key=#{@key}"
|
||||
|
||||
@localfile_5m = "5M.txt"
|
||||
File.open(@localfile_5m, "w"){|f| 5242888.times{ f.write(rand(9).to_s) }}
|
||||
@key_5m = Digest::SHA1.hexdigest(@localfile_5m+Time.now.to_s)
|
||||
@key_5m = make_unique_key_in_bucket(@key_5m)
|
||||
puts "key_5m=#{@key_5m}"
|
||||
|
||||
@localfile_4m = "4M.txt"
|
||||
File.open(@localfile_4m, "w"){|f| (1 << 22).times{ f.write(rand(9).to_s) }}
|
||||
@key_4m = Digest::SHA1.hexdigest(@localfile_4m+Time.now.to_s)
|
||||
@key_4m = make_unique_key_in_bucket(@key_4m)
|
||||
puts "key_4m=#{@key_4m}"
|
||||
|
||||
@localfile_8m = "8M.txt"
|
||||
File.open(@localfile_8m, "w"){|f| (1 << 23).times{ f.write(rand(9).to_s) }}
|
||||
@key_8m = Digest::SHA1.hexdigest(@localfile_8m+Time.now.to_s)
|
||||
@key_8m = make_unique_key_in_bucket(@key_8m)
|
||||
puts "key_8m=#{@key_8m}"
|
||||
|
||||
@localfile_1m = "1M.txt"
|
||||
File.open(@localfile_1m, "w"){|f| (1 << 20).times{ f.write(rand(9).to_s) }}
|
||||
@key_1m = Digest::SHA1.hexdigest(@localfile_1m+Time.now.to_s)
|
||||
@key_1m = make_unique_key_in_bucket(@key_1m)
|
||||
puts "key_1m=#{@key_1m}"
|
||||
end
|
||||
|
||||
after :all do
|
||||
### 清除本地临时文件
|
||||
File.unlink(@localfile_5m) if File.exists?(@localfile_5m)
|
||||
File.unlink(@localfile_4m) if File.exists?(@localfile_4m)
|
||||
File.unlink(@localfile_8m) if File.exists?(@localfile_8m)
|
||||
File.unlink(@localfile_1m) if File.exists?(@localfile_1m)
|
||||
|
||||
### 不删除Bucket以备下次使用
|
||||
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, raw_headers = Qiniu::Storage.upload_with_token(
|
||||
uptoken,
|
||||
__FILE__,
|
||||
@bucket,
|
||||
@key,
|
||||
nil,
|
||||
nil,
|
||||
nil,
|
||||
true
|
||||
)
|
||||
code.should == 200
|
||||
puts data.inspect
|
||||
puts raw_headers.inspect
|
||||
end
|
||||
end
|
||||
|
||||
context ".stat" do
|
||||
it "should exists" do
|
||||
code, data = Qiniu::Storage.stat(@bucket, @key)
|
||||
puts data.inspect
|
||||
code.should == 200
|
||||
end
|
||||
end
|
||||
|
||||
context ".delete" do
|
||||
it "should works" do
|
||||
code, data = Qiniu::Storage.delete(@bucket, @key)
|
||||
puts data.inspect
|
||||
code.should == 200
|
||||
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, raw_headers = Qiniu::Storage.upload_with_token_2(
|
||||
uptoken,
|
||||
__FILE__,
|
||||
@key
|
||||
)
|
||||
|
||||
code.should == 200
|
||||
puts data.inspect
|
||||
puts raw_headers.inspect
|
||||
end
|
||||
end # .upload_with_token_2
|
||||
|
||||
context ".stat" do
|
||||
it "should exists" do
|
||||
code, data = Qiniu::Storage.stat(@bucket, @key)
|
||||
puts data.inspect
|
||||
code.should == 200
|
||||
end
|
||||
end
|
||||
|
||||
context ".delete" do
|
||||
it "should works" do
|
||||
code, data = Qiniu::Storage.delete(@bucket, @key)
|
||||
puts data.inspect
|
||||
code.should == 200
|
||||
end
|
||||
end
|
||||
|
||||
context ".upload_with_put_policy" do
|
||||
it "should works" do
|
||||
pp = Qiniu::Auth::PutPolicy.new(@bucket, @key)
|
||||
pp.end_user = "why404@gmail.com"
|
||||
puts 'put_policy=' + pp.to_json
|
||||
|
||||
code, data, raw_headers = Qiniu::Storage.upload_with_put_policy(
|
||||
pp,
|
||||
__FILE__,
|
||||
@key + '-not-equal'
|
||||
)
|
||||
code.should_not == 200
|
||||
puts data.inspect
|
||||
puts raw_headers.inspect
|
||||
|
||||
code, data, raw_headers = Qiniu::Storage.upload_with_put_policy(
|
||||
pp,
|
||||
__FILE__,
|
||||
@key
|
||||
)
|
||||
|
||||
code.should == 200
|
||||
puts data.inspect
|
||||
puts raw_headers.inspect
|
||||
end
|
||||
end # .upload_with_put_policy
|
||||
|
||||
context ".stat" do
|
||||
it "should exists" do
|
||||
code, data = Qiniu::Storage.stat(@bucket, @key)
|
||||
puts data.inspect
|
||||
code.should == 200
|
||||
end
|
||||
end
|
||||
|
||||
context ".delete" do
|
||||
it "should works" do
|
||||
code, data = Qiniu::Storage.delete(@bucket, @key)
|
||||
puts data.inspect
|
||||
code.should == 200
|
||||
end
|
||||
end
|
||||
|
||||
### 测试断点续上传
|
||||
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, raw_headers = Qiniu::Storage.resumable_upload_with_token(
|
||||
uptoken,
|
||||
@localfile_5m,
|
||||
@bucket,
|
||||
@key_5m
|
||||
)
|
||||
(code/100).should == 2
|
||||
puts data.inspect
|
||||
puts raw_headers.inspect
|
||||
puts "key_5m=#{@key_5m}"
|
||||
end
|
||||
end
|
||||
|
||||
context ".stat" do
|
||||
it "should exists" do
|
||||
code, data = Qiniu::Storage.stat(@bucket, @key_5m)
|
||||
puts data.inspect
|
||||
code.should == 200
|
||||
end
|
||||
end
|
||||
|
||||
context ".delete" do
|
||||
it "should works" do
|
||||
code, data = Qiniu::Storage.delete(@bucket, @key_5m)
|
||||
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, raw_headers = Qiniu::Storage.resumable_upload_with_token(
|
||||
uptoken,
|
||||
@localfile_4m,
|
||||
@bucket,
|
||||
@key_4m
|
||||
)
|
||||
(code/100).should == 2
|
||||
puts data.inspect
|
||||
puts raw_headers.inspect
|
||||
puts "key_4m=#{@key_4m}"
|
||||
end
|
||||
end
|
||||
|
||||
context ".stat" do
|
||||
it "should exists" do
|
||||
code, data = Qiniu::Storage.stat(@bucket, @key_4m)
|
||||
puts data.inspect
|
||||
code.should == 200
|
||||
end
|
||||
end
|
||||
|
||||
context ".delete" do
|
||||
it "should works" do
|
||||
code, data = Qiniu::Storage.delete(@bucket, @key_4m)
|
||||
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, raw_headers = Qiniu::Storage.resumable_upload_with_token(
|
||||
uptoken,
|
||||
@localfile_8m,
|
||||
@bucket,
|
||||
@key_8m
|
||||
)
|
||||
(code/100).should == 2
|
||||
puts data.inspect
|
||||
puts raw_headers.inspect
|
||||
puts "key_8m=#{@key_8m}"
|
||||
end
|
||||
end
|
||||
|
||||
context ".stat" do
|
||||
it "should exists" do
|
||||
code, data = Qiniu::Storage.stat(@bucket, @key_8m)
|
||||
puts data.inspect
|
||||
code.should == 200
|
||||
end
|
||||
end
|
||||
|
||||
context ".delete" do
|
||||
it "should works" do
|
||||
code, data = Qiniu::Storage.delete(@bucket, @key_8m)
|
||||
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, raw_headers = Qiniu::Storage.resumable_upload_with_token(
|
||||
uptoken,
|
||||
@localfile_1m,
|
||||
@bucket,
|
||||
@key_1m
|
||||
)
|
||||
(code/100).should == 2
|
||||
puts data.inspect
|
||||
puts raw_headers.inspect
|
||||
puts "key_1m=#{@key_1m}"
|
||||
end
|
||||
end
|
||||
|
||||
context ".stat" do
|
||||
it "should exists" do
|
||||
code, data = Qiniu::Storage.stat(@bucket, @key_1m)
|
||||
puts data.inspect
|
||||
code.should == 200
|
||||
end
|
||||
end
|
||||
|
||||
context ".delete" do
|
||||
it "should works" do
|
||||
code, data = Qiniu::Storage.delete(@bucket, @key_1m)
|
||||
puts data.inspect
|
||||
code.should == 200
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end # module Storage
|
||||
end # module Qiniu
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
require 'spec_helper'
|
||||
require 'fakeweb'
|
||||
require 'qiniu/rs/utils'
|
||||
require 'qiniu/utils'
|
||||
|
||||
module Qiniu
|
||||
module RS
|
||||
@@ -28,7 +28,7 @@ module Qiniu
|
||||
FakeWeb.allow_net_connect = false
|
||||
FakeWeb.register_uri(:get, "http://docs.qiniutek.com/", :body => {:abc => 123}.to_json)
|
||||
res = Utils.send_request_with 'http://docs.qiniutek.com/', nil, :method => :get
|
||||
res.should == [200, {"abc" => 123}]
|
||||
res.should == [200, {"abc" => 123}, {}]
|
||||
end
|
||||
|
||||
[400, 500].each do |code|
|
||||
Executable
+10
@@ -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
|
||||
+10
-9
@@ -1,18 +1,19 @@
|
||||
# -*- 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 => "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.establish_connection! :access_key => ENV["QINIU_ACCESS_KEY"], :secret_key => ENV["QINIU_SECRET_KEY"]
|
||||
end
|
||||
end
|
||||
|
||||
def make_unique_bucket (bucket)
|
||||
bucket + "-" + ENV["QINIU_ACCESS_KEY"][0, 8]
|
||||
end # make_unique_bucket
|
||||
|
||||
def make_unique_key_in_bucket (key)
|
||||
"#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}-" + key
|
||||
end # make_unique_key_in_bucket
|
||||
|
||||
Reference in New Issue
Block a user