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 provides an example on how to create a bucket named examplebucket:

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

 // Create the bucket and enable the hierarchical namespace feature for the bucket. 
 CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucketName).withHnsStatus(HnsStatus.Enabled);
 // The following code provides an example on how to specify the storage class, access control list (ACL), and redundancy type of the bucket that you want to create. 
 // In this example, the storage class of the bucket is set to Standard. 
 createBucketRequest.setStorageClass(StorageClass.Standard);
 // By default, the redundancy type of a bucket is set to DataRedundancyType.LRS, which indicates locally redundant storage (LRS). 
 createBucketRequest.setDataRedundancyType(DataRedundancyType.LRS);
 // Set the ACL of the bucket to public-read. The default value is private. 
 createBucketRequest.setCannedACL(CannedAccessControlList.PublicRead);
 ossClient.createBucket(createBucketRequest);

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

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.