A bucket is a container for storing objects. All objects must be stored in a bucket. This topic describes how to create a bucket.
Notes
In this topic, the public endpoint of the China (Hangzhou) region is used. To access OSS from other Alibaba Cloud services in the same region, use an internal endpoint. For details about supported regions and endpoints, see Regions and endpoints.
In this topic, access credentials are obtained from environment variables. For more information about how to configure access credentials, see Configure access credentials.
This topic demonstrates creating an OSSClient instance with an OSS endpoint. For alternative configurations, such as using a custom domain or authenticating with credentials from Security Token Service (STS), see Client configuration.
Starting October 13, 2025, at 10:00 (UTC+8), OSS begins a phased rollout to enable blocking public access by default for all new buckets. This change applies to buckets created through the API, SDKs, and ossutil. For the specific rollout schedule in each region, see the Notice. When this feature is enabled, you cannot grant public access to the bucket, either through ACLs (such as public-read or public-read-write) or bucket policies. If your use case requires public access, disable this setting after the bucket is created.
Permissions
By default, an Alibaba Cloud account has full permissions. RAM users or RAM roles under an Alibaba Cloud account do not have any permissions by default. The Alibaba Cloud account or account administrator must grant operation permissions through RAM policies or Bucket Policy.
API | Action | Definition |
PutBucket |
| Creates a bucket. |
| After creating a bucket, this permission is required to modify the bucket ACL. |
Sample code
The following code provides an example of how to create a bucket named examplebucket.
import com.aliyun.oss.*;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.model.*;
public class Demo {
public static void main(String[] args) throws Exception {
// Set yourEndpoint to the Endpoint of the region where the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set Endpoint to https://oss-cn-hangzhou.aliyuncs.com.
String endpoint = "yourEndpoint";
// Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
// Specify the bucket name.
String bucketName = "examplebucket";
// Specify the resource group ID. If you do not specify a resource group ID, the bucket is added to the default resource group.
//String rsId = "rg-aek27tc****";
// Specify the region where the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set Region to cn-hangzhou.
String region = "cn-hangzhou";
// Create an OSSClient instance.
// After an OSSClient instance is no longer used, call the shutdown method to release resources.
ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
OSS ossClient = OSSClientBuilder.create()
.endpoint(endpoint)
.credentialsProvider(credentialsProvider)
.clientConfiguration(clientBuilderConfiguration)
.region(region)
.build();
try {
// Create a bucket and enable the hierarchical namespace feature.
CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucketName).withHnsStatus(HnsStatus.Enabled);
// If you want to specify the storage class, ACL, and data redundancy type when you create a bucket, see the following code.
// The following code provides an example of how to set the storage class to Standard.
createBucketRequest.setStorageClass(StorageClass.Standard);
// The default data redundancy type is LRS, which is specified as DataRedundancyType.LRS. If you want to set the data redundancy type to ZRS, set the value to DataRedundancyType.ZRS.
createBucketRequest.setDataRedundancyType(DataRedundancyType.ZRS);
// Set the ACL of the bucket to public-read. The default ACL is private.
createBucketRequest.setCannedACL(CannedAccessControlList.PublicRead);
// When you create a bucket in a region that supports resource groups, you can assign the bucket to a resource group.
//createBucketRequest.setResourceGroupId(rsId);
// Create the bucket.
ossClient.createBucket(createBucketRequest);
} catch (OSSException oe) {
System.out.println("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
System.out.println("Error Message:" + oe.getErrorMessage());
System.out.println("Error Code:" + oe.getErrorCode());
System.out.println("Request ID:" + oe.getRequestId());
System.out.println("Host ID:" + oe.getHostId());
} catch (ClientException ce) {
System.out.println("Caught an ClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with OSS, "
+ "such as not being able to access the network.");
System.out.println("Error Message:" + ce.getMessage());
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
}
}References
For the complete sample code for creating a bucket, see the GitHub sample.
For more information about the API operation to create a bucket, see PutBucket.