diff --git a/src/cloud/ec2/econe-upload b/src/cloud/ec2/econe-upload index 89bc9ad591..3072744c6b 100755 --- a/src/cloud/ec2/econe-upload +++ b/src/cloud/ec2/econe-upload @@ -22,6 +22,9 @@ # --url , -U : # 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}" diff --git a/src/cloud/ec2/lib/EC2QueryClient.rb b/src/cloud/ec2/lib/EC2QueryClient.rb index 695d051887..754f7ce58a 100644 --- a/src/cloud/ec2/lib/EC2QueryClient.rb +++ b/src/cloud/ec2/lib/EC2QueryClient.rb @@ -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