全部產品
Search
文件中心

Object Storage Service:靜態網站託管

更新時間:Aug 30, 2018

您可以將儲存空間配置成靜態網站託管模式。配置生效後,訪問網站相當於訪問儲存空間,並且能夠自動跳轉至指定的索引頁面和錯誤頁面。

更多關於靜態網站託管的介紹請參見開發指南中的配置靜態網站託管。以下場景的完整代碼請參見GitHub

設定靜態網站託管

以下代碼用於設定靜態網站託管:

  1. <?php
  2. if (is_file(__DIR__ . '/../autoload.php')) {
  3. require_once __DIR__ . '/../autoload.php';
  4. }
  5. if (is_file(__DIR__ . '/../vendor/autoload.php')) {
  6. require_once __DIR__ . '/../vendor/autoload.php';
  7. }
  8. use OSS\OssClient;
  9. use OSS\Core\OssException;
  10. use OSS\Model\WebsiteConfig;
  11. // 阿里雲主帳號AccessKey擁有所有API的存取權限,風險很高。強烈建議您建立並使用RAM帳號進行API訪問或日常運維,請登入 https://ram.console.aliyun.com 建立RAM帳號。
  12. $accessKeyId = "<yourAccessKeyId>";
  13. $accessKeySecret = "<yourAccessKeySecret>";
  14. // Endpoint以杭州為例,其它Region請按實際情況填寫。
  15. $endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
  16. $bucket= "<yourBucketName>";
  17. $websiteConfig = new WebsiteConfig("index.html", "error.html");
  18. try {
  19. $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
  20. $ossClient->putBucketWebsite($bucket, $websiteConfig);
  21. } catch (OssException $e) {
  22. printf(__FUNCTION__ . ": FAILED\n");
  23. printf($e->getMessage() . "\n");
  24. return;
  25. }
  26. print(__FUNCTION__ . ": OK" . "\n");

查看靜態網站託管配置

以下代碼用於查看靜態網站託管配置:

  1. <?php
  2. if (is_file(__DIR__ . '/../autoload.php')) {
  3. require_once __DIR__ . '/../autoload.php';
  4. }
  5. if (is_file(__DIR__ . '/../vendor/autoload.php')) {
  6. require_once __DIR__ . '/../vendor/autoload.php';
  7. }
  8. use OSS\OssClient;
  9. use OSS\Core\OssException;
  10. // 阿里雲主帳號AccessKey擁有所有API的存取權限,風險很高。強烈建議您建立並使用RAM帳號進行API訪問或日常運維,請登入 https://ram.console.aliyun.com 建立RAM帳號。
  11. $accessKeyId = "<yourAccessKeyId>";
  12. $accessKeySecret = "<yourAccessKeySecret>";
  13. // Endpoint以杭州為例,其它Region請按實際情況填寫。
  14. $endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
  15. $bucket= "<yourBucketName>";
  16. $websiteConfig = null;
  17. try{
  18. $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
  19. $websiteConfig = $ossClient->getBucketWebsite($bucket);
  20. } catch(OssException $e) {
  21. printf(__FUNCTION__ . ": FAILED\n");
  22. printf($e->getMessage() . "\n");
  23. return;
  24. }
  25. print(__FUNCTION__ . ": OK" . "\n");
  26. print($websiteConfig->serializeToXml() . "\n");

刪除靜態網站託管配置

以下代碼用於刪除靜態網站託管配置:

  1. <?php
  2. if (is_file(__DIR__ . '/../autoload.php')) {
  3. require_once __DIR__ . '/../autoload.php';
  4. }
  5. if (is_file(__DIR__ . '/../vendor/autoload.php')) {
  6. require_once __DIR__ . '/../vendor/autoload.php';
  7. }
  8. use OSS\OssClient;
  9. use OSS\Core\OssException;
  10. // 阿里雲主帳號AccessKey擁有所有API的存取權限,風險很高。強烈建議您建立並使用RAM帳號進行API訪問或日常運維,請登入 https://ram.console.aliyun.com 建立RAM帳號。
  11. $accessKeyId = "<yourAccessKeyId>";
  12. $accessKeySecret = "<yourAccessKeySecret>";
  13. // Endpoint以杭州為例,其它Region請按實際情況填寫。
  14. $endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
  15. $bucket= "<yourBucketName>";
  16. try{
  17. $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
  18. $ossClient->deleteBucketWebsite($bucket);
  19. } catch(OssException $e) {
  20. printf(__FUNCTION__ . ": FAILED\n");
  21. printf($e->getMessage() . "\n");
  22. return;
  23. }
  24. print(__FUNCTION__ . ": OK" . "\n");