Static Website Hosting
API Description
You can use this API to upload the files of the static website to your bucket in OBS as objects and configure the public-read permission on the files, and then configure the static website hosting mode for your bucket to host your static websites in OBS. After this, when third-party users access your websites, they actually access the objects in your bucket in OBS. When using static website hosting, you can configure request redirection to redirect specific or all requests.
For more information, see Static Website Hosting.
Website File Hosting
- Upload a website file to your bucket in OBS as an object and set the MIME type for the object.
- Set the ACL of the object to public-read.
- Access the object using a browser.
Sample code is as follows:
# Import the module.
from obs import ObsClient
# Create an instance of ObsClient.
obsClient = ObsClient(
access_key_id='*** Provide your Access Key ***',
secret_access_key='*** Provide your Secret Key ***',
server='https://your-endpoint'
)
from obs import PutObjectHeader
from obs import HeadPermission
headers = PutObjectHeader()
# Set the MIME type for the object.
headers.contentType = 'text/html'
# Upload an object.
resp = obsClient.putFile('bucketname', 'test.html', 'localfile.html', headers=headers)
if resp.status < 300:
print('requestId:', resp.requestId)
# Set the object ACL to public-read.
resp2 = obsClient.setObjectAcl('bucketname', 'test.html', aclControl=HeadPermission.PUBLIC_READ)
if resp2.status < 300:
print('requestId:', resp2.requestId)
else:
print('errorCode:', resp2.errorCode)
print('errorMessage:', resp2.errorMessage)
else:
print('errorCode:', resp.errorCode)
print('errorMessage:', resp.errorMessage)
You can use https://bucketname.your-endpoint/test.html in a browser to access files hosted using the sample code.
Configuring Website Hosting
Call ObsClient.setBucketWebsite to set website hosting on a bucket.
Configure the default homepage and error pages.
# Import the module.
from obs import ObsClient
# Create an instance of ObsClient.
obsClient = ObsClient(
access_key_id='*** Provide your Access Key ***',
secret_access_key='*** Provide your Secret Key ***',
server='https://your-endpoint'
)
from obs import WebsiteConfiguration
from obs import IndexDocument
from obs import ErrorDocument
from obs import RoutingRule
from obs import Condition
from obs import Redirect
# Configure the error page.
errorDocument = ErrorDocument(key='error.html')
# Configure the default homepage.
indexDocument = IndexDocument(suffix='index.html')
resp = obsClient.setBucketWebsite('bucketname',
WebsiteConfiguration(errorDocument=errorDocument, indexDocument=indexDocument))
if resp.status < 300:
print('requestId:', resp.requestId)
else:
print('errorCode:', resp.errorCode)
print('errorMessage:', resp.errorMessage)
Configure redirection rules.
# Import the module.
from obs import ObsClient
# Create an instance of ObsClient.
obsClient = ObsClient(
access_key_id='*** Provide your Access Key ***',
secret_access_key='*** Provide your Secret Key ***',
server='https://your-endpoint'
)
from obs import WebsiteConfiguration
from obs import IndexDocument
from obs import ErrorDocument
from obs import RoutingRule
from obs import Condition
from obs import Redirect
# Configure the error page.
errorDocument = ErrorDocument(key='error.html')
# Configure the default homepage.
indexDocument = IndexDocument(suffix='index.html')
routingRule = RoutingRule(condition=Condition(keyPrefixEquals='keyprefix'),
redirect=Redirect(protocol='http', replaceKeyPrefixWith='replacekeyprefix', httpRedirectCode=305, hostName='www.example.com'))
resp = obsClient.setBucketWebsite('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)
Configure redirection for all requests.
# Import the module.
from obs import ObsClient
# Create an instance of ObsClient.
obsClient = ObsClient(
access_key_id='*** Provide your Access Key ***',
secret_access_key='*** Provide your Secret Key ***',
server='https://your-endpoint'
)
from obs import WebsiteConfiguration
from obs import RedirectAllRequestTo
resp = obsClient.setBucketWebsite('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)
Viewing Website Hosting
You can call ObsClient.getBucketWebsite to view the hosting settings of a bucket. Sample code is as follows:
# Import the module.
from obs import ObsClient
# Create an instance of ObsClient.
obsClient = ObsClient(
access_key_id='*** Provide your Access Key ***',
secret_access_key='*** Provide your Secret Key ***',
server='https://your-endpoint'
)
resp = obsClient.getBucketWebsite('bucketname')
if resp.status < 300:
print('requestId:', resp.requestId)
if resp.body.redirectAllRequestTo:
print('redirectAllRequestTo.hostName:', resp.body.redirectAllRequestTo.hostName, ',redirectAllRequestTo.protocol:', resp.body.redirectAllRequestTo.protocol)
if resp.body.indexDocument:
print('indexDocument.suffix:', resp.body.indexDocument.suffix)
if resp.body.errorDocument:
print('errorDocument.key:', resp.body.errorDocument.key)
if resp.body.routingRules:
index = 1
for rout in resp.body.routingRules:
print('routingRule[', index, ']:')
index += 1
print('condition.keyPrefixEquals:', rout.condition.keyPrefixEquals, ',condition.httpErrorCodeReturnedEquals:', rout.condition.httpErrorCodeReturnedEquals)
print('redirect.protocol:', rout.redirect.protocol, ',redirect.hostName:', rout.redirect.hostName, ',redirect.replaceKeyPrefixWith:', rout.redirect.replaceKeyPrefixWith, ',redirect.replaceKeyWith:', rout.redirect.replaceKeyWith, ',redirect.httpRedirectCode:', rout.redirect.httpRedirectCode)
else:
print('errorCode:', resp.errorCode)
print('errorMessage:', resp.errorMessage)
Deleting Website Hosting Settings
You can call ObsClient.deleteBucketWebsite to delete the hosting settings of a bucket. Sample code is as follows:
# Import the module.
from obs import ObsClient
# Create an instance of ObsClient.
obsClient = ObsClient(
access_key_id='*** Provide your Access Key ***',
secret_access_key='*** Provide your Secret Key ***',
server='https://your-endpoint'
)
resp = obsClient.deleteBucketWebsite('bucketname')
if resp.status < 300:
print('requestId:', resp.requestId)
else:
print('errorCode:', resp.errorCode)
print('errorMessage:', resp.errorMessage)
Last Article: Server-Side Encryption
Next Article: Data Types
Did this article solve your problem?
Thank you for your score!Your feedback would help us improve the website.