No Description

Bucket.php 4.6KB

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. //******************************* 简单使用 ****************************************************************
  9. //创建bucket
  10. $ossClient->createBucket($bucket, OssClient::OSS_ACL_TYPE_PUBLIC_READ_WRITE);
  11. Common::println("bucket $bucket created");
  12. // 判断Bucket是否存在
  13. $doesExist = $ossClient->doesBucketExist($bucket);
  14. Common::println("bucket $bucket exist? " . ($doesExist ? "yes" : "no"));
  15. // 获取Bucket列表
  16. $bucketListInfo = $ossClient->listBuckets();
  17. // 设置bucket的ACL
  18. $ossClient->putBucketAcl($bucket, OssClient::OSS_ACL_TYPE_PUBLIC_READ_WRITE);
  19. Common::println("bucket $bucket acl put");
  20. // 获取bucket的ACL
  21. $acl = $ossClient->getBucketAcl($bucket);
  22. Common::println("bucket $bucket acl get: " . $acl);
  23. //******************************* 完整用法参考下面函数 ****************************************************
  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. * 创建一个存储空间
  32. * acl 指的是bucket的访问控制权限,有三种,私有读写,公共读私有写,公共读写。
  33. * 私有读写就是只有bucket的拥有者或授权用户才有权限操作
  34. * 三种权限分别对应 (OssClient::OSS_ACL_TYPE_PRIVATE,OssClient::OSS_ACL_TYPE_PUBLIC_READ, OssClient::OSS_ACL_TYPE_PUBLIC_READ_WRITE)
  35. *
  36. * @param OssClient $ossClient OssClient实例
  37. * @param string $bucket 要创建的存储空间名称
  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. * 判断Bucket是否存在
  53. *
  54. * @param OssClient $ossClient OssClient实例
  55. * @param string $bucket 存储空间名称
  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. * 删除bucket,如果bucket不为空则bucket无法删除成功, 不为空表示bucket既没有object,也没有未完成的multipart上传时的parts
  74. *
  75. * @param OssClient $ossClient OssClient实例
  76. * @param string $bucket 待删除的存储空间名称
  77. * @return null
  78. */
  79. function deleteBucket($ossClient, $bucket)
  80. {
  81. try {
  82. $ossClient->deleteBucket($bucket);
  83. } catch (OssException $e) {
  84. printf(__FUNCTION__ . ": FAILED\n");
  85. printf($e->getMessage() . "\n");
  86. return;
  87. }
  88. print(__FUNCTION__ . ": OK" . "\n");
  89. }
  90. /**
  91. * 设置bucket的acl配置
  92. *
  93. * @param OssClient $ossClient OssClient实例
  94. * @param string $bucket 存储空间名称
  95. * @return null
  96. */
  97. function putBucketAcl($ossClient, $bucket)
  98. {
  99. $acl = OssClient::OSS_ACL_TYPE_PRIVATE;
  100. try {
  101. $ossClient->putBucketAcl($bucket, $acl);
  102. } catch (OssException $e) {
  103. printf(__FUNCTION__ . ": FAILED\n");
  104. printf($e->getMessage() . "\n");
  105. return;
  106. }
  107. print(__FUNCTION__ . ": OK" . "\n");
  108. }
  109. /**
  110. * 获取bucket的acl配置
  111. *
  112. * @param OssClient $ossClient OssClient实例
  113. * @param string $bucket 存储空间名称
  114. * @return null
  115. */
  116. function getBucketAcl($ossClient, $bucket)
  117. {
  118. try {
  119. $res = $ossClient->getBucketAcl($bucket);
  120. } catch (OssException $e) {
  121. printf(__FUNCTION__ . ": FAILED\n");
  122. printf($e->getMessage() . "\n");
  123. return;
  124. }
  125. print(__FUNCTION__ . ": OK" . "\n");
  126. print('acl: ' . $res);
  127. }
  128. /**
  129. * 列出用户所有的Bucket
  130. *
  131. * @param OssClient $ossClient OssClient实例
  132. * @return null
  133. */
  134. function listBuckets($ossClient)
  135. {
  136. $bucketList = null;
  137. try {
  138. $bucketListInfo = $ossClient->listBuckets();
  139. } catch (OssException $e) {
  140. printf(__FUNCTION__ . ": FAILED\n");
  141. printf($e->getMessage() . "\n");
  142. return;
  143. }
  144. print(__FUNCTION__ . ": OK" . "\n");
  145. $bucketList = $bucketListInfo->getBucketList();
  146. foreach ($bucketList as $bucket) {
  147. print($bucket->getLocation() . "\t" . $bucket->getName() . "\t" . $bucket->getCreatedate() . "\n");
  148. }
  149. }