A bucket is a container used to store objects in Object Storage Service (OSS). Each object is contained in a bucket. This topic describes how to create a bucket.

Sample code

The following code shows how to create a bucket named examplebucket:

// Construct a request to create a bucket. 
// Specify the name of the bucket. 
CreateBucketRequest createBucketRequest = new CreateBucketRequest("examplebucket");
// Set the region in which the bucket is located to the China (Hangzhou) region. 
createBucketRequest.setLocationConstraint("oss-cn-hangzhou");
// Specify the access control list (ACL) of the bucket. 
// createBucketRequest.setBucketACL(CannedAccessControlList.Private);
// Specify the storage class of the bucket. 
// createBucketRequest.setBucketStorageClass(StorageClass.Standard);

// 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());
        }
    }
});

References

  • For more information about the complete sample code that is used to create a bucket, visit GitHub.
  • For more information about the API operation that you can call to create a bucket, see PutBucket.