Create a bucket (Java SDK V1)

Updated at:

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.

  • In this example, a bucket with a region attribute is created. If you want to use the following code to create a bucket without a region attribute, you must replace the endpoint with https://oss-rg-china-mainland.aliyuncs.com. In addition, a bucket without a region attribute supports only the Standard storage class and only locally redundant storage (LRS) for data redundancy, and does not support resource group configuration. For more information about buckets without a region attribute, see Bucket region attributes.
  • Starting at 10:00:00 (UTC+8) on October 13, 2025, OSS gradually enables Block public access by default for buckets that are created by calling API operations or by using OSS SDKs or ossutil in all regions. For the time when the change takes effect in each region, see Announcement. After this feature is enabled, you cannot grant public access permissions, including the public-read or public-read-write ACL and bucket policies that allow public access. If your business requires public access, you can disable block public access 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

Description

PutBucket

oss:PutBucket

Creates a bucket.

oss:PutBucketAcl

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. To run this code, replace the bucket name with a globally unique name that is relevant to your business.

When you create a bucket in a region that supports resource groups, you can assign the bucket to a resource group. For more information about resource groups, see Use resource groups.

Note

When you configure a resource group, you can obtain the resource group ID from the Resource Management console or by calling the ListResourceGroups API operation. For more information, see View a resource group and ListResourceGroups.

import com.aliyun.oss.*;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.model.CannedAccessControlList;
import com.aliyun.oss.model.CreateBucketRequest;
import com.aliyun.oss.model.DataRedundancyType;
import com.aliyun.oss.model.StorageClass;

public class CreateBucket {

    public static void main(String[] args) throws Exception {
        // Set yourEndpoint to the Endpoint of the region where the bucket is located.
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        // 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 CreateBucketRequest object.
            CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucketName);

            // 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 locally redundant storage (LRS), which is specified as DataRedundancyType.LRS. If you want to set the data redundancy type to zone-redundant storage (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.