Permalink
Browse files

website enabled bucket

  • Loading branch information...
1 parent 5e6f375 commit f1f97874f2d53d294ddc96a819166f565b4209fd @suratpyari suratpyari committed Mar 14, 2012
View
@@ -21,12 +21,14 @@
require 's3/service'
require 's3/owner'
require 's3/bucket'
+require 's3/website'
require 's3/object'
require 's3/error'
require 's3/exceptions'
require 's3/connection'
require 's3/authentication'
require 's3/response'
+require 's3/content'
AWS::S3::Base.class_eval do
include AWS::S3::Connection::Management
@@ -209,7 +209,7 @@ def path
end
def extract_significant_parameter
- request.path[/[&?](acl|torrent|logging)(?:&|=|$)/, 1]
+ request.path[/[&?](acl|torrent|logging|website|)(?:&|=|$)/, 1]
end
def only_path
View
@@ -64,6 +64,10 @@ class << self
# It is unlikely that you would call this method directly. Subclasses of Base have convenience methods for each http request verb
# that wrap calls to request.
def request(verb, path, options = {}, body = nil, attempts = 0, &block)
+ if options[:body] and body.nil?
+ body=options[:body]
+ # options.delete(:body)
+ end
Service.response = nil
process_options!(options, verb)
response = response_class.new(connection.request(verb, path, options, body, attempts, &block))
View
@@ -316,4 +316,4 @@ def reload!(options = {})
end
end
end
-end
+end
View
@@ -24,8 +24,7 @@ def initialize(options = {})
end
def request(verb, path, headers = {}, body = nil, attempts = 0, &block)
- body.rewind if body.respond_to?(:rewind) unless attempts.zero?
-
+ body.rewind if body.respond_to?(:rewind) unless attempts.zero?
requester = Proc.new do
path = self.class.prepare_path(path) if attempts.zero? # Only escape the path once
request = request_method(verb).new(path, headers)
@@ -275,4 +274,4 @@ def validate(options)
end
end
end
-end
+end
View
@@ -0,0 +1,23 @@
+module Content
+ attr_reader :object_cache #:nodoc:
+
+ include Enumerable
+
+ def initialize(attributes = {}) #:nodoc:
+ super
+ @object_cache = []
+ build_contents!
+ end
+
+ private
+ def build_contents!
+ return unless has_contents?
+ attributes.delete('contents').each do |content|
+ add new_object(content)
+ end
+ end
+
+ def has_contents?
+ attributes.has_key?('contents')
+ end
+end
View
@@ -66,4 +66,4 @@ def method_missing(method, *args, &block)
end
end
end
-#:startdoc:
+#:startdoc:
View
@@ -184,7 +184,7 @@ def copy(key, copy_key, bucket = nil, options = {})
source_key = path!(bucket, key)
default_options = {'x-amz-copy-source' => source_key}
target_key = path!(bucket, copy_key)
- returning put(target_key, default_options.merge(options)) do
+ returning put(target_key, default_options) do
acl(copy_key, bucket, acl(key, bucket)) if options[:copy_acl]
end
end
View
@@ -69,6 +69,14 @@ def bucket
end
end
end
+
+ class Website
+ class Response < Base::Response
+ def website
+ parsed
+ end
+ end
+ end
class S3Object
class Response < Base::Response
View
@@ -4,7 +4,7 @@ module VERSION #:nodoc:
MAJOR = '0'
MINOR = '6'
TINY = '2'
- BETA = Time.now.to_i.to_s
+ BETA = nil #Time.now.to_i.to_s
end
Version = [VERSION::MAJOR, VERSION::MINOR, VERSION::TINY, VERSION::BETA].compact * '.'
View
@@ -0,0 +1,102 @@
+require File.dirname(__FILE__) + "/content"
+module AWS
+ module S3
+ # To enable a bucket as a website you just specify its name.
+ #
+ # # Pick a existing bucket name, or else you'll get an error
+ # Website.create('jukebox')
+ # By default index document is "index.html" and error document is "error.html"
+ #
+ # If Its different you can do
+ # Website.create('jukebox', "about.html", "404.html")
+ #
+ #
+ # Once you have succesfully enabled as website you can you can fetch it by name using Website.find.
+ #
+ # music_website = Website.find('jukebox')
+ #
+ # The bucket that is not website enabled will will throw an error.
+ #
+ # You can remove website from bucket using Website.delete.
+ #
+ # Website.delete('jukebox')
+
+ class Website < Base
+
+ class Builder < XmlGenerator #:nodoc:
+ attr_reader :index_page, :error_page
+ def initialize(index_page, error_page)
+ @index_page = index_page
+ @error_page = error_page
+ super()
+ end
+
+ def build
+ xml.tag!('WebsiteConfiguration', 'xmlns' => 'http://s3.amazonaws.com/doc/2006-03-01/') do
+ xml.IndexDocument do
+ xml.Suffix index_page
+ end
+ xml.ErrorDocument do
+ xml.Key error_page
+ end
+ end
+ end
+ end
+
+ class << self
+ # To enable a bucket as a website you just specify its name.
+ #
+ # # Pick a existing bucket name, or else you'll get an error
+ # Website.create('jukebox')
+ # By default index document is "index.html" and error document is "error.html"
+ #
+ # If Its different you can do
+ # Website.create('jukebox', "about.html", "404.html")
+
+ def create(name=nil, index_page="index.html", error_page="error.html")
+ put(path(name), {}, Builder.new(index_page, error_page).to_s).success?
+ end
+
+ # Fetches if a bucket is website enabled.
+ #
+ # website=Website.find('jukebox')
+ #
+ # website.index_doc
+ # => 'index.html'
+ # website.error_doc
+ # => 'error.html'
+ def find(name = nil)
+ new(get(path(name)).website)
+ end
+
+ # disables a bucket aswebsite.
+ # Website.delete('photos')
+ def delete(name = nil, options = {})
+ Base.delete(path(name)).success?
+ end
+
+ private
+
+ def path(name, options = {})
+ if name.is_a?(Hash)
+ options = name
+ name = nil
+ end
+ # "/#{website_name(name)}#{RequestOptions.process(options).to_query_string}"
+ "/#{name}/?website"
+ end
+
+ include Content
+ end
+
+ def index_doc
+ self.index_document["suffix"]
+ end
+
+ def error_doc
+ self.error_document["key"]
+ end
+
+ end
+ end
+end

0 comments on commit f1f9787

Please sign in to comment.