mirror of
https://github.com/wahyd4/ruby-sdk.git
synced 2026-08-27 13:45:55 +10:00
合并最新代码。
This commit is contained in:
+14
-14
@@ -12,30 +12,30 @@ module Qiniu
|
||||
include Utils
|
||||
|
||||
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, options[:mime])})
|
||||
[code, data]
|
||||
end
|
||||
code, data, raw_headers = http_request url, data, options.merge({:qbox_signature_token => generate_qbox_signature(url, data, options[:mime])})
|
||||
[code, data, raw_headers]
|
||||
end # call_with_signature
|
||||
|
||||
def request(url, data = nil, options = {})
|
||||
code, data = Auth.call_with_signature(url, data, 0, options)
|
||||
[code, data]
|
||||
end
|
||||
code, data, raw_headers = Auth.call_with_signature(url, data, 0, options)
|
||||
[code, data, raw_headers]
|
||||
end # request
|
||||
|
||||
EMPTY_ARGS = {}
|
||||
|
||||
### 生成下载授权URL
|
||||
def authorize_download_url(url, args = EMPTY_ARGS)
|
||||
### 提取AK/SK信息
|
||||
ak = args[:access_key]
|
||||
if ak.nil? then
|
||||
ak = Config.settings[:access_key]
|
||||
access_key = args[:access_key]
|
||||
if access_key.nil? then
|
||||
access_key = Config.settings[:access_key]
|
||||
end
|
||||
|
||||
sk = args[:access_key]
|
||||
if sk.nil? then
|
||||
sk = Config.settings[:secret_key]
|
||||
secret_key = args[:secret_key]
|
||||
if secret_key.nil? then
|
||||
secret_key = Config.settings[:secret_key]
|
||||
end
|
||||
|
||||
|
||||
### 授权期计算
|
||||
if args[:expires].is_a?(Integer) && args[:expires] > 0 then
|
||||
# 指定相对时间,单位:秒
|
||||
@@ -58,7 +58,7 @@ module Qiniu
|
||||
end
|
||||
|
||||
### 生成数字签名
|
||||
sign = HMAC::SHA1.new(secret_key).update(download_url)
|
||||
sign = HMAC::SHA1.new(secret_key).update(download_url).digest
|
||||
encoded_sign = Utils.urlsafe_base64_encode(sign)
|
||||
|
||||
### 生成下载授权凭证
|
||||
|
||||
+5
-3
@@ -75,9 +75,10 @@ module Qiniu
|
||||
data = {}
|
||||
body = response.respond_to?(:body) ? response.body : {}
|
||||
data = safe_json_parse(body) unless body.empty?
|
||||
raw_headers = response.raw_headers
|
||||
end
|
||||
[code, data]
|
||||
end
|
||||
[code, data, raw_headers]
|
||||
end # send_request_with
|
||||
|
||||
def http_request url, data = nil, options = {}
|
||||
retry_times = 0
|
||||
@@ -100,8 +101,9 @@ module Qiniu
|
||||
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?
|
||||
raw_headers = response.raw_headers
|
||||
end
|
||||
[code, data]
|
||||
[code, data, raw_headers]
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
+67
-11
@@ -19,26 +19,31 @@ module Qiniu
|
||||
|
||||
@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
|
||||
@@ -54,9 +59,17 @@ module Qiniu
|
||||
### 测试单文件直传
|
||||
context ".put_file" do
|
||||
it "should works" do
|
||||
code, data = Qiniu::Storage.put_file(__FILE__, @bucket, @key, 'application/x-ruby', 'customMeta', true)
|
||||
code, data, raw_headers = Qiniu::Storage.put_file(
|
||||
__FILE__,
|
||||
@bucket,
|
||||
@key,
|
||||
'application/x-ruby',
|
||||
'customMeta',
|
||||
true,
|
||||
)
|
||||
code.should == 200
|
||||
puts data.inspect
|
||||
puts raw_headers.inspect
|
||||
end
|
||||
end
|
||||
|
||||
@@ -80,9 +93,19 @@ module Qiniu
|
||||
it "should works" do
|
||||
upopts = {:scope => @bucket, :expires_in => 3600, :customer => "why404@gmail.com"}
|
||||
uptoken = Qiniu.generate_upload_token(upopts)
|
||||
code, data = Qiniu::Storage.upload_with_token(uptoken, __FILE__, @bucket, @key, nil, nil, nil, true)
|
||||
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
|
||||
|
||||
@@ -107,10 +130,15 @@ module Qiniu
|
||||
upopts = {:scope => @bucket, :expires_in => 3600, :endUser => "why404@gmail.com"}
|
||||
uptoken = Qiniu.generate_upload_token(upopts)
|
||||
|
||||
code, data = Qiniu::Storage.upload_with_token_2(uptoken, __FILE__, @key)
|
||||
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
|
||||
|
||||
@@ -135,9 +163,16 @@ module Qiniu
|
||||
it "should works" do
|
||||
upopts = {:scope => @bucket, :expires_in => 3600, :customer => "why404@gmail.com"}
|
||||
uptoken = Qiniu.generate_upload_token(upopts)
|
||||
code, data = Qiniu::Storage.resumable_upload_with_token(uptoken, @localfile_5m, @bucket, @key_5m)
|
||||
puts data.inspect
|
||||
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
|
||||
|
||||
@@ -161,9 +196,16 @@ module Qiniu
|
||||
it "should works" do
|
||||
upopts = {:scope => @bucket, :expires_in => 3600, :customer => "why404@gmail.com"}
|
||||
uptoken = Qiniu.generate_upload_token(upopts)
|
||||
code, data = Qiniu::Storage.resumable_upload_with_token(uptoken, @localfile_4m, @bucket, @key_4m)
|
||||
puts data.inspect
|
||||
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
|
||||
|
||||
@@ -187,9 +229,16 @@ module Qiniu
|
||||
it "should works" do
|
||||
upopts = {:scope => @bucket, :expires_in => 3600, :customer => "why404@gmail.com"}
|
||||
uptoken = Qiniu.generate_upload_token(upopts)
|
||||
code, data = Qiniu::Storage.resumable_upload_with_token(uptoken, @localfile_8m, @bucket, @key_8m)
|
||||
puts data.inspect
|
||||
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
|
||||
|
||||
@@ -213,9 +262,16 @@ module Qiniu
|
||||
it "should works" do
|
||||
upopts = {:scope => @bucket, :expires_in => 3600, :customer => "why404@gmail.com"}
|
||||
uptoken = Qiniu.generate_upload_token(upopts)
|
||||
code, data = Qiniu::Storage.resumable_upload_with_token(uptoken, @localfile_1m, @bucket, @key_1m)
|
||||
puts data.inspect
|
||||
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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user