BucketWebsite.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. require_once __DIR__ . '/Common.php';
  3. use OSS\OssClient;
  4. use OSS\Core\OssException;
  5. use OSS\Model\WebsiteConfig;
  6. $bucket = Common::getBucketName();
  7. $ossClient = Common::getOssClient();
  8. if (is_null($ossClient)) exit(1);
  9. //*******************************简单使用***************************************************************
  10. // 设置Bucket的静态网站托管模式
  11. $websiteConfig = new WebsiteConfig("index.html", "error.html");
  12. $ossClient->putBucketWebsite($bucket, $websiteConfig);
  13. Common::println("bucket $bucket websiteConfig created:" . $websiteConfig->serializeToXml());
  14. // 查看Bucket的静态网站托管状态
  15. $websiteConfig = $ossClient->getBucketWebsite($bucket);
  16. Common::println("bucket $bucket websiteConfig fetched:" . $websiteConfig->serializeToXml());
  17. // 删除Bucket的静态网站托管模式
  18. $ossClient->deleteBucketWebsite($bucket);
  19. Common::println("bucket $bucket websiteConfig deleted");
  20. //******************************* 完整用法参考下面函数 ****************************************************
  21. putBucketWebsite($ossClient, $bucket);
  22. getBucketWebsite($ossClient, $bucket);
  23. deleteBucketWebsite($ossClient, $bucket);
  24. getBucketWebsite($ossClient, $bucket);
  25. /**
  26. * 设置bucket的静态网站托管模式配置
  27. *
  28. * @param $ossClient OssClient
  29. * @param $bucket string 存储空间名称
  30. * @return null
  31. */
  32. function putBucketWebsite($ossClient, $bucket)
  33. {
  34. $websiteConfig = new WebsiteConfig("index.html", "error.html");
  35. try {
  36. $ossClient->putBucketWebsite($bucket, $websiteConfig);
  37. } catch (OssException $e) {
  38. printf(__FUNCTION__ . ": FAILED\n");
  39. printf($e->getMessage() . "\n");
  40. return;
  41. }
  42. print(__FUNCTION__ . ": OK" . "\n");
  43. }
  44. /**
  45. * 获取bucket的静态网站托管状态
  46. *
  47. * @param OssClient $ossClient OssClient实例
  48. * @param string $bucket 存储空间名称
  49. * @return null
  50. */
  51. function getBucketWebsite($ossClient, $bucket)
  52. {
  53. $websiteConfig = null;
  54. try {
  55. $websiteConfig = $ossClient->getBucketWebsite($bucket);
  56. } catch (OssException $e) {
  57. printf(__FUNCTION__ . ": FAILED\n");
  58. printf($e->getMessage() . "\n");
  59. return;
  60. }
  61. print(__FUNCTION__ . ": OK" . "\n");
  62. print($websiteConfig->serializeToXml() . "\n");
  63. }
  64. /**
  65. * 删除bucket的静态网站托管模式配置
  66. *
  67. * @param OssClient $ossClient OssClient实例
  68. * @param string $bucket 存储空间名称
  69. * @return null
  70. */
  71. function deleteBucketWebsite($ossClient, $bucket)
  72. {
  73. try {
  74. $ossClient->deleteBucketWebsite($bucket);
  75. } catch (OssException $e) {
  76. printf(__FUNCTION__ . ": FAILED\n");
  77. printf($e->getMessage() . "\n");
  78. return;
  79. }
  80. print(__FUNCTION__ . ": OK" . "\n");
  81. }