edit-icon download-icon

Static website hosting

Last Updated: Nov 01, 2017

We mentioned in Bind custom domain names (CNAME) that OSS allows you to direct your domain name to the OSS service address. In this way, the OSS bucket is accessed when you access your website. You must specify the names of the objects in the bucket corresponding to the homepage (index) and error page (error) of the website respectively.

For more information about static website hosting, see Static Website Hosting.

Note: For complete code, see GitHub.

Configure static website hosting

You can set static website hosting through OssClient::putBucketWebsite:

  1. <?php
  2. /**
  3. * Configure static website hosting mode for a bucket
  4. *
  5. * @param $ossClient OssClient
  6. * @param $bucket string Bucket name
  7. * @return null
  8. */
  9. function putBucketWebsite($ossClient, $bucket)
  10. {
  11. $websiteConfig = new WebsiteConfig("index.html", "error.html");
  12. try {
  13. $ossClient->putBucketWebsite($bucket, $websiteConfig);
  14. } catch (OssException $e) {
  15. printf(__FUNCTION__ . ": FAILED\n");
  16. printf($e->getMessage() . "\n");
  17. return;
  18. }
  19. print(__FUNCTION__ . ": OK" . "\n");
  20. }

Get the static website hosting configuration

You can get static website hosting configuration through OssClient::getBucketWebsite:

  1. <?php
  2. /**
  3. * Get the static website hosting status for a bucket
  4. *
  5. * @param OssClient $ossClient OSSClient instance
  6. * @param string $bucket Bucket name
  7. * @return null
  8. */
  9. function getBucketWebsite($ossClient, $bucket) {
  10. $websiteConfig = null;
  11. try{
  12. $websiteConfig = $ossClient->getBucketWebsite($bucket);
  13. } catch(OssException $e) {
  14. printf(__FUNCTION__ . ": FAILED\n");
  15. printf($e->getMessage() . "\n");
  16. return;
  17. }
  18. print(__FUNCTION__ . ": OK" . "\n");
  19. print($websiteConfig->serializeToXml() . "\n");
  20. }

Delete the static website hosting configuration

You can delete the static website hosting configuration through OssClient::deleteBucketWebsite:

  1. <?php
  2. /**
  3. * Delete the static website hosting configuration for a bucket
  4. *
  5. * @param OssClient $ossClient OSSClient instance
  6. * @param string $bucket Bucket name
  7. * @return null
  8. */
  9. function deleteBucketWebsite($ossClient, $bucket) {
  10. try{
  11. $ossClient->deleteBucketWebsite($bucket);
  12. } catch(OssException $e) {
  13. printf(__FUNCTION__ . ": FAILED\n");
  14. printf($e->getMessage() . "\n");
  15. return;
  16. }
  17. print(__FUNCTION__ . ": OK" . "\n");
  18. }
Thank you! We've received your feedback.