Static website hosting

Last Updated: Oct 24, 2017

Python SDK can be used to set your buckets to the static website hosting mode. After the setting, OSS can be accessed as a static website, and the page can automatically jump to the index page and error page.

For more information, see 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: When the static website hosting mode is disabled, the NoSuchWebsite exception is thrown during get_bucket_website.

Disable static website hosting

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