From 2c23e250ea1e98a68988c181fd21e74f052f10c1 Mon Sep 17 00:00:00 2001 From: Bradley Schaefer Date: Mon, 8 Apr 2013 19:51:47 -0500 Subject: [PATCH 1/4] Add post_object_restore support --- .../requests/storage/post_object_restore.rb | 54 +++++++++++++++++++ lib/fog/aws/storage.rb | 6 ++- 2 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 lib/fog/aws/requests/storage/post_object_restore.rb diff --git a/lib/fog/aws/requests/storage/post_object_restore.rb b/lib/fog/aws/requests/storage/post_object_restore.rb new file mode 100644 index 000000000..e50ee4730 --- /dev/null +++ b/lib/fog/aws/requests/storage/post_object_restore.rb @@ -0,0 +1,54 @@ +module Fog + module Storage + class AWS + class Real + + # Restore an object from Glacier to its original S3 path + # + # @param bucket_name [String] Name of bucket containing object + # @param object_name [String] Name of object to restore + # @option days [Integer] Number of days to restore object for. Defaults to 100000 (a very long time) + # + # @return [Excon::Response] response: + # * status [Integer] 200 + # + # @see http://docs.amazonwebservices.com/AmazonS3/latest/API/RESTObjectPOSTrestore.html + # + def post_object_restore(bucket_name, object_name, days = 100000) + unless bucket_name + raise ArgumentError.new('bucket_name is required') + end + unless object_name + raise ArgumentError.new('object_name is required') + end + + data = '' + days.to_s + '' + + headers = {} + headers['Content-MD5'] = Base64.encode64(Digest::MD5.digest(data)).strip + headers['Content-Type'] = 'application/xml' + headers['Date'] = Fog::Time.now.to_date_header + + request({ + :headers => headers, + :host => "#{bucket_name}.#{@host}", + :expect => 202, + :body => data, + :method => 'POST', + :query => {'restore' => nil}, + :path => CGI.escape(object_name) + }) + end + + end + + class Mock # :nodoc:all + + def post_object_restore(bucket_name, object_name, days = 100000) + raise "TODO" + end + + end + end + end +end diff --git a/lib/fog/aws/storage.rb b/lib/fog/aws/storage.rb index 74b1f3bfa..134e10845 100644 --- a/lib/fog/aws/storage.rb +++ b/lib/fog/aws/storage.rb @@ -33,6 +33,7 @@ module Fog response-content-language response-content-type response-expires + restore torrent uploadId uploads @@ -87,6 +88,7 @@ module Fog request :list_multipart_uploads request :list_parts request :post_object_hidden_fields + request :post_object_restore request :put_bucket request :put_bucket_acl request :put_bucket_cors @@ -217,7 +219,7 @@ module Fog :path => path, :headers => headers, }) - + # ret.delete(:path_style) ret.delete(:bucket_name) @@ -490,7 +492,7 @@ DATA expires = Fog::Time.now.to_date_header signature = signature(params, expires) - + params = request_params(params) params.delete(:port) unless params[:port] From 121f1ce294f650f9f88a3f8ebe677fc8ecb796b4 Mon Sep 17 00:00:00 2001 From: Bradley Schaefer Date: Mon, 8 Apr 2013 20:40:06 -0500 Subject: [PATCH 2/4] Fix excon expects syntax for post object restore --- lib/fog/aws/requests/storage/post_object_restore.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/fog/aws/requests/storage/post_object_restore.rb b/lib/fog/aws/requests/storage/post_object_restore.rb index e50ee4730..47280f848 100644 --- a/lib/fog/aws/requests/storage/post_object_restore.rb +++ b/lib/fog/aws/requests/storage/post_object_restore.rb @@ -32,7 +32,7 @@ module Fog request({ :headers => headers, :host => "#{bucket_name}.#{@host}", - :expect => 202, + :expects => [200, 202], :body => data, :method => 'POST', :query => {'restore' => nil}, From a181b375ebf9d0d32b45d892f74967591c2b2abe Mon Sep 17 00:00:00 2001 From: Bradley Schaefer Date: Mon, 10 Jun 2013 16:28:29 -0500 Subject: [PATCH 3/4] Add 409 (Conflict) as an expected restore response --- .../aws/requests/storage/post_object_restore.rb | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/lib/fog/aws/requests/storage/post_object_restore.rb b/lib/fog/aws/requests/storage/post_object_restore.rb index 47280f848..5679c0208 100644 --- a/lib/fog/aws/requests/storage/post_object_restore.rb +++ b/lib/fog/aws/requests/storage/post_object_restore.rb @@ -10,17 +10,15 @@ module Fog # @option days [Integer] Number of days to restore object for. Defaults to 100000 (a very long time) # # @return [Excon::Response] response: - # * status [Integer] 200 + # * status [Integer] 200 (OK) Object is previously restored + # * status [Integer] 202 (Accepted) Object is not previously restored + # * status [Integer] 409 (Conflict) Restore is already in progress # # @see http://docs.amazonwebservices.com/AmazonS3/latest/API/RESTObjectPOSTrestore.html # def post_object_restore(bucket_name, object_name, days = 100000) - unless bucket_name - raise ArgumentError.new('bucket_name is required') - end - unless object_name - raise ArgumentError.new('object_name is required') - end + raise ArgumentError.new('bucket_name is required') unless bucket_name + raise ArgumentError.new('object_name is required') unless object_name data = '' + days.to_s + '' @@ -32,7 +30,7 @@ module Fog request({ :headers => headers, :host => "#{bucket_name}.#{@host}", - :expects => [200, 202], + :expects => [200, 202, 409], :body => data, :method => 'POST', :query => {'restore' => nil}, From 53b360d611015937ced05a814f4a113b73486d9e Mon Sep 17 00:00:00 2001 From: Bradley Schaefer Date: Mon, 10 Jun 2013 16:50:52 -0500 Subject: [PATCH 4/4] Very basic mock for post_object_restore --- lib/fog/aws/requests/storage/post_object_restore.rb | 4 +++- tests/aws/requests/storage/object_tests.rb | 7 +++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/fog/aws/requests/storage/post_object_restore.rb b/lib/fog/aws/requests/storage/post_object_restore.rb index 5679c0208..944ab7afd 100644 --- a/lib/fog/aws/requests/storage/post_object_restore.rb +++ b/lib/fog/aws/requests/storage/post_object_restore.rb @@ -43,7 +43,9 @@ module Fog class Mock # :nodoc:all def post_object_restore(bucket_name, object_name, days = 100000) - raise "TODO" + response = get_object(bucket_name, object_name) + response.body = nil + response end end diff --git a/tests/aws/requests/storage/object_tests.rb b/tests/aws/requests/storage/object_tests.rb index 6a76fb409..42b03fec9 100644 --- a/tests/aws/requests/storage/object_tests.rb +++ b/tests/aws/requests/storage/object_tests.rb @@ -46,6 +46,10 @@ Shindo.tests('AWS::Storage | object requests', ['aws']) do Fog::Storage[:aws].head_object(@directory.identity, 'fog_object') end + tests("#post_object_restore('#{@directory.identity}', 'fog_object')").succeeds do + Fog::Storage[:aws].post_object_restore(@directory.identity, 'fog_object') + end + tests("#put_object_acl('#{@directory.identity}', 'fog_object', 'private')").succeeds do Fog::Storage[:aws].put_object_acl(@directory.identity, 'fog_object', 'private') end @@ -160,6 +164,9 @@ Shindo.tests('AWS::Storage | object requests', ['aws']) do Fog::Storage[:aws].put_object_acl('#{@directory.identity}', 'fog_object', 'invalid') end + tests("#post_object_restore('#{@directory.identity}', 'fog_non_object')").raises(Excon::Errors::NotFound) do + Fog::Storage[:aws].post_object_restore(@directory.identity, 'fog_non_object') + end end @directory.destroy