From 88339d5475568599074739e57c7c5978ba87d486 Mon Sep 17 00:00:00 2001 From: Kyle Rames Date: Thu, 11 Jul 2013 16:19:31 -0500 Subject: [PATCH] [openstack|storage] added the ability to upload files using blocks in the same manner as the Rackspace provider --- .../openstack/requests/storage/put_object.rb | 11 +++--- .../requests/storage/object_tests.rb | 35 +++++++++++++++++-- 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/lib/fog/openstack/requests/storage/put_object.rb b/lib/fog/openstack/requests/storage/put_object.rb index 40681c7bc..d6203a21e 100644 --- a/lib/fog/openstack/requests/storage/put_object.rb +++ b/lib/fog/openstack/requests/storage/put_object.rb @@ -11,19 +11,22 @@ module Fog # * data<~String|File> - data to upload # * options<~Hash> - config headers for object. Defaults to {}. # - def put_object(container, object, data, options = {}) + def put_object(container, object, data, options = {}, &block) data = Fog::Storage.parse_data(data) headers = data[:headers].merge!(options) - request( - :body => data[:body], + + params = block_given? ? { :request_block => block } : { :body => data[:body] } + + params.merge!( :expects => 201, :idempotent => true, :headers => headers, :method => 'PUT', :path => "#{Fog::OpenStack.escape(container)}/#{Fog::OpenStack.escape(object)}" ) - end + request(params) + end end end end diff --git a/tests/openstack/requests/storage/object_tests.rb b/tests/openstack/requests/storage/object_tests.rb index a6e2a972e..7a840c05e 100644 --- a/tests/openstack/requests/storage/object_tests.rb +++ b/tests/openstack/requests/storage/object_tests.rb @@ -17,9 +17,9 @@ Shindo.tests('Fog::Storage[:openstack] | object requests', ["openstack"]) do Fog::Storage[:openstack].put_object('fogobjecttests', 'fog_object', lorem_file) end - tests("#get_object('fogobjectests', 'fog_object')").succeeds do + tests("#get_object('fogobjectests', 'fog_object')").returns(lorem_file.read) do pending if Fog.mocking? - Fog::Storage[:openstack].get_object('fogobjecttests', 'fog_object') + Fog::Storage[:openstack].get_object('fogobjecttests', 'fog_object').body end tests("#get_object('fogobjecttests', 'fog_object', &block)").returns(lorem_file.read) do @@ -41,6 +41,37 @@ Shindo.tests('Fog::Storage[:openstack] | object requests', ["openstack"]) do Fog::Storage[:openstack].delete_object('fogobjecttests', 'fog_object') end + tests("put_object with block") do + tests("#put_object('fogobjecttests', 'fog_object', &block)") do + pending if Fog.mocking? + + begin + file = lorem_file + buffer_size = file.size / 2 # chop it up into two buffers + Fog::Storage[:openstack].put_object('fogobjecttests', 'fog_block_object', nil) do + if file.pos < file.size + file.sysread(buffer_size) + else + "" + end + end + ensure + file.close + end + end + + tests("object successfully uploaded?").returns(lorem_file.read) do + pending if Fog.mocking? + Fog::Storage[:openstack].get_object('fogobjecttests', 'fog_block_object').body + end + + tests("delete file").succeeds do + pending if Fog.mocking? + Fog::Storage[:openstack].delete_object('fogobjecttests', 'fog_block_object') + end + end + + end tests('failure') do