mirror of
https://github.com/OpenNebula/one.git
synced 2026-11-19 03:12:55 +09:00
Added an option to use "multipart-post" instead of Curb/Curl library
git-svn-id: http://svn.opennebula.org/one/trunk@790 3034c82b-c49b-4eb3-8279-a7acafdc01c0
This commit is contained in:
1 parent
dc96f3b844
commit
f775db264c
2 files changed
+54
-15
No files matched your search
@@ -22,6 +22,9 @@
|
||||
# --url <url>, -U <url>:
|
||||
# Set url as the web service url to use
|
||||
#
|
||||
# --multipart, -M:
|
||||
# Use 'multipart-post' library instead of Curb/Curl
|
||||
#
|
||||
# IMAGE-PATH: Path to the image to upload
|
||||
|
||||
# -------------------------------------------------------------------------- #
|
||||
@@ -63,13 +66,15 @@ opts = GetoptLong.new(
|
||||
['--help', '-h',GetoptLong::NO_ARGUMENT],
|
||||
['--access-key', '-K',GetoptLong::REQUIRED_ARGUMENT],
|
||||
['--secret-key', '-S',GetoptLong::REQUIRED_ARGUMENT],
|
||||
['--url', '-U',GetoptLong::REQUIRED_ARGUMENT]
|
||||
['--url', '-U',GetoptLong::REQUIRED_ARGUMENT],
|
||||
['--multipart', '-M',GetoptLong::NO_ARGUMENT]
|
||||
)
|
||||
|
||||
url = nil
|
||||
access = nil
|
||||
secret = nil
|
||||
auth = nil
|
||||
curb = true
|
||||
|
||||
begin
|
||||
opts.each do |opt, arg|
|
||||
@@ -82,6 +87,8 @@ begin
|
||||
secret = arg
|
||||
when '--url'
|
||||
url = arg
|
||||
when '--multipart'
|
||||
curb = false
|
||||
end
|
||||
end
|
||||
rescue Exception => e
|
||||
@@ -104,7 +111,7 @@ rescue Exception => e
|
||||
exit -1
|
||||
end
|
||||
|
||||
rc = ec2_client.upload_image(image)
|
||||
rc = ec2_client.upload_image(image, curb)
|
||||
|
||||
if OpenNebula::is_error?(rc)
|
||||
puts "#{$0}: #{rc.message}"
|
||||
|
||||
@@ -9,10 +9,21 @@ end
|
||||
|
||||
require 'rubygems'
|
||||
require 'AWS'
|
||||
require 'curb'
|
||||
require 'uri'
|
||||
require 'OpenNebula'
|
||||
|
||||
begin
|
||||
require 'curb'
|
||||
CURL_LOADED=true
|
||||
rescue LoadError
|
||||
CURL_LOADED=false
|
||||
end
|
||||
|
||||
begin
|
||||
require 'net/http/post/multipart'
|
||||
rescue LoadError
|
||||
end
|
||||
|
||||
module EC2QueryClient
|
||||
###########################################################################
|
||||
#
|
||||
@@ -126,7 +137,7 @@ module EC2QueryClient
|
||||
#
|
||||
# Returns true if HTTP code is 200,
|
||||
#######################################################################
|
||||
def upload_image(file_name)
|
||||
def upload_image(file_name, curb=true)
|
||||
params = { "Action" => "UploadImage",
|
||||
"SignatureVersion" => "2",
|
||||
"SignatureMethod" => 'HmacSHA1',
|
||||
@@ -139,22 +150,43 @@ module EC2QueryClient
|
||||
|
||||
post_fields = Array.new;
|
||||
|
||||
params.each { |k,v|
|
||||
post_fields << Curl::PostField.content(k,v)
|
||||
}
|
||||
if curb and CURL_LOADED
|
||||
params.each { |k,v|
|
||||
post_fields << Curl::PostField.content(k,v)
|
||||
}
|
||||
|
||||
post_fields << Curl::PostField.content("Signature",sig)
|
||||
post_fields << Curl::PostField.file("file",file_name)
|
||||
post_fields << Curl::PostField.content("Signature",sig)
|
||||
post_fields << Curl::PostField.file("file",file_name)
|
||||
|
||||
connection = Curl::Easy.new(@uri.to_s)
|
||||
connection.multipart_form_post = true
|
||||
connection = Curl::Easy.new(@uri.to_s)
|
||||
connection.multipart_form_post = true
|
||||
|
||||
connection.http_post(*post_fields)
|
||||
connection.http_post(*post_fields)
|
||||
|
||||
if connection.response_code == 200
|
||||
return AWS::Response.parse(:xml => connection.body_str)
|
||||
if connection.response_code == 200
|
||||
return AWS::Response.parse(:xml => connection.body_str)
|
||||
else
|
||||
return OpenNebula::Error.new(connection.body_str)
|
||||
end
|
||||
else
|
||||
return OpenNebula::Error.new(connection.body_str)
|
||||
params["Signature"]=sig
|
||||
|
||||
file=File.open(file_name)
|
||||
params["file"]=UploadIO.new(file,
|
||||
'application/octet-stream', file_name)
|
||||
|
||||
req = Net::HTTP::Post::Multipart.new('/', params)
|
||||
res = Net::HTTP.start(@uri.host, @uri.port) do |http|
|
||||
http.request(req)
|
||||
end
|
||||
|
||||
file.close
|
||||
|
||||
if res.code == '200'
|
||||
return AWS::Response.parse(:xml => res.body)
|
||||
else
|
||||
return OpenNebula::Error.new(res.body)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in new issue
Block a user