• Object Storage Service

obs
  1. Help Center
  2. Object Storage Service
  3. Developer Guide (Python SDK)
  4. Static Website Hosting
  5. Setting Website Hosting

Setting Website Hosting

You can call ObsClient.setBucketWebsiteConfiguration to set website hosting for a bucket.

Configuring the Default Homepage and Error Pages

Sample code:

# Import the module.
from com.obs.client.obs_client import ObsClient
 
# Create an instance of ObsClient.
obsClient = ObsClient(
    access_key_id='*** Provide your Access Key ***',    
    secret_access_key='*** Provide your Secret Key ***',    
    server='yourdomainname'
)

from com.obs.models.website_configuration import WebsiteConfiguration
from com.obs.models.index_document import IndexDocument
from com.obs.models.error_document import ErrorDocument
from com.obs.models.routing_rule import RoutingRule
from com.obs.models.condition import Condition
from com.obs.models.redirect import Redirect

# Configure the error pages.
errorDocument = ErrorDocument(key='error.html')
# Configure the default homepage.
indexDocument = IndexDocument(suffix='index.html')
resp = obsClient.setBucketWebsiteConfiguration('bucketname', 
                WebsiteConfiguration(errorDocument=errorDocument, indexDocument=indexDocument)) 

if resp.status < 300:    
    print('requestId:', resp.requestId)
else:    
    print('errorCode:', resp.errorCode)    
    print('errorMessage:', resp.errorMessage)

Configuring Redirection Rules

Sample code:

# Import the module.
from com.obs.client.obs_client import ObsClient
 
# Create an instance of ObsClient.
obsClient = ObsClient(
    access_key_id='*** Provide your Access Key ***',    
    secret_access_key='*** Provide your Secret Key ***',    
    server='yourdomainname'
)

from com.obs.models.website_configuration import WebsiteConfiguration
from com.obs.models.index_document import IndexDocument
from com.obs.models.error_document import ErrorDocument
from com.obs.models.routing_rule import RoutingRule
from com.obs.models.condition import Condition
from com.obs.models.redirect import Redirect

#  Configure the error pages.
errorDocument = ErrorDocument(key='error.html')
# Configure the default homepage.
indexDocument = IndexDocument(suffix='index.html')
routingRule = RoutingRule(condition=Condition(httpErrorCodeReturnedEquals=404, keyPrefixEquals='keyprefix'), 
                           redirect=Redirect(protocol='http', replaceKeyPrefixWith='replacekeyprefix', httpRedirectCode=305, hostName='www.example.com'))
resp = obsClient.setBucketWebsiteConfiguration('bucketname', 
                WebsiteConfiguration(errorDocument=errorDocument, indexDocument=indexDocument, routingRules=[routingRule])) 

if resp.status < 300:    
    print('requestId:', resp.requestId)
else:    
    print('errorCode:', resp.errorCode)    
    print('errorMessage:', resp.errorMessage)

Configuring Redirection for All Requests

Sample code:

# Import the module.
from com.obs.client.obs_client import ObsClient
 
# Create an instance of ObsClient.
obsClient = ObsClient(
    access_key_id='*** Provide your Access Key ***',    
    secret_access_key='*** Provide your Secret Key ***',    
    server='yourdomainname'
)

from com.obs.models.website_configuration import WebsiteConfiguration
from com.obs.models.redirect_all_request_to import RedirectAllRequestTo

resp = obsClient.setBucketWebsiteConfiguration('bucketname', 
                WebsiteConfiguration(redirectAllRequestTo=RedirectAllRequestTo(hostName='www.example.com', protocol='http'))) 

if resp.status < 300:    
    print('requestId:', resp.requestId)
else:    
    print('errorCode:', resp.errorCode)    
    print('errorMessage:', resp.errorMessage)