This commit is contained in:
404
2012-07-31 10:14:10 +08:00
parent da3bdd362e
commit 1a9dfeb9a7
8 changed files with 54 additions and 26 deletions
+12 -4
View File
@@ -66,6 +66,18 @@ module Qiniu
code == StatusOK
end
def upload_with_token opts = {}
code, data = IO.upload_with_token(opts[:uptoken],
opts[:file],
opts[:bucket],
opts[:key],
opts[:mime_type],
opts[:note],
opts[:callback_params],
opts[:enable_crc32_check])
code == StatusOK ? data : false
end
def stat(bucket, key)
code, data = RS.stat(bucket, key)
code == StatusOK ? data : false
@@ -152,10 +164,6 @@ module Qiniu
code == StatusOK ? data : false
end
#def generate_upload_token(scope, expires_in, callback_url = nil, return_url = nil)
# Utils.generate_upload_token(scope, expires_in, callback_url, return_url)
#end
def generate_upload_token(opts = {})
token_obj = UploadToken.new(opts)
token_obj.access_key = Config.settings[:access_key]
+1
View File
@@ -22,6 +22,7 @@ module Qiniu
:auth_url => "https://acc.qbox.me/oauth2/token",
:rs_host => "http://rs.qbox.me:10100",
:io_host => "http://iovip.qbox.me",
:up_host => "http://m1.qbox.me:13019",
:client_id => "a75604760c4da4caaa456c0c5895c061c3065c5a",
:client_secret => "75df554a39f58accb7eb293b550fa59618674b7d",
:access_key => "",
+8
View File
@@ -34,6 +34,14 @@ module Qiniu
Auth.request url, ::IO.read(local_file), options
end
def upload_with_token(uptoken, local_file, bucket, key = nil, mime_type = nil, custom_meta = nil, callback_params = nil, enable_crc32_check = false)
action_params = _generate_action_params(local_file, bucket, key, mime_type, custom_meta, enable_crc32_check)
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)
raise NoSuchFileError, local_file unless File.exist?(local_file)
+2 -16
View File
@@ -91,13 +91,14 @@ module Qiniu
end
end
def upload_multipart_data(url, filepath, action_string, callback_query_string = '')
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
@@ -129,21 +130,6 @@ module Qiniu
%Q(#{access_key}:#{encoded_digest})
end
=begin
def generate_upload_token(scope, expires_in, callback_url = nil, return_url = nil)
access_key = Config.settings[:access_key]
secret_key = Config.settings[:secret_key]
params = {:scope => scope, :deadline => Time.now.to_i + expires_in}
params[:callbackUrl] = callback_url if !callback_url.nil? && !calback_url.empty?
params[:returnUrl] = return_url if !return_url.nil? && !return_url.empty?
signature = urlsafe_base64_encode(params.to_json)
hmac = HMAC::SHA1.new(secret_key)
hmac.update(signature)
encoded_digest = urlsafe_base64_encode(hmac.digest)
%Q(#{access_key}:#{encoded_digest}:#{signature})
end
=end
end
end
end
+1 -1
View File
@@ -2,6 +2,6 @@
module Qiniu
module RS
VERSION = "2.3.3"
VERSION = "2.4.0"
end
end
+10
View File
@@ -44,6 +44,16 @@ module Qiniu
end
end
context ".upload_with_token" do
it "should works" do
upopts = {:scope => @bucket, :expires_in => 3600, :callback_url => "http://localhost:4567"}
uptoken = Qiniu::Auth.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
+16
View File
@@ -71,6 +71,22 @@ module Qiniu
end
end
context ".upload_with_token" do
it "should works" do
uptoken_opts = {:scope => @bucket, :expires_in => 3600, :callback_url => "http://localhost:4567"}
upload_opts = {
:uptoken => Qiniu::RS.generate_upload_token(uptoken_opts),
:file => __FILE__,
:bucket => @bucket,
:key => @key,
:enable_crc32_check => true
}
result = Qiniu::RS.upload_with_token(upload_opts)
result.should_not be_false
puts result.inspect
end
end
context ".stat" do
it "should works" do
result = Qiniu::RS.stat(@bucket, @key)
+4 -5
View File
@@ -6,15 +6,14 @@ 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 => "3fPHl_SLkPXdioqI_A8_NGngPWVJhlDk2ktRjogH",
# :secret_key => "bXTPMDJrVYRJUiSDRFtFYwycVD_mjXxYWrCYlDHy"
=begin
Qiniu::RS.establish_connection! :access_key => "bE21M6FW9V7zAFrBY5psgKOKJQLiBj12qMWTpc57",
:secret_key => "uMo7Nyq_eDK_CuQ8_FYCxoTHQZqjiaPh-cbiKO7L",
:auth_url => "http://m1.qbox.me:13001/oauth2/token",
:rs_host => "http://m1.qbox.me:13003",
:io_host => "http://m1.qbox.me:13004"
=end
:io_host => "http://m1.qbox.me:13004",
:up_host => "http://m1.qbox.me:13019"
end
end