123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- <?php
- require_once __DIR__ . '/Common.php';
- use OSS\OssClient;
- use OSS\Core\OssException;
- $ossClient = Common::getOssClient();
- if (is_null($ossClient)) exit(1);
- $bucket = Common::getBucketName();
- //******************************* Simple Usage****************************************************************
- // Create a bucket
- $ossClient->createBucket($bucket, OssClient::OSS_ACL_TYPE_PUBLIC_READ_WRITE);
- Common::println("bucket $bucket created");
- // Check whether a bucket exists
- $doesExist = $ossClient->doesBucketExist($bucket);
- Common::println("bucket $bucket exist? " . ($doesExist ? "yes" : "no"));
- // Get the bucket list
- $bucketListInfo = $ossClient->listBuckets();
- // Set bucket ACL
- $ossClient->putBucketAcl($bucket, OssClient::OSS_ACL_TYPE_PUBLIC_READ_WRITE);
- Common::println("bucket $bucket acl put");
- // Get bucket ACL
- $acl = $ossClient->getBucketAcl($bucket);
- Common::println("bucket $bucket acl get: " . $acl);
- //******************************* For complete usage, see the following functions ****************************************************
- createBucket($ossClient, $bucket);
- doesBucketExist($ossClient, $bucket);
- deleteBucket($ossClient, $bucket);
- putBucketAcl($ossClient, $bucket);
- getBucketAcl($ossClient, $bucket);
- listBuckets($ossClient);
- /**
- * Create a new bucket
- * acl indicates the access permission of a bucket, including: private, public-read-only/private-read-write, and public read-write.
- * Private indicates that only the bucket owner or authorized users can access the data..
- * 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)
- *
- * @param OssClient $ossClient OssClient instance
- * @param string $bucket Name of the bucket to create
- * @return null
- */
- function createBucket($ossClient, $bucket)
- {
- try {
- $ossClient->createBucket($bucket, OssClient::OSS_ACL_TYPE_PUBLIC_READ_WRITE);
- } catch (OssException $e) {
- printf(__FUNCTION__ . ": FAILED\n");
- printf($e->getMessage() . "\n");
- return;
- }
- print(__FUNCTION__ . ": OK" . "\n");
- }
- /**
- * Check whether a bucket exists.
- *
- * @param OssClient $ossClient OssClient instance
- * @param string $bucket bucket name
- */
- function doesBucketExist($ossClient, $bucket)
- {
- try {
- $res = $ossClient->doesBucketExist($bucket);
- } catch (OssException $e) {
- printf(__FUNCTION__ . ": FAILED\n");
- printf($e->getMessage() . "\n");
- return;
- }
- if ($res === true) {
- print(__FUNCTION__ . ": OK" . "\n");
- } else {
- print(__FUNCTION__ . ": FAILED" . "\n");
- }
- }
- /**
- * Delete a bucket. If the bucket is not empty, the deletion fails.
- * A bucket which is not empty indicates that it does not contain any objects or parts that are not completely uploaded during multipart upload
- *
- * @param OssClient $ossClient OssClient instance
- * @param string $bucket Name of the bucket to delete
- * @return null
- */
- function deleteBucket($ossClient, $bucket)
- {
- try {
- $ossClient->deleteBucket($bucket);
- } catch (OssException $e) {
- printf(__FUNCTION__ . ": FAILED\n");
- printf($e->getMessage() . "\n");
- return;
- }
- print(__FUNCTION__ . ": OK" . "\n");
- }
- /**
- * Set bucket ACL
- *
- * @param OssClient $ossClient OssClient instance
- * @param string $bucket bucket name
- * @return null
- */
- function putBucketAcl($ossClient, $bucket)
- {
- $acl = OssClient::OSS_ACL_TYPE_PRIVATE;
- try {
- $ossClient->putBucketAcl($bucket, $acl);
- } catch (OssException $e) {
- printf(__FUNCTION__ . ": FAILED\n");
- printf($e->getMessage() . "\n");
- return;
- }
- print(__FUNCTION__ . ": OK" . "\n");
- }
- /**
- * Get bucket ACL
- *
- * @param OssClient $ossClient OssClient instance
- * @param string $bucket bucket name
- * @return null
- */
- function getBucketAcl($ossClient, $bucket)
- {
- try {
- $res = $ossClient->getBucketAcl($bucket);
- } catch (OssException $e) {
- printf(__FUNCTION__ . ": FAILED\n");
- printf($e->getMessage() . "\n");
- return;
- }
- print(__FUNCTION__ . ": OK" . "\n");
- print('acl: ' . $res);
- }
- /**
- * List all buckets
- *
- * @param OssClient $ossClient OssClient instance
- * @return null
- */
- function listBuckets($ossClient)
- {
- $bucketList = null;
- try {
- $bucketListInfo = $ossClient->listBuckets();
- } catch (OssException $e) {
- printf(__FUNCTION__ . ": FAILED\n");
- printf($e->getMessage() . "\n");
- return;
- }
- print(__FUNCTION__ . ": OK" . "\n");
- $bucketList = $bucketListInfo->getBucketList();
- foreach ($bucketList as $bucket) {
- print($bucket->getLocation() . "\t" . $bucket->getName() . "\t" . $bucket->getCreatedate() . "\n");
- }
- }
|