Bucket.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. require_once __DIR__ . '/Common.php';
  3. use OSS\OssClient;
  4. use OSS\Core\OssException;
  5. $ossClient = Common::getOssClient();
  6. if (is_null($ossClient)) exit(1);
  7. $bucket = Common::getBucketName();
  8. //******************************* Simple Usage****************************************************************
  9. // Create a bucket
  10. $ossClient->createBucket($bucket, OssClient::OSS_ACL_TYPE_PUBLIC_READ_WRITE);
  11. Common::println("bucket $bucket created");
  12. // Check whether a bucket exists
  13. $doesExist = $ossClient->doesBucketExist($bucket);
  14. Common::println("bucket $bucket exist? " . ($doesExist ? "yes" : "no"));
  15. // Get the bucket list
  16. $bucketListInfo = $ossClient->listBuckets();
  17. // Set bucket ACL
  18. $ossClient->putBucketAcl($bucket, OssClient::OSS_ACL_TYPE_PUBLIC_READ_WRITE);
  19. Common::println("bucket $bucket acl put");
  20. // Get bucket ACL
  21. $acl = $ossClient->getBucketAcl($bucket);
  22. Common::println("bucket $bucket acl get: " . $acl);
  23. //******************************* For complete usage, see the following functions ****************************************************
  24. createBucket($ossClient, $bucket);
  25. doesBucketExist($ossClient, $bucket);
  26. deleteBucket($ossClient, $bucket);
  27. putBucketAcl($ossClient, $bucket);
  28. getBucketAcl($ossClient, $bucket);
  29. listBuckets($ossClient);
  30. /**
  31. * Create a new bucket
  32. * acl indicates the access permission of a bucket, including: private, public-read-only/private-read-write, and public read-write.
  33. * Private indicates that only the bucket owner or authorized users can access the data..
  34. * The three permissions are separately defined by (OssClient::OSS_ACL_TYPE_PRIVATE,OssClient::OSS_ACL_TYPE_PUBLIC_READ, OssClient::OSS_ACL_TYPE_PUBLIC_READ_WRITE)
  35. *
  36. * @param OssClient $ossClient OssClient instance
  37. * @param string $bucket Name of the bucket to create
  38. * @return null
  39. */
  40. function createBucket($ossClient, $bucket)
  41. {
  42. try {
  43. $ossClient->createBucket($bucket, OssClient::OSS_ACL_TYPE_PUBLIC_READ_WRITE);
  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. * Check whether a bucket exists.
  53. *
  54. * @param OssClient $ossClient OssClient instance
  55. * @param string $bucket bucket name
  56. */
  57. function doesBucketExist($ossClient, $bucket)
  58. {
  59. try {
  60. $res = $ossClient->doesBucketExist($bucket);
  61. } catch (OssException $e) {
  62. printf(__FUNCTION__ . ": FAILED\n");
  63. printf($e->getMessage() . "\n");
  64. return;
  65. }
  66. if ($res === true) {
  67. print(__FUNCTION__ . ": OK" . "\n");
  68. } else {
  69. print(__FUNCTION__ . ": FAILED" . "\n");
  70. }
  71. }
  72. /**
  73. * Delete a bucket. If the bucket is not empty, the deletion fails.
  74. * A bucket which is not empty indicates that it does not contain any objects or parts that are not completely uploaded during multipart upload
  75. *
  76. * @param OssClient $ossClient OssClient instance
  77. * @param string $bucket Name of the bucket to delete
  78. * @return null
  79. */
  80. function deleteBucket($ossClient, $bucket)
  81. {
  82. try {
  83. $ossClient->deleteBucket($bucket);
  84. } catch (OssException $e) {
  85. printf(__FUNCTION__ . ": FAILED\n");
  86. printf($e->getMessage() . "\n");
  87. return;
  88. }
  89. print(__FUNCTION__ . ": OK" . "\n");
  90. }
  91. /**
  92. * Set bucket ACL
  93. *
  94. * @param OssClient $ossClient OssClient instance
  95. * @param string $bucket bucket name
  96. * @return null
  97. */
  98. function putBucketAcl($ossClient, $bucket)
  99. {
  100. $acl = OssClient::OSS_ACL_TYPE_PRIVATE;
  101. try {
  102. $ossClient->putBucketAcl($bucket, $acl);
  103. } catch (OssException $e) {
  104. printf(__FUNCTION__ . ": FAILED\n");
  105. printf($e->getMessage() . "\n");
  106. return;
  107. }
  108. print(__FUNCTION__ . ": OK" . "\n");
  109. }
  110. /**
  111. * Get bucket ACL
  112. *
  113. * @param OssClient $ossClient OssClient instance
  114. * @param string $bucket bucket name
  115. * @return null
  116. */
  117. function getBucketAcl($ossClient, $bucket)
  118. {
  119. try {
  120. $res = $ossClient->getBucketAcl($bucket);
  121. } catch (OssException $e) {
  122. printf(__FUNCTION__ . ": FAILED\n");
  123. printf($e->getMessage() . "\n");
  124. return;
  125. }
  126. print(__FUNCTION__ . ": OK" . "\n");
  127. print('acl: ' . $res);
  128. }
  129. /**
  130. * List all buckets
  131. *
  132. * @param OssClient $ossClient OssClient instance
  133. * @return null
  134. */
  135. function listBuckets($ossClient)
  136. {
  137. $bucketList = null;
  138. try {
  139. $bucketListInfo = $ossClient->listBuckets();
  140. } catch (OssException $e) {
  141. printf(__FUNCTION__ . ": FAILED\n");
  142. printf($e->getMessage() . "\n");
  143. return;
  144. }
  145. print(__FUNCTION__ . ": OK" . "\n");
  146. $bucketList = $bucketListInfo->getBucketList();
  147. foreach ($bucketList as $bucket) {
  148. print($bucket->getLocation() . "\t" . $bucket->getName() . "\t" . $bucket->getCreatedate() . "\n");
  149. }
  150. }