BucketCors.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. require_once __DIR__ . '/Common.php';
  3. use OSS\OssClient;
  4. use OSS\Core\OssException;
  5. use OSS\Model\CorsConfig;
  6. use OSS\Model\CorsRule;
  7. $ossClient = Common::getOssClient();
  8. if (is_null($ossClient)) exit(1);
  9. $bucket = Common::getBucketName();
  10. //******************************* 简单使用 ****************************************************************
  11. // 设置cors配置
  12. $corsConfig = new CorsConfig();
  13. $rule = new CorsRule();
  14. $rule->addAllowedHeader("x-oss-header");
  15. $rule->addAllowedOrigin("http://www.b.com");
  16. $rule->addAllowedMethod("POST");
  17. $rule->setMaxAgeSeconds(10);
  18. $corsConfig->addRule($rule);
  19. $ossClient->putBucketCors($bucket, $corsConfig);
  20. Common::println("bucket $bucket corsConfig created:" . $corsConfig->serializeToXml());
  21. // 获取cors配置
  22. $corsConfig = $ossClient->getBucketCors($bucket);
  23. Common::println("bucket $bucket corsConfig fetched:" . $corsConfig->serializeToXml());
  24. // 删除cors配置
  25. $ossClient->deleteBucketCors($bucket);
  26. Common::println("bucket $bucket corsConfig deleted");
  27. //******************************* 完整用法参考下面函数 *****************************************************
  28. putBucketCors($ossClient, $bucket);
  29. getBucketCors($ossClient, $bucket);
  30. deleteBucketCors($ossClient, $bucket);
  31. getBucketCors($ossClient, $bucket);
  32. /**
  33. * 设置bucket的cors配置
  34. *
  35. * @param OssClient $ossClient OssClient实例
  36. * @param string $bucket 存储空间名称
  37. * @return null
  38. */
  39. function putBucketCors($ossClient, $bucket)
  40. {
  41. $corsConfig = new CorsConfig();
  42. $rule = new CorsRule();
  43. $rule->addAllowedHeader("x-oss-header");
  44. $rule->addAllowedOrigin("http://www.b.com");
  45. $rule->addAllowedMethod("POST");
  46. $rule->setMaxAgeSeconds(10);
  47. $corsConfig->addRule($rule);
  48. try {
  49. $ossClient->putBucketCors($bucket, $corsConfig);
  50. } catch (OssException $e) {
  51. printf(__FUNCTION__ . ": FAILED\n");
  52. printf($e->getMessage() . "\n");
  53. return;
  54. }
  55. print(__FUNCTION__ . ": OK" . "\n");
  56. }
  57. /**
  58. * 获取并打印bucket的cors配置
  59. *
  60. * @param OssClient $ossClient OssClient实例
  61. * @param string $bucket 存储空间名称
  62. * @return null
  63. */
  64. function getBucketCors($ossClient, $bucket)
  65. {
  66. $corsConfig = null;
  67. try {
  68. $corsConfig = $ossClient->getBucketCors($bucket);
  69. } catch (OssException $e) {
  70. printf(__FUNCTION__ . ": FAILED\n");
  71. printf($e->getMessage() . "\n");
  72. return;
  73. }
  74. print(__FUNCTION__ . ": OK" . "\n");
  75. print($corsConfig->serializeToXml() . "\n");
  76. }
  77. /**
  78. * 删除bucket的所有的cors配置
  79. *
  80. * @param OssClient $ossClient OssClient实例
  81. * @param string $bucket 存储空间名称
  82. * @return null
  83. */
  84. function deleteBucketCors($ossClient, $bucket)
  85. {
  86. try {
  87. $ossClient->deleteBucketCors($bucket);
  88. } catch (OssException $e) {
  89. printf(__FUNCTION__ . ": FAILED\n");
  90. printf($e->getMessage() . "\n");
  91. return;
  92. }
  93. print(__FUNCTION__ . ": OK" . "\n");
  94. }