mirror of
https://github.com/wahyd4/ruby-sdk.git
synced 2026-08-10 05:18:37 +10:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cacdb4f926 | ||
|
|
557b75f33f | ||
|
|
2cbb175423 | ||
|
|
a8a8f63887 | ||
|
|
64cf9b5d3f | ||
|
|
1a73c39ffb | ||
|
|
864076b9ec | ||
|
|
bb6efd4e0d | ||
|
|
ef1739cb2a | ||
|
|
611837b137 | ||
|
|
000eef8870 | ||
|
|
bacf83dd3c | ||
|
|
b8ad8bd7a1 | ||
|
|
38a0877b66 | ||
|
|
241a16c40b | ||
|
|
9a5a27429d | ||
|
|
8011d6d837 | ||
|
|
2451b5b51f | ||
|
|
bd34ea428e | ||
|
|
fa40449b12 | ||
|
|
cf8a38faa0 | ||
|
|
f3dd759a5b | ||
|
|
1072b1ebab | ||
|
|
df68455eed | ||
|
|
ec58895ca7 | ||
|
|
4f9ca6e468 | ||
|
|
93a78047c1 | ||
|
|
f304a2cb67 | ||
|
|
c2a30d5a01 | ||
|
|
7432857c5a | ||
|
|
1d50e3aa54 | ||
|
|
13e1051b87 | ||
|
|
7c1fd4d207 | ||
|
|
7b628281d3 | ||
|
|
263ea64367 | ||
|
|
2dfd3da781 | ||
|
|
1af173d04f | ||
|
|
b176c7a604 | ||
|
|
9c719cc8e5 | ||
|
|
e8a1343328 | ||
|
|
88b56daabd | ||
|
|
bab0f4a602 | ||
|
|
1b9077a7dd | ||
|
|
64ddbbe831 | ||
|
|
36d5fc0684 | ||
|
|
490f359a17 | ||
|
|
01bd208627 | ||
|
|
d0c69c2255 | ||
|
|
7e4aff00a1 | ||
|
|
93ca5c5009 | ||
|
|
46144f145c | ||
|
|
b2ddd186aa | ||
|
|
9f07e8d236 | ||
|
|
4f04812b5c | ||
|
|
fcc6870f2e | ||
|
|
2bd158c1fe | ||
|
|
556210548b | ||
|
|
3b58147e70 | ||
|
|
6746a8eeeb | ||
|
|
da0d8d5ef8 | ||
|
|
1a950e8f85 | ||
|
|
d1b3f3c080 | ||
|
|
46eb6e3d58 | ||
|
|
8e86a92080 | ||
|
|
33447b0d5f | ||
|
|
e7f64fabae | ||
|
|
1d63970688 | ||
|
|
83475e594a | ||
|
|
9210c7fc32 | ||
|
|
1a9dfeb9a7 | ||
|
|
da3bdd362e | ||
|
|
2bbcfff82f | ||
|
|
b2b5b8b92d | ||
|
|
e5c57d4d72 | ||
|
|
9ff7228a8f | ||
|
|
abadfe65ba | ||
|
|
006df109a7 | ||
|
|
c4b23a4d89 | ||
|
|
02d25bf63b | ||
|
|
28d4aa7743 | ||
|
|
25fd1777f6 | ||
|
|
7df449cc73 | ||
|
|
3765e72c8a | ||
|
|
84157d35fa | ||
|
|
4a83010548 | ||
|
|
313e368602 | ||
|
|
dddbf793e5 | ||
|
|
28c71771e4 | ||
|
|
d92dc24416 | ||
|
|
1eb43cb00f | ||
|
|
b2f496551b | ||
|
|
07a01834d6 | ||
|
|
c60cc6e4b8 | ||
|
|
0a4f8c1471 | ||
|
|
9719aae614 | ||
|
|
f8c3f93d29 | ||
|
|
971fa3e92b | ||
|
|
70d6d55e0c | ||
|
|
8e90310b9c | ||
|
|
8cf75eecbd | ||
|
|
ccd46828c6 | ||
|
|
2cc53fabdd | ||
|
|
7eb27d0deb | ||
|
|
b0b568d0c7 | ||
|
|
cffec32884 | ||
|
|
0f59b15100 | ||
|
|
459c32c278 |
@@ -0,0 +1,31 @@
|
||||
## CHANGE LOG
|
||||
|
||||
### 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)
|
||||
@@ -1,4 +1,4 @@
|
||||
source 'http://ruby.taobao.org'
|
||||
source 'https://rubygems.org'
|
||||
|
||||
# Specify your gem's dependencies in qiniu-s3.gemspec
|
||||
gemspec
|
||||
|
||||
+19
-17
@@ -1,35 +1,37 @@
|
||||
PATH
|
||||
remote: .
|
||||
specs:
|
||||
qiniu-rs (1.1.0)
|
||||
json
|
||||
mime-types
|
||||
rest-client
|
||||
qiniu-rs (3.2.2)
|
||||
json (~> 1.7)
|
||||
mime-types (~> 1.19)
|
||||
rest-client (~> 1.6)
|
||||
ruby-hmac (~> 0.4)
|
||||
|
||||
GEM
|
||||
remote: https://rubygems.org/
|
||||
specs:
|
||||
diff-lcs (1.1.3)
|
||||
fakeweb (1.3.0)
|
||||
json (1.7.3)
|
||||
mime-types (1.18)
|
||||
rake (0.9.2.2)
|
||||
json (1.7.5)
|
||||
mime-types (1.19)
|
||||
rake (10.0.2)
|
||||
rest-client (1.6.7)
|
||||
mime-types (>= 1.16)
|
||||
rspec (2.10.0)
|
||||
rspec-core (~> 2.10.0)
|
||||
rspec-expectations (~> 2.10.0)
|
||||
rspec-mocks (~> 2.10.0)
|
||||
rspec-core (2.10.1)
|
||||
rspec-expectations (2.10.0)
|
||||
rspec (2.12.0)
|
||||
rspec-core (~> 2.12.0)
|
||||
rspec-expectations (~> 2.12.0)
|
||||
rspec-mocks (~> 2.12.0)
|
||||
rspec-core (2.12.1)
|
||||
rspec-expectations (2.12.0)
|
||||
diff-lcs (~> 1.1.3)
|
||||
rspec-mocks (2.10.1)
|
||||
rspec-mocks (2.12.0)
|
||||
ruby-hmac (0.4.0)
|
||||
|
||||
PLATFORMS
|
||||
ruby
|
||||
|
||||
DEPENDENCIES
|
||||
fakeweb
|
||||
fakeweb (~> 1.3)
|
||||
qiniu-rs!
|
||||
rake
|
||||
rspec
|
||||
rake (>= 0.9)
|
||||
rspec (~> 2.11)
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
# Qiniu Resource (Cloud) Storage SDK for Ruby - [](http://travis-ci.org/why404/qiniu-rs) [](https://gemnasium.com/why404/qiniu-rs)
|
||||
# Qiniu Resource (Cloud) Storage SDK for Ruby - [](http://travis-ci.org/why404/qiniu-rs-for-ruby) [](https://gemnasium.com/why404/qiniu-rs-for-ruby)
|
||||
|
||||
# 关于
|
||||
|
||||
此 Ruby SDK 适用于 Ruby 1.8.x, 1.9.x 版本,基于 [七牛云存储官方API](http://docs.qiniutek.com/v1/api/) 构建。使用此 SDK 构建您的网络应用程序,能让您以非常便捷地方式将数据安全地存储到七牛云存储上。无论您的网络应用是一个网站程序,还是包括从云端(服务端程序)到终端(手持设备应用)的架构的服务或应用,通过七牛云存储及其 SDK,都能让您应用程序的终端用户高速上传和下载,同时也让您的服务端更加轻盈。
|
||||
此 Ruby SDK 适用于 Ruby 1.8.x, 1.9.x, jruby, rbx, ree 版本,基于 [七牛云存储官方API](http://docs.qiniutek.com/v3/api/) 构建。使用此 SDK 构建您的网络应用程序,能让您以非常便捷地方式将数据安全地存储到七牛云存储上。无论您的网络应用是一个网站程序,还是包括从云端(服务端程序)到终端(手持设备应用)的架构的服务或应用,通过七牛云存储及其 SDK,都能让您应用程序的终端用户高速上传和下载,同时也让您的服务端更加轻盈。
|
||||
|
||||
## 安装
|
||||
|
||||
@@ -20,7 +20,7 @@
|
||||
|
||||
## 使用
|
||||
|
||||
参考文档:[七牛云存储 Ruby SDK 使用指南](http://docs.qiniutek.com/v1/sdk/ruby/)
|
||||
参考文档:[七牛云存储 Ruby SDK 使用指南](http://docs.qiniutek.com/v3/sdk/ruby/)
|
||||
|
||||
## 贡献代码
|
||||
|
||||
@@ -32,7 +32,7 @@
|
||||
|
||||
## 许可证
|
||||
|
||||
Copyright (c) 2012 why404
|
||||
Copyright (c) 2012 qiniutek.com
|
||||
|
||||
基于 MIT 协议发布:
|
||||
|
||||
|
||||
+1047
File diff suppressed because it is too large
Load Diff
+120
-5
@@ -1,17 +1,23 @@
|
||||
# -*- encoding: utf-8 -*-
|
||||
|
||||
require 'qiniu/rs/version'
|
||||
|
||||
module Qiniu
|
||||
module RS
|
||||
autoload :Version, 'qiniu/rs/version'
|
||||
autoload :Config, 'qiniu/rs/config'
|
||||
autoload :Log, 'qiniu/rs/log'
|
||||
autoload :Exception, 'qiniu/rs/exceptions'
|
||||
autoload :Utils, 'qiniu/rs/utils'
|
||||
autoload :Auth, 'qiniu/rs/auth'
|
||||
autoload :IO, 'qiniu/rs/io'
|
||||
autoload :UP, 'qiniu/rs/up'
|
||||
autoload :RS, 'qiniu/rs/rs'
|
||||
autoload :EU, 'qiniu/rs/eu'
|
||||
autoload :Pub, 'qiniu/rs/pub'
|
||||
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'
|
||||
|
||||
class << self
|
||||
|
||||
@@ -26,23 +32,105 @@ module Qiniu
|
||||
code == StatusOK
|
||||
end
|
||||
|
||||
def mkbucket(bucket_name)
|
||||
code, data = RS.mkbucket(bucket_name)
|
||||
code == StatusOK
|
||||
end
|
||||
|
||||
def buckets
|
||||
code, data = RS.buckets
|
||||
code == StatusOK ? data : false
|
||||
end
|
||||
|
||||
def set_protected(bucket, protected_mode)
|
||||
code, data = Pub.set_protected(bucket, protected_mode)
|
||||
code == StatusOK
|
||||
end
|
||||
|
||||
def set_separator(bucket, separator)
|
||||
code, data = Pub.set_separator(bucket, separator)
|
||||
code == StatusOK
|
||||
end
|
||||
|
||||
def set_style(bucket, name, style)
|
||||
code, data = Pub.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.put_file(opts[:url],
|
||||
opts[:file],
|
||||
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[:callback_params],
|
||||
opts[:enable_crc32_check])
|
||||
code == StatusOK
|
||||
end
|
||||
|
||||
def upload_file opts = {}
|
||||
[:uptoken, :file, :bucket, :key].each do |opt|
|
||||
raise MissingArgsError, [opt] unless opts.has_key?(opt)
|
||||
end
|
||||
source_file = opts[:file]
|
||||
raise NoSuchFileError, source_file unless File.exist?(source_file)
|
||||
opts[:enable_resumable_upload] = true unless opts.has_key?(:enable_resumable_upload)
|
||||
if opts[:enable_resumable_upload] && File::size(source_file) > Config.settings[:block_size]
|
||||
code, data = UP.upload_with_token(opts[:uptoken],
|
||||
opts[:file],
|
||||
opts[:bucket],
|
||||
opts[:key],
|
||||
opts[:mime_type],
|
||||
opts[:note],
|
||||
opts[:customer],
|
||||
opts[:callback_params],
|
||||
opts[:rotate])
|
||||
else
|
||||
code, data = IO.upload_with_token(opts[:uptoken],
|
||||
opts[:file],
|
||||
opts[:bucket],
|
||||
opts[:key],
|
||||
opts[:mime_type],
|
||||
opts[:note],
|
||||
opts[:callback_params],
|
||||
opts[:enable_crc32_check],
|
||||
opts[:rotate])
|
||||
end
|
||||
raise UploadFailedError.new(code, data) if code != StatusOK
|
||||
return data
|
||||
end
|
||||
|
||||
def stat(bucket, key)
|
||||
code, data = RS.stat(bucket, key)
|
||||
code == StatusOK ? data : false
|
||||
@@ -111,10 +199,37 @@ module Qiniu
|
||||
code == StatusOK ? data : false
|
||||
end
|
||||
|
||||
def image_exif(url)
|
||||
code, data = 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)
|
||||
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 == StatusOK ? data : false
|
||||
end
|
||||
|
||||
def generate_upload_token(opts = {})
|
||||
token_obj = UploadToken.new(opts)
|
||||
token_obj.access_key = Config.settings[:access_key]
|
||||
token_obj.secret_key = Config.settings[:secret_key]
|
||||
#token_obj.scope = opts[:scope]
|
||||
#token_obj.expires_in = opts[:expires_in]
|
||||
#token_obj.callback_url = opts[:callback_url]
|
||||
#token_obj.callback_body_type = opts[:callback_body_type]
|
||||
#token_obj.customer = opts[:customer]
|
||||
#token_obj.escape = opts[:escape]
|
||||
token_obj.generate_token
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
# -*- encoding: utf-8 -*-
|
||||
|
||||
module Qiniu
|
||||
module RS
|
||||
module Abstract
|
||||
def self.included(base)
|
||||
base.extend(ClassMethods)
|
||||
end
|
||||
|
||||
module ClassMethods
|
||||
def abstract_methods(*args)
|
||||
args.each do |name|
|
||||
class_eval <<-END
|
||||
def #{name}(*args)
|
||||
errmsg = %Q(class \#{self.class.name} must implement abstract method #{self.name}##{name}().)
|
||||
raise NotImplementedError.new(errmsg)
|
||||
end
|
||||
END
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
+14
-8
@@ -18,7 +18,7 @@ module Qiniu
|
||||
:password => password
|
||||
}
|
||||
code, data = http_request Config.settings[:auth_url], post_data
|
||||
reset_token(data["access_token"], data["refresh_token"]) if code == 200
|
||||
reset_token(data["access_token"], data["refresh_token"]) if Utils.is_response_ok?(code)
|
||||
[code, data]
|
||||
end
|
||||
|
||||
@@ -29,7 +29,7 @@ module Qiniu
|
||||
:refresh_token => refresh_token
|
||||
}
|
||||
code, data = http_request Config.settings[:auth_url], post_data
|
||||
reset_token(data["access_token"], data["refresh_token"]) if code == 200
|
||||
reset_token(data["access_token"], data["refresh_token"]) if Utils.is_response_ok?(code)
|
||||
[code, data]
|
||||
end
|
||||
|
||||
@@ -48,7 +48,7 @@ module Qiniu
|
||||
raise MissingUsernameOrPassword if (@username.nil? || @password.nil?)
|
||||
code, data = exchange_by_password!(@username, @password)
|
||||
end
|
||||
if code == 200
|
||||
if Utils.is_response_ok?(code)
|
||||
retry_times += 1
|
||||
if Config.settings[:auto_reconnect] && retry_times < Config.settings[:max_retry_times]
|
||||
return call_with_logged_in(url, data, retry_times)
|
||||
@@ -58,19 +58,25 @@ module Qiniu
|
||||
[code, data]
|
||||
end
|
||||
|
||||
def call_with_signature(url, data, retry_times = 0)
|
||||
code, data = http_request url, data, {:signature_auth => true}
|
||||
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)
|
||||
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)
|
||||
code, data = Auth.call_with_signature(url, data, 0, options)
|
||||
end
|
||||
rescue [MissingAccessToken, MissingRefreshToken, MissingUsernameOrPassword] => e
|
||||
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
|
||||
|
||||
+12
-7
@@ -8,7 +8,7 @@
|
||||
# Qbox::Config.load "path/to/your_project/config/qiniu.yml"
|
||||
#
|
||||
|
||||
require "qiniu/rs/version"
|
||||
require 'tmpdir'
|
||||
|
||||
module Qiniu
|
||||
module RS
|
||||
@@ -16,23 +16,28 @@ module Qiniu
|
||||
class << self
|
||||
|
||||
DEFAULT_OPTIONS = {
|
||||
:user_agent => 'Qiniu-RS-Ruby-SDK-' + VERSION + '()',
|
||||
:user_agent => 'Qiniu-RS-Ruby-SDK-' + Version.to_s + '()',
|
||||
:method => :post,
|
||||
:content_type => 'application/x-www-form-urlencoded',
|
||||
:auth_url => "https://acc.qbox.me/oauth2/token",
|
||||
:rs_host => "http://rs.qbox.me:10100",
|
||||
#:rs_host => "http://localhost:10100",
|
||||
:rs_host => "http://rs.qbox.me",
|
||||
:io_host => "http://iovip.qbox.me",
|
||||
#:io_host => "http://localhost:10200",
|
||||
:up_host => "http://up.qbox.me",
|
||||
:pub_host => "http://pu.qbox.me:10200",
|
||||
:eu_host => "http://eu.qbox.me",
|
||||
:client_id => "a75604760c4da4caaa456c0c5895c061c3065c5a",
|
||||
:client_secret => "75df554a39f58accb7eb293b550fa59618674b7d",
|
||||
:access_key => "",
|
||||
:secret_key => "",
|
||||
:auto_reconnect => true,
|
||||
:max_retry_times => 5
|
||||
:max_retry_times => 3,
|
||||
:block_size => 1024*1024*4,
|
||||
:chunk_size => 1024*256,
|
||||
:enable_debug => true,
|
||||
:tmpdir => Dir.tmpdir + File::SEPARATOR + 'Qiniu-RS-Ruby-SDK'
|
||||
}
|
||||
|
||||
REQUIRED_OPTION_KEYS = [:client_id, :client_secret, :auth_url, :rs_host, :io_host]
|
||||
REQUIRED_OPTION_KEYS = [:access_key, :secret_key]
|
||||
|
||||
attr_reader :settings, :default_params
|
||||
|
||||
|
||||
Executable
+22
@@ -0,0 +1,22 @@
|
||||
# -*- 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
|
||||
|
||||
@@ -4,9 +4,6 @@ module Qiniu
|
||||
module RS
|
||||
|
||||
class Exception < RuntimeError
|
||||
def to_s
|
||||
inspect
|
||||
end
|
||||
end
|
||||
|
||||
class ResponseError < Exception
|
||||
@@ -32,7 +29,7 @@ module Qiniu
|
||||
|
||||
class RequestFailed < ResponseError
|
||||
def message
|
||||
"HTTP status code #{http_code}"
|
||||
"HTTP status code: #{http_code}. Response body: #{http_body}"
|
||||
end
|
||||
|
||||
def to_s
|
||||
@@ -40,6 +37,43 @@ module Qiniu
|
||||
end
|
||||
end
|
||||
|
||||
class UploadFailedError < Exception
|
||||
def initialize(status_code, response_data)
|
||||
data_string = response_data.map { |key, value| %Q(:#{key.to_s} => #{value.to_s}) }
|
||||
msg = %Q(Uploading Failed. HTTP Status Code: #{status_code}. HTTP response body: #{data_string.join(', ')}.)
|
||||
super(msg)
|
||||
end
|
||||
end
|
||||
|
||||
class FileSeekReadError < Exception
|
||||
def initialize(fpath, block_index, seek_pos, read_length, result_length)
|
||||
msg = "Reading file: #{fpath}, "
|
||||
msg += "at block index: #{block_index}. "
|
||||
msg += "Expected seek_pos:#{seek_pos} and read_length:#{read_length}, "
|
||||
msg += "but got result_length: #{result_length}."
|
||||
super(msg)
|
||||
end
|
||||
end
|
||||
|
||||
class BlockSizeNotMathchError < Exception
|
||||
def initialize(fpath, block_index, offset, restsize, block_size)
|
||||
msg = "Reading file: #{fpath}, "
|
||||
msg += "at block index: #{block_index}. "
|
||||
msg += "Expected offset: #{offset}, restsize: #{restsize} and block_size: #{block_size}, "
|
||||
msg += "but got offset+restsize=#{offset+restsize}."
|
||||
super(msg)
|
||||
end
|
||||
end
|
||||
|
||||
class BlockCountNotMathchError < Exception
|
||||
def initialize(fpath, block_count, checksum_count, progress_count)
|
||||
msg = "Reading file: #{fpath}, "
|
||||
msg += "Expected block_count, checksum_count, progress_count is: #{block_count}, "
|
||||
msg += "but got checksum_count: #{checksum_count}, progress_count: #{progress_count}."
|
||||
super(msg)
|
||||
end
|
||||
end
|
||||
|
||||
class MissingArgsError < Exception
|
||||
def initialize(missing_keys)
|
||||
key_list = missing_keys.map {|key| key.to_s}.join(' and the ')
|
||||
|
||||
@@ -10,10 +10,32 @@ module Qiniu
|
||||
Utils.http_request url + '/imageInfo', nil, {:method => :get}
|
||||
end
|
||||
|
||||
def exif(url)
|
||||
Utils.http_request url + '?exif', nil, {:method => :get}
|
||||
end
|
||||
|
||||
def preivew_url(url, spec)
|
||||
url + '/imagePreview/' + spec.to_s
|
||||
end
|
||||
|
||||
def mogrify_preview_url(source_image_url, options)
|
||||
source_image_url + '?' + generate_mogrify_params_string(options)
|
||||
end
|
||||
|
||||
def generate_mogrify_params_string(options = {})
|
||||
opts = {}
|
||||
options.each do |k, v|
|
||||
opts[k.to_s] = v
|
||||
end
|
||||
params_string = ""
|
||||
keys = ["thumbnail", "gravity", "crop", "quality", "rotate", "format"]
|
||||
keys.each do |key|
|
||||
params_string += %Q(/#{key}/#{opts[key]}) unless opts[key].nil?
|
||||
end
|
||||
params_string += '/auto-orient' unless opts["auto_orient"].nil?
|
||||
'imageMogr' + URI.escape(params_string)
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
+28
-6
@@ -20,20 +20,42 @@ module Qiniu
|
||||
Auth.request(url)
|
||||
end
|
||||
|
||||
def put_file(url, local_file, bucket = nil, key = nil, mime_type = nil, custom_meta = nil, callback_params = nil, enable_crc32_check = false)
|
||||
raise NoSuchFileError unless File.exist?(local_file)
|
||||
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?
|
||||
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
|
||||
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)
|
||||
action_params += '/rotate/' + rotate if !rotate.nil? && rotate.to_i >= 0
|
||||
action_params
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
Executable
+37
@@ -0,0 +1,37 @@
|
||||
# -*- 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
|
||||
|
||||
@@ -6,6 +6,14 @@ module Qiniu
|
||||
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
|
||||
@@ -57,6 +65,18 @@ module Qiniu
|
||||
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
|
||||
|
||||
Executable
+328
@@ -0,0 +1,328 @@
|
||||
# -*- encoding: utf-8 -*-
|
||||
|
||||
require 'zlib'
|
||||
require 'yaml'
|
||||
require 'tmpdir'
|
||||
require 'fileutils'
|
||||
require 'mime/types'
|
||||
require 'digest/sha1'
|
||||
require 'qiniu/rs/abstract'
|
||||
require 'qiniu/rs/exceptions'
|
||||
require 'qiniu/rs/io'
|
||||
|
||||
module Qiniu
|
||||
module RS
|
||||
module UP
|
||||
|
||||
PROGRESS_TMP_FILE = 'progresses'
|
||||
CHECKSUM_TMP_FILE = 'ctxes'
|
||||
|
||||
module AbstractClass
|
||||
class ChunkProgressNotifier
|
||||
include Qiniu::RS::Abstract
|
||||
abstract_methods :notify
|
||||
# def notify(block_index, block_put_progress); end
|
||||
end
|
||||
|
||||
class BlockProgressNotifier
|
||||
include Qiniu::RS::Abstract
|
||||
abstract_methods :notify
|
||||
# def notify(block_index, checksum); end
|
||||
end
|
||||
end
|
||||
|
||||
class ChunkProgressNotifier < AbstractClass::ChunkProgressNotifier
|
||||
attr_reader :tmpdata
|
||||
def initialize(id)
|
||||
@tmpdata = UP::TmpData.new(id, PROGRESS_TMP_FILE)
|
||||
end
|
||||
def notify(index, progress)
|
||||
@tmpdata.set(index, progress)
|
||||
logmsg = "chunk #{progress[:offset]/Config.settings[:chunk_size]} in block #{index} successfully uploaded.\n" + progress.to_s
|
||||
Utils.debug(logmsg)
|
||||
end
|
||||
end
|
||||
|
||||
class BlockProgressNotifier < AbstractClass::BlockProgressNotifier
|
||||
attr_reader :tmpdata
|
||||
def initialize(id)
|
||||
@tmpdata = UP::TmpData.new(id, CHECKSUM_TMP_FILE)
|
||||
end
|
||||
def notify(index, checksum)
|
||||
@tmpdata.set(index, checksum)
|
||||
Utils.debug "block #{index}: {ctx: #{checksum}} successfully uploaded."
|
||||
Utils.debug "block #{index}: {checksum: #{checksum}} successfully uploaded."
|
||||
end
|
||||
end
|
||||
|
||||
class TmpData
|
||||
def initialize(dir, filename)
|
||||
@tmpdir = Config.settings[:tmpdir] + File::SEPARATOR + dir
|
||||
FileUtils.mkdir_p(@tmpdir) unless Dir.exists?(@tmpdir)
|
||||
@tmpfile = @tmpdir + File::SEPARATOR + filename
|
||||
end
|
||||
|
||||
def init(values)
|
||||
File.open(@tmpfile, "w") do |f|
|
||||
YAML::dump(values, f)
|
||||
Utils.debug %Q(Initializing tmpfile: #{@tmpfile})
|
||||
end
|
||||
end
|
||||
|
||||
def all
|
||||
File.exist?(@tmpfile) ? YAML.load_file(@tmpfile) : []
|
||||
end
|
||||
|
||||
def set(index, value)
|
||||
values = all
|
||||
values[index] = value
|
||||
File.open(@tmpfile, "w") do |f|
|
||||
YAML::dump(values, f)
|
||||
Utils.debug %Q(Updating tmpfile: #{@tmpfile})
|
||||
end
|
||||
end
|
||||
|
||||
def sweep!
|
||||
FileUtils.rm_r(@tmpdir) if Dir.exists?(@tmpdir)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
class << self
|
||||
include Utils
|
||||
|
||||
def upload_with_token(uptoken,
|
||||
local_file,
|
||||
bucket,
|
||||
key = nil,
|
||||
mime_type = nil,
|
||||
custom_meta = nil,
|
||||
customer = nil,
|
||||
callback_params = nil,
|
||||
rotate = nil)
|
||||
raise NoSuchFileError, local_file unless File.exist?(local_file)
|
||||
begin
|
||||
ifile = File.open(local_file, 'rb')
|
||||
fh = FileData.new(ifile)
|
||||
fsize = fh.data_size
|
||||
key = Digest::SHA1.hexdigest(local_file + fh.mtime.to_s) if key.nil?
|
||||
if mime_type.nil? || mime_type.empty?
|
||||
mime = MIME::Types.type_for local_file
|
||||
mime_type = mime.empty? ? 'application/octet-stream' : mime[0].content_type
|
||||
end
|
||||
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]
|
||||
ensure
|
||||
ifile.close unless ifile.nil?
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
class FileData
|
||||
attr_accessor :fh
|
||||
def initialize(fh)
|
||||
@fh = fh
|
||||
end
|
||||
def data_size
|
||||
@fh.stat.size
|
||||
end
|
||||
def get_data(offset, length)
|
||||
@fh.seek(offset)
|
||||
@fh.read(length)
|
||||
end
|
||||
def path
|
||||
@fh.path
|
||||
end
|
||||
def mtime
|
||||
@fh.mtime
|
||||
end
|
||||
#delegate :path, :mtime, :to => :fh
|
||||
end
|
||||
|
||||
def _new_block_put_progress_data
|
||||
{:ctx => nil, :offset => 0, :restsize => nil, :status_code => nil, :host => nil}
|
||||
end
|
||||
|
||||
def _call_binary_with_token(uptoken, url, data, content_type = nil, retry_times = 0)
|
||||
options = {
|
||||
:method => :post,
|
||||
:content_type => 'application/octet-stream',
|
||||
:upload_signature_token => uptoken
|
||||
}
|
||||
options[:content_type] = content_type if !content_type.nil? && !content_type.empty?
|
||||
code, data = http_request url, data, options
|
||||
unless Utils.is_response_ok?(code)
|
||||
retry_times += 1
|
||||
if Config.settings[:auto_reconnect] && retry_times < Config.settings[:max_retry_times]
|
||||
return _call_binary_with_token(uptoken, url, data, options[:content_type], retry_times)
|
||||
end
|
||||
end
|
||||
[code, data]
|
||||
end
|
||||
|
||||
def _mkblock(uptoken, block_size, body)
|
||||
url = Config.settings[:up_host] + "/mkblk/#{block_size}"
|
||||
_call_binary_with_token(uptoken, url, body)
|
||||
end
|
||||
|
||||
def _putblock(uphost, uptoken, ctx, offset, body)
|
||||
url = uphost + "/bput/#{ctx}/#{offset}"
|
||||
_call_binary_with_token(uptoken, url, body)
|
||||
end
|
||||
|
||||
def _resumable_put_block(uptoken, fh, block_index, block_size, chunk_size, progress, retry_times = 1, notifier)
|
||||
code, data = 0, {}
|
||||
fpath = fh.path
|
||||
# this block has never been uploaded.
|
||||
if progress[:ctx] == nil || progress[:ctx].empty?
|
||||
progress[:offset] = 0
|
||||
progress[:restsize] = block_size
|
||||
# choose the smaller one
|
||||
body_length = [block_size, chunk_size].min
|
||||
for i in 1..retry_times
|
||||
seek_pos = block_index*Config.settings[:block_size]
|
||||
body = fh.get_data(seek_pos, body_length)
|
||||
result_length = body.length
|
||||
if result_length != body_length
|
||||
raise FileSeekReadError.new(fpath, block_index, seek_pos, body_length, result_length)
|
||||
end
|
||||
code, data = _mkblock(uptoken, block_size, body)
|
||||
body_crc32 = Zlib.crc32(body)
|
||||
if Utils.is_response_ok?(code) && data["crc32"] == body_crc32
|
||||
progress[:ctx] = data["ctx"]
|
||||
progress[:offset] = body_length
|
||||
progress[:restsize] = block_size - body_length
|
||||
progress[:status_code] = code
|
||||
progress[:host] = data["host"]
|
||||
if !notifier.nil? && notifier.respond_to?("notify")
|
||||
notifier.notify(block_index, progress)
|
||||
end
|
||||
break
|
||||
elsif i == retry_times && data["crc32"] != body_crc32
|
||||
Log.logger.error %Q(Uploading block error. Expected crc32: #{body_crc32}, but got: #{data["crc32"]})
|
||||
end
|
||||
end
|
||||
elsif progress[:offset] + progress[:restsize] != block_size
|
||||
raise BlockSizeNotMathchError.new(fpath, block_index, progress[:offset], progress[:restsize], block_size)
|
||||
end
|
||||
# loop uploading other chunks except the first one
|
||||
while progress[:restsize].to_i > 0 && progress[:restsize] < block_size
|
||||
# choose the smaller one
|
||||
body_length = [progress[:restsize], chunk_size].min
|
||||
for i in 1..retry_times
|
||||
seek_pos = block_index*Config.settings[:block_size] + progress[:offset]
|
||||
body = fh.get_data(seek_pos, body_length)
|
||||
result_length = body.length
|
||||
if result_length != body_length
|
||||
raise FileSeekReadError.new(fpath, block_index, seek_pos, body_length, result_length)
|
||||
end
|
||||
code, data = _putblock(progress[:host], uptoken, progress[:ctx], progress[:offset], body)
|
||||
body_crc32 = Zlib.crc32(body)
|
||||
if Utils.is_response_ok?(code) && data["crc32"] == body_crc32
|
||||
progress[:ctx] = data["ctx"]
|
||||
progress[:offset] += body_length
|
||||
progress[:restsize] -= body_length
|
||||
progress[:status_code] = code
|
||||
progress[:host] = data["host"]
|
||||
if !notifier.nil? && notifier.respond_to?("notify")
|
||||
notifier.notify(block_index, progress)
|
||||
end
|
||||
break
|
||||
elsif i == retry_times && data["crc32"] != body_crc32
|
||||
Log.logger.error %Q(Uploading block error. Expected crc32: #{body_crc32}, but got: #{data["crc32"]})
|
||||
end
|
||||
end
|
||||
end
|
||||
# return
|
||||
return [code, data]
|
||||
end
|
||||
|
||||
def _block_count(fsize)
|
||||
((fsize + Config.settings[:block_size] - 1) / Config.settings[:block_size]).to_i
|
||||
end
|
||||
|
||||
def _resumable_put(uptoken, fh, checksums, progresses, block_notifier = nil, chunk_notifier = nil)
|
||||
code, data = 0, {}
|
||||
fsize = fh.data_size
|
||||
block_count = _block_count(fsize)
|
||||
checksum_count = checksums.length
|
||||
progress_count = progresses.length
|
||||
if checksum_count != block_count || progress_count != block_count
|
||||
raise BlockCountNotMathchError.new(fh.path, block_count, checksum_count, progress_count)
|
||||
end
|
||||
0.upto(block_count-1).each do |block_index|
|
||||
if checksums[block_index].nil? || checksums[block_index].empty?
|
||||
block_size = Config.settings[:block_size]
|
||||
if block_index == block_count - 1
|
||||
block_size = fsize - block_index*Config.settings[:block_size]
|
||||
end
|
||||
if progresses[block_index].nil?
|
||||
progresses[block_index] = _new_block_put_progress_data
|
||||
end
|
||||
code, data = _resumable_put_block(uptoken, fh, block_index, block_size, Config.settings[:chunk_size], progresses[block_index], Config.settings[:max_retry_times], chunk_notifier)
|
||||
if Utils.is_response_ok?(code)
|
||||
#checksums[block_index] = data["checksum"]
|
||||
checksums[block_index] = data["ctx"]
|
||||
if !block_notifier.nil? && block_notifier.respond_to?("notify")
|
||||
block_notifier.notify(block_index, checksums[block_index])
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
return [code, data]
|
||||
end
|
||||
|
||||
def _mkfile(uphost, uptoken, entry_uri, fsize, checksums, mime_type = nil, custom_meta = nil, customer = nil, callback_params = nil, rotate = nil)
|
||||
path = '/rs-mkfile/' + Utils.urlsafe_base64_encode(entry_uri) + "/fsize/#{fsize}"
|
||||
path += '/mimeType/' + Utils.urlsafe_base64_encode(mime_type) if !mime_type.nil? && !mime_type.empty?
|
||||
path += '/meta/' + Utils.urlsafe_base64_encode(custom_meta) if !custom_meta.nil? && !custom_meta.empty?
|
||||
path += '/customer/' + customer if !customer.nil? && !customer.empty?
|
||||
callback_query_string = Utils.generate_query_string(callback_params) if !callback_params.nil? && !callback_params.empty?
|
||||
path += '/params/' + Utils.urlsafe_base64_encode(callback_query_string) if !callback_query_string.nil? && !callback_query_string.empty?
|
||||
path += '/rotate/' + rotate if !rotate.nil? && rotate.to_i >= 0
|
||||
url = uphost + path
|
||||
#body = ''
|
||||
#checksums.each do |checksum|
|
||||
# body += Utils.urlsafe_base64_decode(checksum)
|
||||
#end
|
||||
body = checksums.join(',')
|
||||
_call_binary_with_token(uptoken, url, body, 'text/plain')
|
||||
end
|
||||
|
||||
def _resumable_upload(uptoken, fh, fsize, bucket, key, mime_type = nil, custom_meta = nil, customer = nil, callback_params = nil, rotate = nil)
|
||||
block_count = _block_count(fsize)
|
||||
chunk_notifier = ChunkProgressNotifier.new(key)
|
||||
block_notifier = BlockProgressNotifier.new(key)
|
||||
progresses = chunk_notifier.tmpdata.all
|
||||
if progresses.empty?
|
||||
block_count.times{progresses << _new_block_put_progress_data}
|
||||
chunk_notifier.tmpdata.init(progresses)
|
||||
end
|
||||
checksums = block_notifier.tmpdata.all
|
||||
if checksums.empty?
|
||||
block_count.times{checksums << ''}
|
||||
block_notifier.tmpdata.init(checksums)
|
||||
end
|
||||
code, data = _resumable_put(uptoken, fh, checksums, progresses, block_notifier, chunk_notifier)
|
||||
if Utils.is_response_ok?(code)
|
||||
uphost = data["host"]
|
||||
entry_uri = bucket + ':' + key
|
||||
code, data = _mkfile(uphost, uptoken, entry_uri, fsize, checksums, mime_type, custom_meta, customer, callback_params, rotate)
|
||||
end
|
||||
if Utils.is_response_ok?(code)
|
||||
Utils.debug "File #{fh.path} {size: #{fsize}} successfully uploaded."
|
||||
chunk_notifier.tmpdata.sweep!
|
||||
block_notifier.tmpdata.sweep!
|
||||
end
|
||||
[code, data]
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
+38
-39
@@ -1,6 +1,7 @@
|
||||
# -*- encoding: utf-8 -*-
|
||||
|
||||
require 'uri'
|
||||
require 'cgi'
|
||||
require 'json'
|
||||
require 'zlib'
|
||||
require 'base64'
|
||||
@@ -31,6 +32,20 @@ module Qiniu
|
||||
{}
|
||||
end
|
||||
|
||||
def is_response_ok?(status_code)
|
||||
status_code/100 == 2
|
||||
end
|
||||
|
||||
def response_error(status_code, errmsg)
|
||||
[status_code, {"error" => errmsg}]
|
||||
end
|
||||
|
||||
def debug(msg)
|
||||
if Config.settings[:enable_debug]
|
||||
Log.logger.debug(msg)
|
||||
end
|
||||
end
|
||||
|
||||
def send_request_with url, data = nil, options = {}
|
||||
options[:method] = Config.settings[:method] unless options[:method]
|
||||
options[:content_type] = Config.settings[:content_type] unless options[:content_type]
|
||||
@@ -38,22 +53,25 @@ module Qiniu
|
||||
:accept => :json,
|
||||
:user_agent => Config.settings[:user_agent]
|
||||
}
|
||||
if options[:signature_auth] && options[:signature_auth] == true
|
||||
signature_token = generate_qbox_signature(Config.settings[:access_key], Config.settings[:secret_key], url, data)
|
||||
header_options.merge!('Authorization' => "QBox #{signature_token}")
|
||||
auth_token = nil
|
||||
if !options[:qbox_signature_token].nil? && !options[:qbox_signature_token].empty?
|
||||
auth_token = 'QBox ' + options[:qbox_signature_token]
|
||||
elsif !options[:upload_signature_token].nil? && !options[:upload_signature_token].empty?
|
||||
auth_token = 'UpToken ' + options[:upload_signature_token]
|
||||
elsif options[:access_token]
|
||||
header_options.merge!('Authorization' => "Bearer #{options[:access_token]}")
|
||||
auth_token = 'Bearer ' + options[:access_token]
|
||||
end
|
||||
header_options.merge!('Authorization' => auth_token) unless auth_token.nil?
|
||||
case options[:method]
|
||||
when :get
|
||||
response = RestClient.get url, header_options
|
||||
response = RestClient.get(url, header_options)
|
||||
when :post
|
||||
header_options.merge!(:content_type => options[:content_type])
|
||||
response = RestClient.post url, data, header_options
|
||||
response = RestClient.post(url, data, header_options)
|
||||
end
|
||||
code = response.respond_to?(:code) ? response.code.to_i : 0
|
||||
if code != 200
|
||||
raise RequestFailed.new(response)
|
||||
unless is_response_ok?(code)
|
||||
raise RequestFailed.new("Request Failed", response)
|
||||
else
|
||||
data = {}
|
||||
body = response.respond_to?(:body) ? response.body : {}
|
||||
@@ -88,57 +106,38 @@ module Qiniu
|
||||
end
|
||||
end
|
||||
|
||||
def upload_multipart_data(url, filepath, action_string, callback_query_string = '')
|
||||
code, data = 0, {}
|
||||
begin
|
||||
header_options = {
|
||||
:accept => :json,
|
||||
:user_agent => Config.settings[:user_agent]
|
||||
}
|
||||
def upload_multipart_data(url, filepath, action_string, callback_query_string = '', uptoken = nil)
|
||||
post_data = {
|
||||
:file => File.new(filepath, 'rb'),
|
||||
:params => callback_query_string,
|
||||
:action => action_string,
|
||||
:file => File.new(filepath, 'rb'),
|
||||
:multipart => true
|
||||
}
|
||||
response = RestClient.post url, post_data, header_options
|
||||
body = response.respond_to?(:body) ? response.body : ""
|
||||
data = safe_json_parse(body) unless body.empty?
|
||||
code = response.code.to_i if response.respond_to?(:code)
|
||||
rescue Errno::ECONNRESET => err
|
||||
Log.logger.error err
|
||||
rescue => e
|
||||
Log.logger.warn "#{e.message} => Utils.http_request('#{url}')"
|
||||
res = e.response
|
||||
if e.respond_to? :response
|
||||
res = e.response
|
||||
code = res.code.to_i if res.respond_to? :code
|
||||
body = res.respond_to?(:body) ? res.body : ""
|
||||
data = safe_json_parse(body) unless body.empty?
|
||||
end
|
||||
end
|
||||
[code, data]
|
||||
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| key.to_s+"="+value.to_s }
|
||||
URI.escape(total_param.join("&"))
|
||||
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(access_key, secret_key, url, params)
|
||||
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";
|
||||
signature += "\n"
|
||||
if params.is_a?(Hash)
|
||||
total_param = params.map { |key, value| key.to_s+"="+value.to_s }
|
||||
signature += total_param.join("&")
|
||||
params_string = generate_query_string(params)
|
||||
signature += params_string
|
||||
end
|
||||
hmac = HMAC::SHA1.new(secret_key)
|
||||
hmac.update(signature)
|
||||
|
||||
+13
-1
@@ -2,6 +2,18 @@
|
||||
|
||||
module Qiniu
|
||||
module RS
|
||||
VERSION = "2.0.1"
|
||||
module Version
|
||||
MAJOR = 3
|
||||
MINOR = 2
|
||||
PATCH = 2
|
||||
# Returns a version string by joining <tt>MAJOR</tt>, <tt>MINOR</tt>, and <tt>PATCH</tt> with <tt>'.'</tt>
|
||||
#
|
||||
# Example
|
||||
#
|
||||
# Version.to_s # '1.0.2'
|
||||
def self.to_s
|
||||
[MAJOR, MINOR, PATCH].join('.')
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Executable
+23
@@ -0,0 +1,23 @@
|
||||
# -*- encoding: utf-8 -*-
|
||||
|
||||
require 'hmac-sha1'
|
||||
require 'qiniu/rs/config'
|
||||
require 'qiniu/rs/utils'
|
||||
|
||||
module Qiniu
|
||||
module RS
|
||||
class AccessToken
|
||||
|
||||
include Utils
|
||||
|
||||
attr_accessor :access_key, :secret_key
|
||||
|
||||
def generate_encoded_digest(signature)
|
||||
hmac = HMAC::SHA1.new(@secret_key)
|
||||
hmac.update(signature)
|
||||
urlsafe_base64_encode(hmac.digest)
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
Executable
+38
@@ -0,0 +1,38 @@
|
||||
# -*- encoding: utf-8 -*-
|
||||
|
||||
require 'cgi'
|
||||
require 'json'
|
||||
require 'qiniu/tokens/access_token'
|
||||
|
||||
module Qiniu
|
||||
module RS
|
||||
class QboxToken < AccessToken
|
||||
|
||||
attr_accessor :url, :params
|
||||
|
||||
def initialize(opts = {})
|
||||
@url = opts[:url]
|
||||
@params = opts[:params]
|
||||
end
|
||||
|
||||
def generate_signature
|
||||
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)
|
||||
total_param = @params.map { |key, value| %Q(#{CGI.escape(key.to_s)}=#{CGI.escape(value.to_s).gsub('+', '%20')}) }
|
||||
signature += total_param.join("&")
|
||||
end
|
||||
signature
|
||||
end
|
||||
|
||||
def generate_token
|
||||
encoded_digest = generate_encoded_digest(generate_signature)
|
||||
%Q(#{@access_key}:#{encoded_digest})
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
Executable
+41
@@ -0,0 +1,41 @@
|
||||
# -*- encoding: utf-8 -*-
|
||||
|
||||
require 'json'
|
||||
require 'qiniu/tokens/access_token'
|
||||
require 'qiniu/rs/utils'
|
||||
|
||||
module Qiniu
|
||||
module RS
|
||||
class UploadToken < AccessToken
|
||||
|
||||
include Utils
|
||||
|
||||
attr_accessor :scope, :expires_in, :callback_url, :callback_body_type, :customer, :escape
|
||||
|
||||
def initialize(opts = {})
|
||||
@scope = opts[:scope]
|
||||
@expires_in = opts[:expires_in] || 3600
|
||||
@callback_url = opts[:callback_url]
|
||||
@callback_body_type = opts[:callback_body_type]
|
||||
@customer = opts[:customer]
|
||||
@escape = opts[:escape]
|
||||
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[: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)
|
||||
end
|
||||
|
||||
def generate_token
|
||||
signature = generate_signature
|
||||
encoded_digest = generate_encoded_digest(signature)
|
||||
%Q(#{@access_key}:#{encoded_digest}:#{signature})
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
+10
-10
@@ -5,24 +5,24 @@ require File.expand_path('../lib/qiniu/rs/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/v2/sdk/ruby/}
|
||||
gem.description = %q{Qiniu Resource (Cloud) Storage SDK for Ruby. See: http://docs.qiniutek.com/v3/sdk/ruby/}
|
||||
gem.summary = %q{Qiniu Resource (Cloud) Storage SDK for Ruby}
|
||||
gem.homepage = "https://github.com/why404/qiniu-rs"
|
||||
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.require_paths = ["lib"]
|
||||
gem.version = Qiniu::RS::VERSION
|
||||
gem.version = Qiniu::RS::Version.to_s
|
||||
|
||||
# specify any dependencies here; for example:
|
||||
gem.add_development_dependency "rake", "~> 0.9.2.2"
|
||||
gem.add_development_dependency "rspec", "~> 2.10.0"
|
||||
gem.add_development_dependency "fakeweb", "~> 1.3.0"
|
||||
gem.add_runtime_dependency "json", "~> 1.7.3"
|
||||
gem.add_runtime_dependency "rest-client", "~> 1.6.7"
|
||||
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"
|
||||
gem.add_runtime_dependency "mime-types", "~> 1.19"
|
||||
gem.add_runtime_dependency "ruby-hmac", "~> 0.4.0"
|
||||
gem.add_runtime_dependency "jruby-openssl", "~> 0.7.7" if RUBY_PLATFORM == "java"
|
||||
gem.add_runtime_dependency "ruby-hmac", "~> 0.4"
|
||||
gem.add_runtime_dependency "jruby-openssl", "~> 0.7" if RUBY_PLATFORM == "java"
|
||||
end
|
||||
|
||||
Executable
+30
@@ -0,0 +1,30 @@
|
||||
# -*- encoding: utf-8 -*-
|
||||
|
||||
require 'spec_helper'
|
||||
require 'qiniu/rs/abstract'
|
||||
|
||||
describe Qiniu::RS::Abstract do
|
||||
before(:each) do
|
||||
@klass = Class.new do
|
||||
include Qiniu::RS::Abstract
|
||||
|
||||
abstract_methods :foo, :bar
|
||||
end
|
||||
end
|
||||
|
||||
it "raises NotImplementedError" do
|
||||
proc {
|
||||
@klass.new.foo
|
||||
}.should raise_error(NotImplementedError)
|
||||
end
|
||||
|
||||
it "can be overridden" do
|
||||
subclass = Class.new(@klass) do
|
||||
def foo
|
||||
:overridden
|
||||
end
|
||||
end
|
||||
|
||||
subclass.new.foo.should == :overridden
|
||||
end
|
||||
end
|
||||
@@ -8,8 +8,8 @@ module Qiniu
|
||||
describe Auth do
|
||||
|
||||
before :all do
|
||||
@username = "test@qbox.net"
|
||||
@password = "test"
|
||||
@username = "qboxtest"
|
||||
@password = "qboxtest123"
|
||||
|
||||
code, data = Qiniu::RS::Auth.exchange_by_password!(@username, @password)
|
||||
code.should == 200
|
||||
|
||||
Executable
+65
@@ -0,0 +1,65 @@
|
||||
# -*- 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
|
||||
+47
-24
@@ -9,44 +9,67 @@ module Qiniu
|
||||
module RS
|
||||
describe Image do
|
||||
|
||||
before :each do
|
||||
=begin
|
||||
code, data = Qiniu::RS::Auth.exchange_by_password!("test@qbox.net", "test")
|
||||
code.should == 200
|
||||
data.should be_an_instance_of(Hash)
|
||||
data["access_token"].should_not be_empty
|
||||
data["refresh_token"].should_not be_empty
|
||||
data["refresh_token"].should_not be_empty
|
||||
puts data.inspect
|
||||
=end
|
||||
before :all do
|
||||
|
||||
@bucket = "test_images"
|
||||
@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__)
|
||||
|
||||
put_url = Qiniu::RS.put_auth(10)
|
||||
put_url.should_not be_false
|
||||
put_url.should_not be_empty
|
||||
result = Qiniu::RS.upload :url => put_url,
|
||||
:file => local_file,
|
||||
:bucket => @bucket,
|
||||
:key => @key,
|
||||
:enable_crc32_check => true
|
||||
result.should be_true
|
||||
upopts = {:scope => @bucket, :expires_in => 3600, :customer => "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
|
||||
result = Qiniu::RS.get(@bucket, @key)
|
||||
result["url"].should_not be_empty
|
||||
puts result.inspect
|
||||
code, data = Qiniu::RS::Image.info(result["url"])
|
||||
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
|
||||
|
||||
+37
-20
@@ -1,4 +1,4 @@
|
||||
# -*- encoding: utf-8 -*-
|
||||
# Utils.-*- encoding: utf-8 -*-
|
||||
|
||||
require 'spec_helper'
|
||||
require 'qiniu/rs/auth'
|
||||
@@ -10,30 +10,47 @@ module Qiniu
|
||||
describe IO do
|
||||
|
||||
before :all do
|
||||
=begin
|
||||
code, data = Qiniu::RS::Auth.exchange_by_password!("test@qbox.net", "test")
|
||||
code.should == 200
|
||||
data.should be_an_instance_of(Hash)
|
||||
data["access_token"].should_not be_empty
|
||||
data["refresh_token"].should_not be_empty
|
||||
data["refresh_token"].should_not be_empty
|
||||
puts data.inspect
|
||||
=end
|
||||
|
||||
code2, data2 = Qiniu::RS::IO.put_auth()
|
||||
code2.should == 200
|
||||
data2["url"].should_not be_empty
|
||||
data2["expiresIn"].should_not be_zero
|
||||
puts data2.inspect
|
||||
|
||||
@put_url = data2["url"]
|
||||
@bucket = "test"
|
||||
@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(@put_url, __FILE__, @bucket, @key)
|
||||
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
|
||||
|
||||
Executable
+60
@@ -0,0 +1,60 @@
|
||||
# -*- encoding: utf-8 -*-
|
||||
|
||||
require 'spec_helper'
|
||||
require 'qiniu/rs/auth'
|
||||
require 'qiniu/rs/rs'
|
||||
require 'qiniu/rs/pub'
|
||||
|
||||
module Qiniu
|
||||
module RS
|
||||
describe Pub do
|
||||
|
||||
before :all do
|
||||
@bucket = "pub_test_bucket"
|
||||
result = Qiniu::RS.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
|
||||
end
|
||||
|
||||
context ".set_protected" do
|
||||
it "should works" do
|
||||
code, data = Qiniu::RS::Pub.set_protected(@bucket, 1)
|
||||
code.should == 200
|
||||
puts data.inspect
|
||||
end
|
||||
end
|
||||
|
||||
context ".set_separator" do
|
||||
it "should works" do
|
||||
code, data = Qiniu::RS::Pub.set_separator(@bucket, "-")
|
||||
code.should == 200
|
||||
puts data.inspect
|
||||
end
|
||||
end
|
||||
|
||||
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.should == 200
|
||||
puts data.inspect
|
||||
end
|
||||
end
|
||||
|
||||
context ".unset_style" do
|
||||
it "should works" do
|
||||
code, data = Qiniu::RS::Pub.unset_style(@bucket, "small.jpg")
|
||||
code.should == 200
|
||||
puts data.inspect
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
+30
-21
@@ -11,31 +11,40 @@ module Qiniu
|
||||
describe RS do
|
||||
|
||||
before :all do
|
||||
=begin
|
||||
code, data = Qiniu::RS::Auth.exchange_by_password!("test@qbox.net", "test")
|
||||
code.should == 200
|
||||
data.should be_an_instance_of(Hash)
|
||||
data["access_token"].should_not be_empty
|
||||
data["refresh_token"].should_not be_empty
|
||||
data["refresh_token"].should_not be_empty
|
||||
puts data.inspect
|
||||
=end
|
||||
|
||||
code2, data2 = Qiniu::RS::IO.put_auth()
|
||||
code2.should == 200
|
||||
data2["url"].should_not be_empty
|
||||
data2["expiresIn"].should_not be_zero
|
||||
puts data2.inspect
|
||||
|
||||
@put_url = data2["url"]
|
||||
@bucket = "test"
|
||||
@bucket = "rs_test_bucket"
|
||||
@key = Digest::SHA1.hexdigest (Time.now.to_i+rand(100)).to_s
|
||||
@domain = 'cdn.example.com'
|
||||
@domain = 'rs-test-bucket.dn.qbox.me'
|
||||
|
||||
code, data = Qiniu::RS::RS.mkbucket(@bucket)
|
||||
puts [code, data].inspect
|
||||
code.should == 200
|
||||
end
|
||||
|
||||
context "IO.put_file" do
|
||||
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_file(@put_url, __FILE__, @bucket, @key)
|
||||
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
|
||||
|
||||
Executable
+59
@@ -0,0 +1,59 @@
|
||||
# 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| 5242888.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
|
||||
File.unlink(@localfile) if File.exists?(@localfile)
|
||||
|
||||
code, data = Qiniu::RS::RS.drop(@bucket)
|
||||
puts [code, data].inspect
|
||||
code.should == 200
|
||||
end
|
||||
|
||||
context ".upload_with_token" do
|
||||
it "should works" do
|
||||
upopts = {:scope => @bucket, :expires_in => 3600, :customer => "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
|
||||
@@ -3,8 +3,8 @@
|
||||
require 'spec_helper'
|
||||
require 'qiniu/rs/version'
|
||||
|
||||
describe Qiniu::RS do
|
||||
describe Qiniu::RS::Version do
|
||||
it "should has a VERSION" do
|
||||
Qiniu::RS::VERSION.should =~ /^\d+\.\d+\.\d+?$/
|
||||
Qiniu::RS::Version.to_s.should =~ /^\d+\.\d+\.\d+?$/
|
||||
end
|
||||
end
|
||||
|
||||
+195
-7
@@ -1,5 +1,6 @@
|
||||
# -*- encoding: utf-8 -*-
|
||||
|
||||
require 'digest/sha1'
|
||||
require 'spec_helper'
|
||||
require 'qiniu/rs'
|
||||
|
||||
@@ -9,17 +10,93 @@ module Qiniu
|
||||
before :all do
|
||||
@bucket = 'qiniu_rs_test'
|
||||
@key = Digest::SHA1.hexdigest Time.now.to_s
|
||||
@domain = 'cdn.example.com'
|
||||
@domain = 'qiniu-rs-test.dn.qbox.me'
|
||||
|
||||
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
|
||||
|
||||
@test_image_key = 'image_logo_for_test.png'
|
||||
local_file = File.expand_path('./rs/' + @test_image_key, File.dirname(__FILE__))
|
||||
puts local_file.inspect
|
||||
upopts = {:scope => @test_image_bucket, :expires_in => 3600, :customer => "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
|
||||
puts data.inspect
|
||||
end
|
||||
|
||||
=begin
|
||||
context ".login!" do
|
||||
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
|
||||
end
|
||||
|
||||
context ".buckets" do
|
||||
it "should works" do
|
||||
result = Qiniu::RS.login!("test@qbox.net", "test")
|
||||
result.should be_true
|
||||
result = Qiniu::RS.buckets
|
||||
result.should_not be_false
|
||||
puts result.inspect
|
||||
end
|
||||
end
|
||||
|
||||
context ".set_protected" do
|
||||
it "should works" do
|
||||
result = Qiniu::RS.set_protected(@bucket, 1)
|
||||
result.should_not be_false
|
||||
puts result.inspect
|
||||
end
|
||||
end
|
||||
|
||||
context ".set_separator" do
|
||||
it "should works" do
|
||||
result = Qiniu::RS.set_separator(@bucket, "-")
|
||||
result.should_not be_false
|
||||
puts result.inspect
|
||||
end
|
||||
end
|
||||
|
||||
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.should_not be_false
|
||||
puts result.inspect
|
||||
end
|
||||
end
|
||||
|
||||
context ".unset_style" do
|
||||
it "should works" do
|
||||
result = Qiniu::RS.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
|
||||
=end
|
||||
|
||||
context ".put_auth" do
|
||||
it "should works" do
|
||||
@@ -40,11 +117,74 @@ module Qiniu
|
||||
: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),
|
||||
:file => __FILE__,
|
||||
:bucket => @bucket,
|
||||
:key => @key,
|
||||
:enable_crc32_check => true
|
||||
}
|
||||
result = Qiniu::RS.upload_file(upload_opts)
|
||||
result.should_not be_false
|
||||
puts result.inspect
|
||||
end
|
||||
end
|
||||
|
||||
context ".resumable_upload_file" do
|
||||
it "should works" do
|
||||
# generate bigfile for testing
|
||||
localfile = "test_bigfile"
|
||||
File.open(localfile, "w"){|f| 5242888.times{f.write(Random.rand(9).to_s)}}
|
||||
key = Digest::SHA1.hexdigest(localfile+Time.now.to_s)
|
||||
# generate the upload token
|
||||
uptoken_opts = {:scope => @bucket, :expires_in => 3600, :customer => "awhy.xu@gmail.com", :escape => 0}
|
||||
uptoken = Qiniu::RS.generate_upload_token(uptoken_opts)
|
||||
# uploading
|
||||
upload_opts = {
|
||||
:uptoken => uptoken,
|
||||
:file => localfile,
|
||||
:bucket => @bucket,
|
||||
:key => key
|
||||
}
|
||||
#uploading
|
||||
result1 = Qiniu::RS.upload_file(upload_opts)
|
||||
#drop the bigfile
|
||||
File.unlink(localfile) if File.exists?(localfile)
|
||||
#expect
|
||||
puts result1.inspect
|
||||
result1.should_not be_false
|
||||
result1.should_not be_empty
|
||||
#stat
|
||||
result2 = Qiniu::RS.stat(@bucket, key)
|
||||
puts result2.inspect
|
||||
result2.should_not be_false
|
||||
#delete
|
||||
result3 = Qiniu::RS.delete(@bucket, key)
|
||||
puts result3.inspect
|
||||
result3.should_not be_false
|
||||
end
|
||||
end
|
||||
|
||||
context ".stat" do
|
||||
it "should works" do
|
||||
result = Qiniu::RS.stat(@bucket, @key)
|
||||
@@ -138,7 +278,7 @@ module Qiniu
|
||||
|
||||
context ".image_info" do
|
||||
it "should works" do
|
||||
data = Qiniu::RS.get("test_images", "image_logo_for_test.png")
|
||||
data = Qiniu::RS.get(@test_image_bucket, @test_image_key)
|
||||
data.should_not be_false
|
||||
data.should_not be_empty
|
||||
puts data.inspect
|
||||
@@ -149,5 +289,53 @@ module Qiniu
|
||||
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.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 = {
|
||||
:thumbnail => "!120x120>",
|
||||
:gravity => "center",
|
||||
:crop => "!120x120a0a0",
|
||||
:quality => 85,
|
||||
:rotate => 45,
|
||||
:format => "jpg",
|
||||
:auto_orient => true
|
||||
}
|
||||
result2 = Qiniu::RS.image_mogrify_save_as(dest_bucket, dest_key, src_img_url, mogrify_options)
|
||||
result2.should_not be_false
|
||||
result2.should_not be_empty
|
||||
puts result2.inspect
|
||||
end
|
||||
end
|
||||
|
||||
context ".generate_upload_token" do
|
||||
it "should works" do
|
||||
data = Qiniu::RS.generate_upload_token({:scope => 'test_bucket', :expires_in => 3600, :escape => 0})
|
||||
data.should_not be_empty
|
||||
puts data.inspect
|
||||
end
|
||||
end
|
||||
|
||||
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
|
||||
+2
-2
@@ -6,7 +6,7 @@ require 'rspec'
|
||||
|
||||
RSpec.configure do |config|
|
||||
config.before :all do
|
||||
Qiniu::RS.establish_connection! :access_key => "3fPHl_SLkPXdioqI_A8_NGngPWVJhlDk2ktRjogH",
|
||||
:secret_key => "bXTPMDJrVYRJUiSDRFtFYwycVD_mjXxYWrCYlDHy"
|
||||
Qiniu::RS.establish_connection! :access_key => "iN7NgwM31j4-BZacMjPrOQBs34UG1maYCAQmhdCV",
|
||||
:secret_key => "6QTOr2Jg1gcZEWDQXKOGZh5PziC2MCV5KsntT70j"
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user