Static website hosting

Last Updated: Jun 01, 2017

You can use the Python SDK to set your buckets to the static website hosting mode. After the setting, the OSS can be accessed as a static website, and the page can automatically jump to the index page and error page.For more information, refer to Static Website Hosting.

Configure static website hosting

The following code enables the static website hosting mode, and sets the index page to index.html and the error page (the 404 page) to error.html:

  1. # -*- coding: utf-8 -*-
  2. import oss2
  3. from oss2.models import BucketWebsite
  4. auth = oss2.Auth ('Your AccessKeyID', 'Your AccessKeySecret')
  5. bucket = oss2.Bucket (auth, 'Your endpoint', 'your bucket name')
  6. bucket.put_bucket_website(BucketWebsite('index.html', 'error.html'))

Get the static website hosting configuration

  1. try:
  2. website = bucket.get_bucket_website()
  3. print('Index file is {0}, error file is {1}'.format(website.index_file, website.error_file))
  4. except oss2.exceptions.NoSuchWebsite as e:
  5. print('Website is not configured, request_id={0}'.format(e.request_id))

Note that when the static website hosting mode is disabled, the NoSuchWebsite exception will be thrown during get_bucket_website.

Disable static website hosting

  1. bucket.delete_bucket_website()
Thank you! We've received your feedback.