Нет описания

BucketReferer.php 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. require_once __DIR__ . '/Common.php';
  3. use OSS\OssClient;
  4. use OSS\Core\OssException;
  5. use \OSS\Model\RefererConfig;
  6. $bucket = Common::getBucketName();
  7. $ossClient = Common::getOssClient();
  8. if (is_null($ossClient)) exit(1);
  9. //******************************* 简单使用 ****************************************************************
  10. //设置referer白名单
  11. $refererConfig = new RefererConfig();
  12. $refererConfig->setAllowEmptyReferer(true);
  13. $refererConfig->addReferer("www.aliiyun.com");
  14. $refererConfig->addReferer("www.aliiyuncs.com");
  15. $ossClient->putBucketReferer($bucket, $refererConfig);
  16. Common::println("bucket $bucket refererConfig created:" . $refererConfig->serializeToXml());
  17. //获取Referer白名单
  18. $refererConfig = $ossClient->getBucketReferer($bucket);
  19. Common::println("bucket $bucket refererConfig fetched:" . $refererConfig->serializeToXml());
  20. //删除referer白名单
  21. $refererConfig = new RefererConfig();
  22. $ossClient->putBucketReferer($bucket, $refererConfig);
  23. Common::println("bucket $bucket refererConfig deleted");
  24. //******************************* 完整用法参考下面函数 ****************************************************
  25. putBucketReferer($ossClient, $bucket);
  26. getBucketReferer($ossClient, $bucket);
  27. deleteBucketReferer($ossClient, $bucket);
  28. getBucketReferer($ossClient, $bucket);
  29. /**
  30. * 设置bucket的防盗链配置
  31. *
  32. * @param OssClient $ossClient OssClient实例
  33. * @param string $bucket 存储空间名称
  34. * @return null
  35. */
  36. function putBucketReferer($ossClient, $bucket)
  37. {
  38. $refererConfig = new RefererConfig();
  39. $refererConfig->setAllowEmptyReferer(true);
  40. $refererConfig->addReferer("www.aliiyun.com");
  41. $refererConfig->addReferer("www.aliiyuncs.com");
  42. try {
  43. $ossClient->putBucketReferer($bucket, $refererConfig);
  44. } catch (OssException $e) {
  45. printf(__FUNCTION__ . ": FAILED\n");
  46. printf($e->getMessage() . "\n");
  47. return;
  48. }
  49. print(__FUNCTION__ . ": OK" . "\n");
  50. }
  51. /**
  52. * 获取bucket的防盗链配置
  53. *
  54. * @param OssClient $ossClient OssClient实例
  55. * @param string $bucket 存储空间名称
  56. * @return null
  57. */
  58. function getBucketReferer($ossClient, $bucket)
  59. {
  60. $refererConfig = null;
  61. try {
  62. $refererConfig = $ossClient->getBucketReferer($bucket);
  63. } catch (OssException $e) {
  64. printf(__FUNCTION__ . ": FAILED\n");
  65. printf($e->getMessage() . "\n");
  66. return;
  67. }
  68. print(__FUNCTION__ . ": OK" . "\n");
  69. print($refererConfig->serializeToXml() . "\n");
  70. }
  71. /**
  72. * 删除bucket的防盗链配置
  73. * Referer白名单不能直接清空,只能通过重新设置来覆盖之前的规则。
  74. *
  75. * @param OssClient $ossClient OssClient实例
  76. * @param string $bucket 存储空间名称
  77. * @return null
  78. */
  79. function deleteBucketReferer($ossClient, $bucket)
  80. {
  81. $refererConfig = new RefererConfig();
  82. try {
  83. $ossClient->putBucketReferer($bucket, $refererConfig);
  84. } catch (OssException $e) {
  85. printf(__FUNCTION__ . ": FAILED\n");
  86. printf($e->getMessage() . "\n");
  87. return;
  88. }
  89. print(__FUNCTION__ . ": OK" . "\n");
  90. }