From 2b677d4dbd878358082a02a438f4dfa575c80f1f Mon Sep 17 00:00:00 2001 From: mrloop Date: Sat, 7 Feb 2015 12:57:19 +0000 Subject: [PATCH] Making it easier to get pre-signed head requests Very similar to get_url. https://github.com/fog/fog-aws/issues/47 --- lib/fog/aws/models/storage/files.rb | 5 +++ .../aws/requests/storage/head_object_url.rb | 40 +++++++++++++++++++ lib/fog/aws/storage.rb | 1 + tests/requests/storage/object_tests.rb | 6 +++ 4 files changed, 52 insertions(+) create mode 100644 lib/fog/aws/requests/storage/head_object_url.rb diff --git a/lib/fog/aws/models/storage/files.rb b/lib/fog/aws/models/storage/files.rb index 3498485b4..a7c2fc26e 100644 --- a/lib/fog/aws/models/storage/files.rb +++ b/lib/fog/aws/models/storage/files.rb @@ -92,6 +92,11 @@ module Fog service.get_object_https_url(directory.key, key, expires, options) end + def head_url(key, expires, options = {}) + requires :directory + service.head_object_url(directory.key, key, expires, options) + end + def head(key, options = {}) requires :directory data = service.head_object(directory.key, key, options) diff --git a/lib/fog/aws/requests/storage/head_object_url.rb b/lib/fog/aws/requests/storage/head_object_url.rb new file mode 100644 index 000000000..3b85a02c2 --- /dev/null +++ b/lib/fog/aws/requests/storage/head_object_url.rb @@ -0,0 +1,40 @@ +module Fog + module Storage + class AWS + module HeadObjectUrl + def head_object_url(bucket_name, object_name, expires, options = {}) + unless bucket_name + raise ArgumentError.new('bucket_name is required') + end + unless object_name + raise ArgumentError.new('object_name is required') + end + signed_url(options.merge({ + :bucket_name => bucket_name, + :object_name => object_name, + :method => 'HEAD' + }), expires) + end + end + + class Real + # An expiring head request url from S3 + # + # @param bucket_name [String] Name of bucket containing object + # @param object_name [String] Name of object to get expiring url for + # @param expires [Time] An expiry time for this url + # + # @return [Excon::Response] response: + # * body [String] - url for object + # + # @see http://docs.amazonwebservices.com/AmazonS3/latest/dev/S3_QSAuth.html + + include HeadObjectUrl + end + + class Mock # :nodoc:all + include HeadObjectUrl + end + end + end +end diff --git a/lib/fog/aws/storage.rb b/lib/fog/aws/storage.rb index bfb1b6d2e..4fd74ea69 100644 --- a/lib/fog/aws/storage.rb +++ b/lib/fog/aws/storage.rb @@ -88,6 +88,7 @@ module Fog request :get_service request :head_bucket request :head_object + request :head_object_url request :initiate_multipart_upload request :list_multipart_uploads request :list_parts diff --git a/tests/requests/storage/object_tests.rb b/tests/requests/storage/object_tests.rb index 417b53796..b9c602bb0 100644 --- a/tests/requests/storage/object_tests.rb +++ b/tests/requests/storage/object_tests.rb @@ -118,6 +118,12 @@ Shindo.tests('AWS::Storage | object requests', ['aws']) do (object_url =~ /http:\/\/#{Regexp.quote(@directory.identity)}\.s3\.amazonaws\.com\/fog_object/) != nil end + tests("#head_object_url('#{@directory.identity}', 'fog_object', expiration timestamp)").returns(true) do + object_url = Fog::Storage[:aws].head_object_url(@directory.identity, 'fog_object', (Time.now + 60)) + puts object_url + (object_url =~ /https:\/\/#{Regexp.quote(@directory.identity)}\.s3\.amazonaws\.com\/fog_object/) != nil + end + tests("delete_multiple_objects('#{@directory.identity}', ['fog_object', 'fog_other_object'])").formats(@multiple_delete_format) do Fog::Storage[:aws].delete_multiple_objects(@directory.identity, ['fog_object', 'fog_other_object']).body end