BucketCors.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. //******************************* Simple usage****************************************************************
  11. // Set cors configuration
  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. // Get cors configuration
  22. $corsConfig = $ossClient->getBucketCors($bucket);
  23. if ($corsConfig->getResponseVary()){
  24. printf("Response Vary : true" .PHP_EOL);
  25. }else{
  26. printf("Response Vary : false" .PHP_EOL);
  27. }
  28. foreach ($corsConfig->getRules() as $key => $rule){
  29. if($rule->getAllowedHeaders()){
  30. foreach($rule->getAllowedHeaders() as $header){
  31. printf("Allowed Headers :" .$header .PHP_EOL);
  32. }
  33. }
  34. if ($rule->getAllowedMethods()){
  35. foreach($rule->getAllowedMethods() as $method){
  36. printf("Allowed Methods :" .$method . PHP_EOL);
  37. }
  38. }
  39. if($rule->getAllowedOrigins()){
  40. foreach($rule->getAllowedOrigins() as $origin){
  41. printf("Allowed Origins :" .$origin , PHP_EOL);
  42. }
  43. }
  44. if($rule->getExposeHeaders()){
  45. foreach($rule->getExposeHeaders() as $exposeHeader){
  46. printf("Expose Headers :" .$exposeHeader . PHP_EOL);
  47. }
  48. }
  49. printf("Max Age Seconds :" .$rule->getMaxAgeSeconds() .PHP_EOL);
  50. }
  51. // Delete cors configuration
  52. $ossClient->deleteBucketCors($bucket);
  53. Common::println("bucket $bucket corsConfig deleted");
  54. //******************************* For complete usage, see the following functions *****************************************************
  55. putBucketCors($ossClient, $bucket);
  56. getBucketCors($ossClient, $bucket);
  57. deleteBucketCors($ossClient, $bucket);
  58. getBucketCors($ossClient, $bucket);
  59. /**
  60. * Set bucket cores
  61. *
  62. * @param OssClient $ossClient OssClient instance
  63. * @param string $bucket bucket name
  64. * @return null
  65. */
  66. function putBucketCors($ossClient, $bucket)
  67. {
  68. $corsConfig = new CorsConfig();
  69. $rule = new CorsRule();
  70. $rule->addAllowedHeader("x-oss-header");
  71. $rule->addAllowedOrigin("http://www.b.com");
  72. $rule->addAllowedMethod("POST");
  73. $rule->setMaxAgeSeconds(10);
  74. $corsConfig->addRule($rule);
  75. try {
  76. $ossClient->putBucketCors($bucket, $corsConfig);
  77. } catch (OssException $e) {
  78. printf(__FUNCTION__ . ": FAILED\n");
  79. printf($e->getMessage() . "\n");
  80. return;
  81. }
  82. print(__FUNCTION__ . ": OK" . "\n");
  83. }
  84. /**
  85. * Get and print the cors configuration of a bucket
  86. *
  87. * @param OssClient $ossClient OssClient instance
  88. * @param string $bucket bucket name
  89. * @return null
  90. */
  91. function getBucketCors($ossClient, $bucket)
  92. {
  93. $corsConfig = null;
  94. try {
  95. $corsConfig = $ossClient->getBucketCors($bucket);
  96. if ($corsConfig->getResponseVary()){
  97. printf("Response Vary : true" .PHP_EOL);
  98. }else{
  99. printf("Response Vary : false" .PHP_EOL);
  100. }
  101. foreach ($corsConfig->getRules() as $key => $rule){
  102. if($rule->getAllowedHeaders()){
  103. foreach($rule->getAllowedHeaders() as $header){
  104. printf("Allowed Headers :" .$header .PHP_EOL);
  105. }
  106. }
  107. if ($rule->getAllowedMethods()){
  108. foreach($rule->getAllowedMethods() as $method){
  109. printf("Allowed Methods :" .$method . PHP_EOL);
  110. }
  111. }
  112. if($rule->getAllowedOrigins()){
  113. foreach($rule->getAllowedOrigins() as $origin){
  114. printf("Allowed Origins :" .$origin , PHP_EOL);
  115. }
  116. }
  117. if($rule->getExposeHeaders()){
  118. foreach($rule->getExposeHeaders() as $exposeHeader){
  119. printf("Expose Headers :" .$exposeHeader . PHP_EOL);
  120. }
  121. }
  122. printf("Max Age Seconds :" .$rule->getMaxAgeSeconds() .PHP_EOL);
  123. }
  124. } catch (OssException $e) {
  125. printf(__FUNCTION__ . ": FAILED\n");
  126. printf($e->getMessage() . "\n");
  127. return;
  128. }
  129. print(__FUNCTION__ . ": OK" . "\n");
  130. }
  131. /**
  132. * Delete all cors configuraiton of a bucket
  133. *
  134. * @param OssClient $ossClient OssClient instance
  135. * @param string $bucket bucket name
  136. * @return null
  137. */
  138. function deleteBucketCors($ossClient, $bucket)
  139. {
  140. try {
  141. $ossClient->deleteBucketCors($bucket);
  142. } catch (OssException $e) {
  143. printf(__FUNCTION__ . ": FAILED\n");
  144. printf($e->getMessage() . "\n");
  145. return;
  146. }
  147. print(__FUNCTION__ . ": OK" . "\n");
  148. }