added Qiniu::RS.put_file()

This commit is contained in:
404
2012-07-24 16:54:09 +08:00
parent 313e368602
commit 7df449cc73
7 changed files with 81 additions and 27 deletions
+13 -3
View File
@@ -35,13 +35,23 @@ module Qiniu
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
+19 -5
View File
@@ -20,20 +20,34 @@ 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)
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
post_data = {:file => File.new(local_file, 'rb'), :multipart => true}
options = {:qbox_signature_token => Utils.generate_qbox_signature(url, nil)}
Utils.send_multipart_request url, post_data, options
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)
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
end
end
+20 -7
View File
@@ -91,19 +91,22 @@ module Qiniu
end
end
def upload_multipart_data(url, filepath, action_string, callback_query_string = '')
def send_multipart_request(url, post_data, options = {})
code, data = 0, {}
begin
header_options = {
:accept => :json,
:user_agent => Config.settings[:user_agent]
}
post_data = {
:file => File.new(filepath, 'rb'),
:params => callback_query_string,
:action => action_string,
:multipart => true
}
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]
auth_token = 'Bearer ' + options[:access_token]
end
header_options.merge!('Authorization' => auth_token) unless auth_token.nil?
response = RestClient.post url, post_data, header_options
body = response.respond_to?(:body) ? response.body : ""
data = safe_json_parse(body) unless body.empty?
@@ -123,6 +126,16 @@ module Qiniu
[code, data]
end
def upload_multipart_data(url, filepath, action_string, callback_query_string = '')
post_data = {
:file => File.new(filepath, 'rb'),
:params => callback_query_string,
:action => action_string,
:multipart => true
}
send_multipart_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 }
+1 -1
View File
@@ -2,6 +2,6 @@
module Qiniu
module RS
VERSION = "2.2.0"
VERSION = "2.3.0"
end
end
+14 -9
View File
@@ -19,21 +19,26 @@ module Qiniu
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"
@key = Digest::SHA1.hexdigest (Time.now.to_i+rand(100)).to_s
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
+2 -2
View File
@@ -33,9 +33,9 @@ module Qiniu
@domain = 'cdn.example.com'
end
context "IO.put_file" do
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.upload_file(@put_url, __FILE__, @bucket, @key)
code.should == 200
puts data.inspect
end
+12
View File
@@ -43,11 +43,23 @@ 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 ".stat" do
it "should works" do
result = Qiniu::RS.stat(@bucket, @key)