All Products
Search
Document Center

Object Storage Service:Create buckets by using OSS SDK for Android

Last Updated:Feb 20, 2024

A bucket is a container used to store objects in Object Storage Service (OSS). All objects in OSS are stored in buckets. This topic describes how to create a bucket.

Examples

The following sample code provides an example on how to create a bucket named examplebucket.

Note

The region of the bucket is determined by the region of the endpoint specified in initialization.

// Construct a request to create a bucket. 
// Specify the name of the bucket. 
CreateBucketRequest createBucketRequest=new CreateBucketRequest("examplebucket");. 
// 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 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.

  • For more information about how to initialize an OSSClient instance, see Initialization.