Manage buckets

Last Updated: Jun 04, 2017

OSS uses buckets as the namespaces and as the management objects for advanced features such as billing, permission control, and logging. The bucket name is globally unique throughout the OSS and cannot be changed. Every object stored on the OSS must be included in a bucket.

Create a bucket

The following code creates a bucket:

  1. CreateBucketRequest createBucketRequest = new CreateBucketRequest("<bucketName>");
  2. createBucketRequest.setBucketACL(CannedAccessControlList.PublicRead); // Specify the ACL permission of the bucket
  3. createBucketRequest.setLocationConstraint("oss-cn-hangzhou"); // Specify the data center that stores the bucket
  4. OSSAsyncTask createTask = oss.asyncCreateBucket(createBucketRequest, new OSSCompletedCallback<CreateBucketRequest, CreateBucketResult>() {
  5. @Override
  6. public void onSuccess(CreateBucketRequest request, CreateBucketResult result) {
  7. Log.d("locationConstraint", request.getLocationConstraint());
  8. }
  9. @Override
  10. public void onFailure(CreateBucketRequest request, ClientException clientException, ServiceException serviceException) {
  11. // Request exception
  12. if (clientException != null) {
  13. // Local exception, such as a network exception
  14. clientException.printStackTrace();
  15. }
  16. if (serviceException != null) {
  17. // Service exception
  18. Log.e("ErrorCode", serviceException.getErrorCode());
  19. Log.e("RequestId", serviceException.getRequestId());
  20. Log.e("HostId", serviceException.getHostId());
  21. Log.e("RawMessage", serviceException.getRawMessage());
  22. }
  23. }
  24. });

The above code specifies the ACL and data center for the created bucket.

  • Each user can have a maximum of 10 buckets.
  • The name of each bucket is globally unique. If the name of a newly created bucket is the same as an in-service bucket belonging to another user, the creation will fail.
  • When you create a bucket, you can configure the bucket ACL. If no ACL is configured, the bucket permission is private by default.
  • The result of successful bucket creation will be returned to the data center where the bucket is located.

Get the bucket ACL permission

The following code gets the ACL of a bucket:

  1. GetBucketACLRequest getBucketACLRequest = new GetBucketACLRequest("<bucketName>");
  2. OSSAsyncTask getBucketAclTask = oss.asyncGetBucketACL(getBucketACLRequest, new OSSCompletedCallback<GetBucketACLRequest, GetBucketACLResult>() {
  3. @Override
  4. public void onSuccess(GetBucketACLRequest request, GetBucketACLResult result) {
  5. Log.d("BucketAcl", result.getBucketACL());
  6. Log.d("Owner", result.getBucketOwner());
  7. Log.d("ID", result.getBucketOwnerID());
  8. }
  9. @Override
  10. public void onFailure(GetBucketACLRequest request, ClientException clientException, ServiceException serviceException) {
  11. // Request exception
  12. if (clientException != null) {
  13. // Local exception, such as a network exception
  14. clientException.printStackTrace();
  15. }
  16. if (serviceException != null) {
  17. // Service exception
  18. Log.e("ErrorCode", serviceException.getErrorCode());
  19. Log.e("RequestId", serviceException.getRequestId());
  20. Log.e("HostId", serviceException.getHostId());
  21. Log.e("RawMessage", serviceException.getRawMessage());
  22. }
  23. }
  24. });

When you use the above code to obtain the bucket ACL permission, note the following points:

  • Currently, three access permissions are available for a bucket: public-read-write, public-read, and private.
  • Only the bucket owner can use the GetBucketACL interface.
  • The returned result contains the bucket ACL permission and the ID and name (consistent with the ID) of the bucket owner.

Delete a bucket

The following code deletes a bucket:

  1. DeleteBucketRequest deleteBucketRequest = new DeleteBucketRequest("<bucketName>");
  2. OSSAsyncTask deleteBucketTask = oss.asyncDeleteBucket(deleteBucketRequest, new OSSCompletedCallback<DeleteBucketRequest, DeleteBucketResult>() {
  3. @Override
  4. public void onSuccess(DeleteBucketRequest request, DeleteBucketResult result) {
  5. Log.d("DeleteBucket", "Success!");
  6. }
  7. @Override
  8. public void onFailure(DeleteBucketRequest request, ClientException clientException, ServiceException serviceException) {
  9. // Request exception
  10. if (clientException != null) {
  11. // Local exception, such as a network exception
  12. clientException.printStackTrace();
  13. }
  14. if (serviceException != null) {
  15. // Service exception
  16. Log.e("ErrorCode", serviceException.getErrorCode());
  17. Log.e("RequestId", serviceException.getRequestId());
  18. Log.e("HostId", serviceException.getHostId());
  19. Log.e("RawMessage", serviceException.getRawMessage());
  20. }
  21. });

Notes:

  • To prevent accidental deletion, OSS does not allow users to delete a non-empty bucket.
  • Only the bucket owner has the permission to delete the bucket.
Thank you! We've received your feedback.