Permalink
Browse files

Added website configuration support for S3 buckets.

  • Loading branch information...
1 parent 581543e commit 1797f0600cf952f94aa338934a99518f520e6ba3 @jeremyboles committed Dec 7, 2012
View
@@ -140,6 +140,7 @@ class S3
autoload :Tree, 'tree'
autoload :UploadedPart, 'uploaded_part'
autoload :UploadedPartCollection, 'uploaded_part_collection'
+ autoload :WebsiteConfiguration, 'website_configuration'
end
include Core::ServiceInterface
View
@@ -271,6 +271,20 @@ def tags
def tags= tags
self.tags.set(tags)
end
+
+ # @return [WebsiteConfiguration] Returns an object which can be used to
+ # set and delete website configuration information.
+ def website
+ WebsiteConfiguration.new(self)
+ end
+
+ # Sets the bucket's website configuration.
+ # @param (see WebsiteConfiguration#set)
+ # @see WebsiteConfiguration#set
+ def website=(config)
+ self.website.set(config)
+ nil
+ end
# @return [CORSRuleCollection] Returns a collection that can be
# used to manage (add, edit and delete) CORS rules for this bucket.
View
@@ -306,6 +306,79 @@ def self.object_method(method_name, verb, *args, &block)
super(req, options)
end
end
+
+ # @overload put_bucket_website(options = {})
+ # @param [Hash] options
+ # @option options [required,String] :bucket_name
+ # @option options [String] :index
+ # @option options [String] :error
+ bucket_method(:put_bucket_website, :put) do
+ configure_request do |req, options|
+
+ req.add_param('website')
+ xml = Nokogiri::XML::Builder.new
+ xml.WebsiteConfiguration do
+ xml.IndexDocument do
+ xml.Suffix(options[:index_document] || 'index.html')
+ end
+
+ xml.ErrorDocument do
+ xml.Key(options[:error_document] || 'error.html')
+ end
+ end
+
+ xml = xml.doc.root.to_xml
+ req.body = xml
+ req.headers['content-md5'] = md5(xml)
+
+ super(req, options)
+ end
+ end
+
+ # @overload get_bucket_website(options = {})
+ # @param [Hash] options
+ # @option options [required,String] :bucket_name
+ # @return [Core::Response]
+ bucket_method(:get_bucket_website, :get) do
+
+ configure_request do |req, options|
+ req.add_param('website')
+ super(req, options)
+ end
+
+ process_response do |resp|
+ resp.data = XML::GetBucketWebsite.parse(resp.http_response.body)
+ end
+
+ end
+
+ # @overload delete_bucket_website(options = {})
+ # @param [Hash] options
+ # @option options [required,String] :bucket_name
+ # @return [Core::Response]
+ bucket_method(:delete_bucket_website, :delete) do
+ configure_request do |req, options|
+ req.add_param('website')
+ super(req, options)
+ end
+ end
+
+ # @overload get_bucket_tagging(options = {})
+ # @param [Hash] options
+ # @option options [required,String] :bucket_name
+ # @return [Core::Response]
+ bucket_method(:get_bucket_tagging, :get) do
+
+ configure_request do |req, options|
+ req.add_param('tagging')
+ super(req, options)
+ end
+
+ process_response do |resp|
+ resp.data = XML::GetBucketTagging.parse(resp.http_response.body)
+ end
+
+ end
# @overload put_bucket_tagging(options = {})
# @param [Hash] options
@@ -206,7 +206,21 @@ module XML
end
end
end
-
+
+ GetBucketWebsite = BaseGrammar.customize do
+ element("WebsiteConfiguration") do
+ ignore
+ element("IndexDocument") do
+ ignore
+ element("Suffix") { rename :index }
+ end
+ element("ErrorDocument") do
+ ignore
+ element("Key") { rename :error }
+ end
+ end
+ end
+
end
end
end
@@ -180,7 +180,7 @@ class << self
def sub_resources
%w(acl location logging notification partNumber policy
requestPayment torrent uploadId uploads versionId
- versioning versions delete lifecycle tagging cors)
+ versioning versions delete lifecycle tagging cors website)
end
def query_parameters
@@ -0,0 +1,130 @@
+# Copyright 2011-2012 Amazon.com, Inc. or its affiliates. All Rights Reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License"). You
+# may not use this file except in compliance with the License. A copy of
+# the License is located at
+#
+# http://aws.amazon.com/apache2.0/
+#
+# or in the "license" file accompanying this file. This file is
+# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
+# ANY KIND, either express or implied. See the License for the specific
+# language governing permissions and limitations under the License.
+
+module AWS
+ class S3
+
+ # Manages the Website configuration for a single bucket.
+ #
+ # == Getting Rules
+ #
+ # To get the website configuration for a bucket, use the {Bucket#webite}
+ # method. This returns a WebsiteConfiguration for the bucket.
+ #
+ # # getting website configuration info
+ # bucket.website.index_document
+ # bucket.website.error_document
+ #
+ # == Setting Rules
+ #
+ # You can set the website configuration for a bucket (replacing existing
+ # configuration) using the {#set} method.
+ #
+ # # accepts a hash of documents
+ # bucket.website.set(error_document: 'error.html', index_document: 'index.html')
+ #
+ # # configuration can be set one at a time
+ # bucket.website.error_document = 'error.html'
+ # bucket.website.index_document = 'index.html'
+ #
+ # # passing an empty hash or nil object removes the configuration
+ # bucket.website.set({})
+ # bucket.website.set(nil)
+ # bucket.website.clear # does the same thing
+ #
+ class WebsiteConfiguration
+
+ # @param [Bucket] bucket
+ # @param [Hash] options
+ def initialize bucket, options = {}
+ @bucket = bucket
+ end
+
+ # @return [Bucket]
+ attr_reader :bucket
+
+ # @return [String]
+ def error_document
+ website[:error_document][:key] if website[:error_document]
+ end
+
+ # Sets the error document for the bucket website.
+ # @param [String]
+ def error_document=(document)
+ set(:error_document => document)
+ end
+
+ # @return [String]
+ def index_document
+ website[:index_document][:suffix] if website[:index_document]
+ end
+
+ # Sets the index document for the bucket website.
+ # @param [String]
+ def index_document=(document)
+ set(:index_document => document)
+ end
+
+ # Replaces the website configuration for this bucket.
+ #
+ # # replace all exisitng configuration
+ # bucket.cors.set(
+ # :index_document => 'index.html',
+ # :error_document => 'error.html')
+ #
+ # If you pass an empty hash or nil object, the configuration will be
+ # removed from the bucket.
+ #
+ # # these three lines are equivilent
+ # bucket.website.delete
+ # bucket.website = {}
+ # bucket.website = nil
+ #
+ # @param [Hash] options
+ # @return [nil]
+ #
+ def set(options={})
+ if options.nil? || options.empty?
+ delete
+ else
+ options[:error_document] ||= error_document
+ options[:index_document] ||= index_document
+ # puts "Options"
+ # puts options
+ client.put_bucket_website(options.merge(:bucket_name => bucket.name))
+ end
+ @website = nil
+ nil
+ end
+
+ # Removes all website configuration attached to this bucket.
+ #
+ # @return [nil]
+ def delete
+ client.delete_bucket_website(:bucket_name => bucket.name)
+ nil
+ end
+
+ private
+
+ def client
+ bucket.client
+ end
+
+ def website
+ @website ||= client.get_bucket_website(:bucket_name => bucket.name)
+ end
+
+ end
+ end
+end
@@ -181,6 +181,43 @@ class S3
end
end
+
+ context '#website=' do
+
+ it 'calls #put_bucket_website' do
+
+ options = {
+ :index_document => 'index.html',
+ :error_document => 'error.html' }
+
+ client.should_receive(:put_bucket_website).
+ with(:bucket_name => bucket.name,
+ :index_document => 'index.html',
+ :error_document => 'error.html')
+
+ bucket.website = options
+
+ end
+
+ it 'calls #delete_bucket_website when passed a empty hash' do
+
+ client.should_receive(:delete_bucket_website).
+ with(:bucket_name => bucket.name)
+
+ bucket.website = {}
+
+ end
+
+ it 'calls #delete_bucket_website when website is nil' do
+
+ client.should_receive(:delete_bucket_website).
+ with(:bucket_name => bucket.name)
+
+ bucket.website = nil
+
+ end
+
+ end
context '#tags=' do
Oops, something went wrong. Retry.

0 comments on commit 1797f06

Please sign in to comment.