Add support for AWS bucket website redirects

To use, add :redirect_to => REDIRECT_TARGET to options hash for
put_bucket_website, e.g.:
connection.put_bucket_website("www.example.com", nil, :redirect_to =>
"example.com")
This commit is contained in:
Nils Landt 2013-03-21 19:25:24 +01:00
parent c5fa5ccfcc
commit 8bab98f147
3 changed files with 65 additions and 19 deletions

View File

@ -2,9 +2,11 @@ module Fog
module Parsers module Parsers
module Storage module Storage
module AWS module AWS
# http://docs.aws.amazon.com/AmazonS3/latest/API/RESTBucketGETwebsite.html
class GetBucketWebsite < Fog::Parsers::Base class GetBucketWebsite < Fog::Parsers::Base
def reset def reset
@response = { 'ErrorDocument' => {}, 'IndexDocument' => {} } @response = { 'ErrorDocument' => {}, 'IndexDocument' => {}, 'RedirectAllRequestsTo' => {} }
end end
def end_element(name) def end_element(name)
@ -13,6 +15,8 @@ module Fog
@response['ErrorDocument'][name] = value @response['ErrorDocument'][name] = value
when 'Suffix' when 'Suffix'
@response['IndexDocument'][name] = value @response['IndexDocument'][name] = value
when 'HostName'
@response['RedirectAllRequestsTo'][name] = value
end end
end end
end end

View File

@ -5,29 +5,57 @@ module Fog
# Change website configuration for an S3 bucket # Change website configuration for an S3 bucket
# #
# @param bucket_name [String] name of bucket to modify # @param bucket_name [String] name of bucket to modify
# @param suffix [String] suffix to append to requests for the bucket
# @param options [Hash] # @param options [Hash]
# @option options key [String] key to use for 4XX class errors # @option options RedirectAllRequestsTo [String] Host name to redirect all requests to - if this is set, other options are ignored
# @option options IndexDocument [String] suffix to append to requests for the bucket
# @option options ErrorDocument [String] key to use for 4XX class errors
# #
# @see http://docs.amazonwebservices.com/AmazonS3/latest/API/RESTBucketPUTwebsite.html # @see http://docs.amazonwebservices.com/AmazonS3/latest/API/RESTBucketPUTwebsite.html
def put_bucket_website(bucket_name, suffix, options = {}) def put_bucket_website(bucket_name, options, options_to_be_deprecated = {})
data = options ||= {}
<<-DATA
<WebsiteConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/"> # Method used to be called with the suffix as the second parameter. Warn user that this is not the case any more and move on
if options.is_a?(String)
Fog::Logger.deprecation("put_bucket_website with #{options.class} param is deprecated, use put_bucket_website('#{bucket_name}', :IndexDocument => '#{options}') instead [light_black](#{caller.first})[/]")
options = { :IndexDocument => options }
end
# Parameter renamed from "key" to "ErrorDocument"
if options_to_be_deprecated[:key]
Fog::Logger.deprecation("put_bucket_website with three parameters is deprecated, use put_bucket_website('#{bucket_name}', :ErrorDocument => '#{options_to_be_deprecated[:key]}') instead [light_black](#{caller.first})[/]")
options[:ErrorDocument] = options_to_be_deprecated[:key]
end
options.merge!(options_to_be_deprecated) { |key, o1, o2| o1 }
data = "<WebsiteConfiguration xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\">"
if options[:RedirectAllRequestsTo]
# Redirect precludes all other options
data << <<-DATA
<RedirectAllRequestsTo>
<HostName>#{options[:RedirectAllRequestsTo]}</HostName>
</RedirectAllRequestsTo>
DATA
else
if options[:IndexDocument]
data << <<-DATA
<IndexDocument> <IndexDocument>
<Suffix>#{suffix}</Suffix> <Suffix>#{options[:IndexDocument]}</Suffix>
</IndexDocument> </IndexDocument>
DATA DATA
end
if options[:key] if options[:ErrorDocument]
data << data << <<-DATA
<<-DATA
<ErrorDocument> <ErrorDocument>
<Key>#{options[:key]}</Key> <Key>#{options[:ErrorDocument]}</Key>
</ErrorDocument> </ErrorDocument>
DATA DATA
end end
end
data << '</WebsiteConfiguration>' data << '</WebsiteConfiguration>'
request({ request({

View File

@ -137,10 +137,19 @@ Shindo.tests('Fog::Storage[:aws] | bucket requests', ["aws"]) do
Fog::Storage[:aws].put_request_payment(@aws_bucket_name, 'Requester') Fog::Storage[:aws].put_request_payment(@aws_bucket_name, 'Requester')
end end
# This should show a warning, but work (second parameter is options hash for now)
tests("#put_bucket_website('#{@aws_bucket_name}', 'index.html')").succeeds do tests("#put_bucket_website('#{@aws_bucket_name}', 'index.html')").succeeds do
Fog::Storage[:aws].put_bucket_website(@aws_bucket_name, 'index.html') Fog::Storage[:aws].put_bucket_website(@aws_bucket_name, 'index.html')
end end
tests("#put_bucket_website('#{@aws_bucket_name}', :IndexDocument => 'index.html')").succeeds do
Fog::Storage[:aws].put_bucket_website(@aws_bucket_name, :IndexDocument => 'index.html')
end
tests("#put_bucket_website('#{@aws_bucket_name}', :RedirectAllRequestsTo => 'redirect.example..com')").succeeds do
Fog::Storage[:aws].put_bucket_website(@aws_bucket_name, :RedirectAllRequestsTo => 'redirect.example.com')
end
tests("#put_bucket_acl('#{@aws_bucket_name}', 'private')").succeeds do tests("#put_bucket_acl('#{@aws_bucket_name}', 'private')").succeeds do
Fog::Storage[:aws].put_bucket_acl(@aws_bucket_name, 'private') Fog::Storage[:aws].put_bucket_acl(@aws_bucket_name, 'private')
end end
@ -358,6 +367,11 @@ Shindo.tests('Fog::Storage[:aws] | bucket requests', ["aws"]) do
storage_eu_endpoint.put_bucket(@aws_bucket_name) storage_eu_endpoint.put_bucket(@aws_bucket_name)
end end
end end
tests("#put_bucket_website('fognonbucket', :RedirectAllRequestsTo => 'redirect.example.com')").raises(Excon::Errors::NotFound) do
Fog::Storage[:aws].put_bucket_website('fognonbucket', :RedirectAllRequestsTo => 'redirect.example.com')
end
end end
# don't keep the bucket around # don't keep the bucket around