While Amazon AWS S3 are usually used to store files and documents (objects are stored in buckets), users can easily create their own static website by configure a bucket to host the webpage. The first step is to sign up for an Amazon AWS account. User will get to enjoy the free-tier version for the 1st year.
The detailed guide for setting up the static website are provided in the amazon AWS link. Below list the main steps:
- Create a bucket. Note that if we have our own registered domain name, we will need to ensure the bucket name is same as the domain name. See additional steps in link for mapping the domain name to the bucket url.
- Upload two files (index.html and error.html by default, we can specify other names but have to align with step 3 below) to the bucket. The index.html will be the landing page.
- Under bucket properties, select static website hosting. After which we will need to set the main page (index.html) and error page (eg error.html). This will allow the bucket to open the page (index.html) upon visiting the given url.
- Note that all objects (including image, video or wav files) in bucket have a particular url.
- Enable public access on either every single object by clicking on objects-> permission or public access to whole bucket by setting the bucket policy.
- Note that there will be charges for storage and also for GET/POST requests.
A basic index.html can be as simple as below or it can be much more complicated which include client side rendering/processing (CSS, Javascript, JQuery).
<html><body><h1> This is the body</h1></body></html>
To simplify the uploading process and development work, we can use python with aws boto3 to auto upload different files and set configurations/permissions for each file. To use boto3 with python. simply pip install boto3. We would need to configure the AWS IAM role and also local PC to include the credentials as shown in link. An example of the python script is shown below. Use argument -ACL for permission setting and -ContentType to modify file type.
import smallutils as su import os, sys import boto3 TARGET_FNAME = r'directory/targetfile_to_update.html' TARGET_BUCKET = r'bucket_name' BUCKET_KNAME = 'filename_in_bucket.html' MODIFY_CONTENT_TYPE = 1 #changing the default content type. particular for html, need change to text/html. FOLDER_NAME = 'DATA/' #need a / at the end PUT_FILES = 1 #if 1-- put files, else treat as creating folder<span data-</span> if __name__ == "__main__": print "Print S3 resources" s3 = boto3.resource('s3') print "List of buckets: " for bucket in s3.buckets.all(): print bucket.name if PUT_FILES: print "Put files in bucket." data = open(TARGET_FNAME, 'rb') if MODIFY_CONTENT_TYPE: s3.Bucket(TARGET_BUCKET).put_object(Key=BUCKET_KNAME, Body=data, ACL='public-read', ContentType = 'text/html' ) #modify the content type else: s3.Bucket(TARGET_BUCKET).put_object(Key=BUCKET_KNAME, Body=data, ACL='public-read', ) #modify the content type else: # assumte to be create folder print "Create Folder" s3.Bucket(TARGET_BUCKET).put_object(Key=FOLDER_NAME, Body='') # ACL='public-read-write'??
We can also add in CSS and Jquery to render the index.html website.