Static website hosting

  • Configure static website hosting

    The following code provides an example on how to configure static website hosting:

    # -*- coding: utf-8 -*-
    import oss2
    from oss2.models import BucketWebsite
    
    # Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console. 
    auth = oss2.Auth('yourAccessKeyId', 'yourAccessKeySecret')
    # In this example, the endpoint of the China (Hangzhou) region is used. Specify the endpoint based on your business requirements. For more information about the endpoints of other regions, see Regions and endpoints. 
    # Specify the name of the bucket. Example: examplebucket. For more information about the naming conventions for buckets, see Bucket naming conventions. 
    bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhou.aliyuncs.com', 'examplebucket')
    
    # Enable static website hosting and set the default homepage to index.html and the default 404 page to error.html. 
    bucket.put_bucket_website(BucketWebsite('index.html', 'error.html'))            
  • Query static website hosting configurations

    The following code provides an example on how to query static website hosting configurations:

    # -*- coding: utf-8 -*-
    import oss2
    
    # Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console. 
    auth = oss2.Auth('yourAccessKeyId', 'yourAccessKeySecret')
    # In this example, the endpoint of the China (Hangzhou) region is used. Specify the endpoint based on your business requirements. 
    # Specify the name of the bucket. Example: examplebucket. 
    bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhou.aliyuncs.com', 'examplebucket')
    
    try:
        # If static website hosting is not enabled for the specified bucket, a NoSuchWebsite exception is thrown when you call get_bucket_website. 
        website = bucket.get_bucket_website()
        print('Index file is {0}, error file is {1}'.format(website.index_file, website.error_file))
    except oss2.exceptions.NoSuchWebsite as e:
        print('Website is not configured, request_id={0}'.format(e.request_id))           
  • Delete static website hosting configurations

    The following code provides an example on how to delete static website hosting configurations:

    # -*- coding: utf-8 -*-
    import oss2
    # Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console. 
    auth = oss2.Auth('yourAccessKeyId', 'yourAccessKeySecret')
    # In this example, the endpoint of the China (Hangzhou) region is used. Specify the endpoint based on your business requirements. 
    # Specify the name of the bucket. Example: examplebucket. 
    bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhou.aliyuncs.com', 'examplebucket')
    
    bucket.delete_bucket_website()           

Mirroring-based back-to-origin

  • Configure mirroring-based back-to-origin

    The following code provides an example on how to configure mirroring-based back-to-origin rules in the preceding scenario:

    # -*- coding: utf-8 -*-
    import oss2
    from oss2.models import BucketWebsite
    
    # Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console. 
    auth = oss2.Auth('yourAccessKeyId', 'yourAccessKeySecret')
    # In this example, the endpoint of the China (Hangzhou) region is used. Specify the endpoint based on your business requirements. 
    # Specify the name of the bucket. Example: examplebucket. For more information about the naming conventions for buckets, see Bucket naming conventions. 
    bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhou.aliyuncs.com', 'examplebucket')
    
    # Enable static website hosting and set the default homepage to index.html and the default 404 page to error.html. 
    index_file = 'index.html'
    error_file = 'error.html'
    # Specify the matching conditions. 
    condition1 = Condition(key_prefix_equals='examplefolder',
                           http_err_code_return_equals=404)
    
    # Specify the headers included in the request when you use mirroring-based back-to-origin. 
    mirror_headers_set_1 = MirrorHeadersSet("myheader-key5","myheader-value5")
    mirror_headers_set_2 = MirrorHeadersSet("myheader-key6","myheader-value6")
    set_list = [mirror_headers_set_1, mirror_headers_set_2]
    pass_list = ['myheader-key1', 'myheader-key2']
    remove_list = ['myheader-key3', 'myheader-key4']
    mirror_header = RedirectMirrorHeaders(pass_all=True, pass_list=pass_list, remove_list=remove_list, set_list=set_list)
    # Specify the operation to perform after the rule is matched. 
    
    redirect1 = Redirect(redirect_type=REDIRECT_TYPE_MIRROR, mirror_url='https://www.example.com/',
                         mirror_pass_query_string=True, mirror_follow_redirect=True, mirror_check_md5=True, mirror_headers=mirror_header)
    
    rule1 = RoutingRule(rule_num=1, condition=condition1, redirect=redirect1)
    
    # Configure mirroring-based back-to-origin. 
    website_set = BucketWebsite(index_file, error_file, [rule1])
  • Query mirroring-based back-to-origin configurations

    The following code provides an example on how to query the mirroring-based back-to-origin configurations:

    # -*- coding: utf-8 -*-
    import oss2
    # Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console. 
    auth = oss2.Auth('yourAccessKeyId', 'yourAccessKeySecret')
    # In this example, the endpoint of the China (Hangzhou) region is used. Specify the endpoint based on your business requirements. 
    # Specify the name of the bucket. Example: examplebucket. 
    bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhou.aliyuncs.com', 'examplebucket')
    
    try:
        # If static website hosting is not enabled for the specified bucket, a NoSuchWebsite exception is thrown when you call get_bucket_website. 
        website_get = bucket.get_bucket_website()
        # Query the default homepage. 
        print(website_get.index_file)
        # Query the default 404 page. 
        print(website_get.error_file)
        for rule in website_get.rules:
            print(rule.rule_num)
            # Query the prefix for rule matching. 
            print(rule.condition.key_prefix_equals)
            # Query the HTTP status code. 
            print(rule.condition.http_err_code_return_equals)
            # Query the redirect type. 
            print(rule.redirect.redirect_type)
            # Query the origin URL for mirroring-based back-to-origin. 
            print(rule.redirect.mirror_url)
            # Query the parameters in the request. 
            print(rule.redirect.pass_query_string)
            # Specify the string that is used to replace the object name when the request is redirected. This parameter can be set to a variable. 
            # print(rule.redirect.replace_key_with)
            # Specify the string that is used to replace the prefix of the object name when the request is redirected. If the prefix of an object is empty, this string precedes the object name. 
            # print(rule.redirect.replace_key_prefix_with)
            # Query the protocol that is used for redirection. 
            # print(rule.redirect.proto)
            # Query the domain name to which the request is redirected. 
            # print(rule.redirect.host_name)
            # Query the HTTP status code in the response. 
            # print(rule.redirect.http_redirect_code)
    except oss2.exceptions.NoSuchWebsite as e:
        print('Website is not configured, request_id={0}'.format(e.request_id))
  • Delete mirroring-based back-to-origin configurations

    The following code provides an example on how to delete mirroring-based back-to-origin configurations:

    # -*- coding: utf-8 -*-
    import oss2
    # Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console. 
    auth = oss2.Auth('yourAccessKeyId', 'yourAccessKeySecret')
    # In this example, the endpoint of the China (Hangzhou) region is used. Specify the endpoint based on your business requirements. 
    # Specify the name of the bucket. Example: examplebucket. 
    bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhou.aliyuncs.com', 'examplebucket')
    
    bucket.delete_bucket_website()           

References

  • For more information about the API operation that you can call to configure static website hosting or mirroring-based back-to-origin, see PutBucketWebsite.
  • For more information about the API operation that you can call to query static website hosting or mirroring-based back-to-origin configurations, see GetBucketWebsite.
  • For more information about the API operation that you can call to delete static website hosting or mirroring-based back-to-origin configurations, see DeleteBucketWebsite.