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:

// This example uses the endpoint of the China (Hangzhou) region. Specify the actual endpoint based on your requirements.
String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to log on to OSS, because the account has permissions on all API operations. We recommend that you use your RAM user's credentials to call API operations or perform routine operations and maintenance. To create your RAM user, log on to https://ram.console.aliyun.com.
String accessKeyId = "<yourAccessKeyId>";
String accessKeySecret = "<yourAccessKeySecret>";

// Create an OSSClient instance.
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

// Create a CreateBucketRequest object.
CreateBucketRequest createBucketRequest = new CreateBucketRequest("<yourBucketName>");

// The following code provides an example on how to specify the storage class and the type of redundant storage when you create a bucket.
// In this example, the storage class of the bucket is Standard.
// createBucketRequest.setStorageClass(StorageClass.Standard);
// By default, the type of redundant storage is DataRedundancyType.LRS, indicating locally redundant storage (LRS). To set the type of redundant storage to zone-redundant storage, replace DataRedundancyType.LRS with DataRedundancyType.ZRS.
// createBucketRequest.setDataRedundancyType(DataRedundancyType.ZRS)

// Create a bucket.
ossClient.createBucket(createBucketRequest);

// Shut down the OSSClient instance.
ossClient.shutdown();

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.

To create a bucket of the Infrequent Access (IA) or Archive storage class, use OSS SDK for Java 2.6.0 or later. The following code provides an example on how to create a bucket:

CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucketName);
// Set the ACL of the bucket to public read. The default ACL is private.
createBucketRequest.setCannedACL(CannedAccessControlList.PublicRead);
// Set the storage class of the bucket to IA. The default storage class is Standard.
createBucketRequest.setStorageClass(StorageClass.IA);
ossClient.createBucket(createBucketRequest);