Well, as is so often the case, I have been proven wrong. AWS has just announced a new feature of S3 that lets you easily host static websites entirely on S3. The features are pretty simple to use. The basic process is:
- Create a bucket to hold your website (or use an existing one)
- Make sure the bucket is readable by the world
- Upload your website content including the default page (usually index.html) and a optional page to display in case of errors
- Configure your bucket for use as a website (using a new API call)
- Access your website via the new hostname S3 provides for website viewing. You can also create CNAME aliases, etc. to map the bucket name to your own domain name
The following Python code provides an example of all of the above steps.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import boto | |
from boto.s3.connection import Location | |
# | |
# create a couple of strings with our very minimal web content | |
# | |
index_html = """ | |
<html> | |
<head><title>My S3 Webpage</title></head> | |
<body><h2>This is my S3-based website</h2></body> | |
</html>""" | |
error_html = """ | |
<html> | |
<head><title>Something is wrong</title></head> | |
<body><h2>Something is terribly wrong with my S3-based website</h2></body> | |
</html>""" | |
# create a connection to S3 | |
conn = boto.connect_s3(host='s3-us-west-1.amazonaws.com') | |
# create a bucket and make it publicly readable | |
website_bucket = conn.create_bucket('garnaat-website-2', | |
location=Location.USWest, | |
policy='public-read') | |
# upload our HTML pages and make sure they are publicly readable | |
# also make sure Content-Type is set to text/html | |
index_key = website_bucket.new_key('index.html') | |
index_key.content_type = 'text/html' | |
index_key.set_contents_from_string(index_html, policy='public-read') | |
error_key = website_bucket.new_key('error.html') | |
error_key.content_type = 'text/html' | |
error_key.set_contents_from_string(error_html, policy='public-read') | |
# now set the website configuration for our bucket | |
website_bucket.configure_website('index.html', 'error.html') | |
time.sleep(5) | |
# now get the website configuration, just to check it | |
print website_bucket.get_website_configuration() |
I could now access my website using the following special link:
http://garnaat-website-2.s3-website-us-west-1.amazonaws.com/
I could also use the CNAME aliasing features of S3 to map my S3 website to my own domain (which is probably what most people will want to do). It's a great new feature for S3 and something that should prove useful to a lot of people.
http://garnaat-website-2.s3-website-us-west-1.amazonaws.com/
I could also use the CNAME aliasing features of S3 to map my S3 website to my own domain (which is probably what most people will want to do). It's a great new feature for S3 and something that should prove useful to a lot of people.
Nice feature, now we can host our sphinx docs in S3. Thinking about a make_s3 added to make_whatever in sphinx.
ReplyDeleteTrue. I just moved an old static website over. It took about 10 minutes. I used the s3put command to copy the entire directory over to S3, configured the bucket and then set up a couple of DNS records. Very cool.
ReplyDeleteIs Location.USWest a "must" or one can use "s3.amazonaws.com" ( connect_s3() without parameter ) and Location.DEFAULT instead?
ReplyDeleteThis should work with any location. I was just doing my initial testing in eu-west.
ReplyDeleteI tried with connect_s3() and location.DEFAULT but is not working for me. If you where using connect_s3() (empty) do you use the location.DEFAULT?
ReplyDeleteOr have you ever tried another zone?
There are 4 different s3 endpoint for each bucket location. so you have to set proper s3 website endpoint for different bucket location.
ReplyDeleteUS : s3-website-us-east-1.amazonaws.com
US West : s3-website-us-west-1.amazonaws.com
EU : s3-website-eu-west-1.amazonaws.com
Asia Pacific : s3-website-ap-southeast-1.amazonaws.com