add Qiniu::RS.generate_download_token()

This commit is contained in:
404
2012-12-19 17:53:26 +08:00
parent d1e60697c3
commit 5b0a561cc1
13 changed files with 86 additions and 21 deletions
+4
View File
@@ -1,5 +1,9 @@
## CHANGE LOG
### v3.3.0
- 私有资源下载新版实现,添加 Qiniu::RS.generate_download_token() 方法。参考 [downloadToken](http://docs.qiniutek.com/v3/api/io/#get)
### v3.2.2
fixed E701 error
+4 -4
View File
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
qiniu-rs (3.2.2)
qiniu-rs (3.3.0)
json (~> 1.7)
mime-types (~> 1.19)
rest-client (~> 1.6)
@@ -14,15 +14,15 @@ GEM
fakeweb (1.3.0)
json (1.7.5)
mime-types (1.19)
rake (10.0.2)
rake (10.0.3)
rest-client (1.6.7)
mime-types (>= 1.16)
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)
rspec-core (2.12.2)
rspec-expectations (2.12.1)
diff-lcs (~> 1.1.3)
rspec-mocks (2.12.0)
ruby-hmac (0.4.0)
+1 -1
View File
@@ -1,4 +1,4 @@
# Qiniu Resource (Cloud) Storage SDK for Ruby - [![Build Status](https://secure.travis-ci.org/why404/qiniu-rs-for-ruby.png?branch=master)](http://travis-ci.org/why404/qiniu-rs-for-ruby) [![Dependency Status](https://gemnasium.com/why404/qiniu-rs-for-ruby.png)](https://gemnasium.com/why404/qiniu-rs-for-ruby)
# Qiniu Resource (Cloud) Storage SDK for Ruby - [![Build Status](https://api.travis-ci.org/qiniu/ruby-sdk.png?branch=master)](https://travis-ci.org/qiniu/ruby-sdk) [![Dependency Status](https://gemnasium.com/why404/qiniu-rs-for-ruby.png)](https://gemnasium.com/why404/qiniu-rs-for-ruby)
# 关于
+11 -1
View File
@@ -17,6 +17,7 @@ module Qiniu
autoload :AccessToken, 'qiniu/tokens/access_token'
autoload :QboxToken, 'qiniu/tokens/qbox_token'
autoload :UploadToken, 'qiniu/tokens/upload_token'
autoload :DownloadToken, 'qiniu/tokens/download_token'
autoload :Abstract, 'qiniu/rs/abstract'
class << self
@@ -100,7 +101,7 @@ module Qiniu
end
def upload_file opts = {}
uncontained_opts = [:uptoken, :file, :bucket, :key] - opts.keys
uncontained_opts = [:uptoken, :file, :bucket, :key] - opts.keys
raise MissingArgsError, uncontained_opts unless uncontained_opts.empty?
source_file = opts[:file]
@@ -232,6 +233,15 @@ module Qiniu
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
+4 -3
View File
@@ -58,7 +58,7 @@ module Qiniu
class TmpData
def initialize(dir, filename)
@tmpdir = Config.settings[:tmpdir] + File::SEPARATOR + dir
FileUtils.mkdir_p(@tmpdir) unless Dir.exists?(@tmpdir)
FileUtils.mkdir_p(@tmpdir) unless File.directory?(@tmpdir)
@tmpfile = @tmpdir + File::SEPARATOR + filename
end
@@ -83,7 +83,7 @@ module Qiniu
end
def sweep!
FileUtils.rm_r(@tmpdir) if Dir.exists?(@tmpdir)
FileUtils.rm_r(@tmpdir) if File.directory?(@tmpdir)
end
end
@@ -170,7 +170,7 @@ module Qiniu
_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)
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.
@@ -318,6 +318,7 @@ module Qiniu
end
end
end
end
end
+2 -2
View File
@@ -4,8 +4,8 @@ module Qiniu
module RS
module Version
MAJOR = 3
MINOR = 2
PATCH = 2
MINOR = 3
PATCH = 0
# Returns a version string by joining <tt>MAJOR</tt>, <tt>MINOR</tt>, and <tt>PATCH</tt> with <tt>'.'</tt>
#
# Example
+33
View File
@@ -0,0 +1,33 @@
# -*- encoding: utf-8 -*-
require 'json'
require 'qiniu/tokens/access_token'
require 'qiniu/rs/utils'
module Qiniu
module RS
class DownloadToken < AccessToken
include Utils
attr_accessor :pattern, :expires_in
def initialize(opts = {})
@pattern = opts[:pattern] || "*"
@expires_in = opts[:expires_in] || 3600
end
def generate_signature
params = {"S" => @pattern, "E" => Time.now.to_i + @expires_in}
Utils.urlsafe_base64_encode(params.to_json)
end
def generate_token
signature = generate_signature
encoded_digest = generate_encoded_digest(signature)
%Q(#{@access_key}:#{encoded_digest}:#{signature})
end
end
end
end
+5 -3
View File
@@ -8,6 +8,8 @@ module Qiniu
module RS
class QboxToken < AccessToken
include Utils
attr_accessor :url, :params
def initialize(opts = {})
@@ -20,10 +22,10 @@ module Qiniu
signature = uri.path
query_string = uri.query
signature += '?' + query_string if !query_string.nil? && !query_string.empty?
signature += "\n";
signature += "\n"
if @params.is_a?(Hash)
total_param = @params.map { |key, value| %Q(#{CGI.escape(key.to_s)}=#{CGI.escape(value.to_s).gsub('+', '%20')}) }
signature += total_param.join("&")
params_string = Utils.generate_query_string(@params)
signature += params_string
end
signature
end
+1 -1
View File
@@ -27,7 +27,7 @@ module Qiniu
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)
Utils.urlsafe_base64_encode(params.to_json)
end
def generate_token
+1 -1
View File
@@ -11,7 +11,7 @@ module Qiniu
before :all do
@bucket = "io_test_bucket"
@key = Digest::SHA1.hexdigest (Time.now.to_i+rand(100)).to_s
@key = Digest::SHA1.hexdigest((Time.now.to_i+rand(100)).to_s)
result = Qiniu::RS.mkbucket(@bucket)
puts result.inspect
+1 -1
View File
@@ -12,7 +12,7 @@ module Qiniu
before :all do
@bucket = "rs_test_bucket"
@key = Digest::SHA1.hexdigest (Time.now.to_i+rand(100)).to_s
@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)
+1 -1
View File
@@ -11,7 +11,7 @@ module Qiniu
before :all do
@localfile = "bigfile.txt"
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)}}
@bucket = "up_test_bucket"
@key = Digest::SHA1.hexdigest(@localfile+Time.now.to_s)
+18 -3
View File
@@ -31,10 +31,13 @@ module Qiniu
end
after :all do
result = Qiniu::RS.drop(@bucket)
puts result.inspect
result = Qiniu::RS.unpublish(@domain)
result.should_not be_false
result1 = Qiniu::RS.drop(@bucket)
puts result1.inspect
result1.should_not be_false
result2 = Qiniu::RS.drop(@test_image_bucket)
puts result2.inspect
result2.should_not be_false
@@ -180,7 +183,7 @@ module Qiniu
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}
@@ -281,12 +284,14 @@ module Qiniu
end
end
=begin
context ".unpublish" do
it "should works" do
result = Qiniu::RS.unpublish(@domain)
result.should_not be_false
end
end
=end
context ".delete" do
it "should works" do
@@ -360,6 +365,16 @@ module Qiniu
data = Qiniu::RS.generate_upload_token({:scope => 'test_bucket', :expires_in => 3600, :escape => 0})
data.should_not be_empty
puts data.inspect
data.split(":").length.should == 3
end
end
context ".generate_download_token" do
it "should works" do
data = Qiniu::RS.generate_download_token({:expires_in => 1, :pattern => 'http://qiniu-rs-test.dn.qbox.me/*'})
data.should_not be_empty
puts data.inspect
data.split(":").length.should == 3
end
end