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.

Usage notes

  • In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS by using other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about the regions and endpoints supported by OSS, see Regions and endpoints.
  • In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient by using custom domain names or STS, see Create an OSSClient instance.
  • To create a bucket, you must have the oss:PutBucket permission. For more information, see Attach a custom policy to a RAM user.

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.