Create a bucket (Harmony SDK)
A bucket is a container for storing objects. All objects must be stored in a bucket. This topic describes how to create a bucket.
Usage notes
For information about the regions and endpoints supported by OSS, see Regions and endpoints.
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 |
|
Creates a bucket. |
|
|
After creating a bucket, this permission is required to modify the bucket ACL. |
Sample code
The following sample code creates a bucket:
import Client, { RequestError } from '@aliyun/oss';
// Create an OSS client instance.
const client = new Client({
// Specify the AccessKey ID obtained from STS.
accessKeyId: 'yourAccessKeyId',
// Specify the AccessKey secret obtained from STS.
accessKeySecret: 'yourAccessKeySecret',
// Specify the STS token obtained from STS.
securityToken: 'yourSecurityToken',
// Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to oss-cn-hangzhou.
region: 'oss-cn-hangzhou',
});
const putBucket = async () => {
try {
// Use the putBucket method to create a bucket.
const res = await client.putBucket({
bucket: 'yourBucketName', // Specify the name of the bucket.
createBucketConfig: {
dataRedundancyType: EDataRedundancyType.ZRS, // Set the redundancy type of the bucket to zone-redundant storage.
}
});
// Display the response.
console.log(JSON.stringify(res));
} catch (err) {
// Capture possible errors.
if (err instanceof RequestError) {
// In case of a RequestError, display error information, such as the error code, message, request ID, and status code.
console.log('code: ', err.code);
console.log('message: ', err.message);
console.log('requestId: ', err.requestId);
console.log('status: ', err.status);
console.log('ec: ', err.ec);
} else {
//In case of an unknown error, display the error.
console.log('unknown error: ', err);
}
}
};
// Call the function to create the bucket.
putBucket();Common scenarios
Create a bucket within a specific resource group
Create a bucket in the specified storage class
Create a bucket with the specified ACL
Reference
For more information about creating a bucket, see PutBucket.