A bucket is a container for objects stored in OSS. Every object is contained in a bucket. This topic describes how to create a bucket.

The following code provides an example on how to create a bucket:

CreateBucketRequest createBucketRequest = new CreateBucketRequest("<bucketName>");

// Specify the ACL of the bucket.
createBucketRequest.setBucketACL(CannedAccessControlList.PublicRead); 
// Specify the region where the bucket is located.
createBucketRequest.setLocationConstraint("oss-cn-hangzhou"); 

// Create the bucket asynchronously.
OSSAsyncTask createTask = oss.asyncCreateBucket(createBucketRequest, new OSSCompletedCallback<CreateBucketRequest, CreateBucketResult>() {
    @Override
    public void onSuccess(CreateBucketRequest request, CreateBucketResult result) {
        Log.d("asyncCreateBucket", "Success");
    }
    @Override
    public void onFailure(CreateBucketRequest request, ClientException clientException, ServiceException serviceException) {
        // Handle request exceptions.
        if (clientException ! = null) {
            // Handle client exceptions, such as network exceptions.
            clientException.printStackTrace();
        }
        if (serviceException ! = null) {
            // Handle service exceptions.
            Log.e("ErrorCode", serviceException.getErrorCode());
            Log.e("RequestId", serviceException.getRequestId());
            Log.e("HostId", serviceException.getHostId());
            Log.e("RawMessage", serviceException.getRawMessage());
        }
    }
});

The preceding code specifies the ACL and region of the bucket.

  • The maximum number of buckets that can be created by an Alibaba Cloud account within a region is 100.
  • When you create a bucket, you can set the bucket ACL. By default, the bucket ACL is set to private if no ACL is set.
  • Bucket names must be globally unique in OSS. A bucket whose name already exists cannot be created.
  • After a bucket is created, the result is returned to the region where the bucket is located.

For more information about the bucket naming conventions, see Bucket.

You can specify the ACL and storage class when you create a bucket. For more information, see Set the ACL for a bucket and Overview of storage classes.