Create a bucket (Harmony SDK)

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.

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

oss:PutBucket

Creates a bucket.

oss:PutBucketAcl

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

The following sample code creates a bucket that belongs to the specified resource group:

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 putBucketWithResourceGroupId = async () => {
  try {
    // Create a bucket that belongs to the specified resource group.
    // Specify the following parameters:
    // - bucket: the name of the bucket.
    // - resourceGroupId: the ID of the resource group.
    const res = await client.putBucket({
      bucket: 'yourBucketName',
      resourceGroupId: 'yourResourceGroupId',
      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.
putBucketWithResourceGroupId();

Create a bucket in the specified storage class

The following sample code creates a bucket that has the specified storage class:

import Client, { RequestError, EStorageClass } 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 putBucketWithStorageClass = async () => {
  try {
    // Create a bucket that is in the specified storage class.
    // Specify the following parameters:
    // - bucket: the name of the bucket.
    // - createBucketConfig: the bucket configuration.
    // - storageClass: the storage class.
    const res = await client.putBucket({
      bucket: 'yourBucketName',
      createBucketConfig: {
        storageClass: EStorageClass.ARCHIVE,
        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.
putBucketWithStorageClass();

Create a bucket with the specified ACL

The following sample code creates a bucket that has the specified access control list (ACL):

import Client, { RequestError, EBucketAcl } 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 putBucketWithAcl = async () => {
  try {
    // Create a bucket that has the specified ACL.
    // Specify the following parameters:
    // - bucket: the name of the bucket.
    // - acl: the ACL of the bucket. Valid values:
    //   - EBucketAcl.PRIVATE: Only the bucket owner has read and write access.
    //   - EBucketAcl.PUBLIC_READ: All users have read access to bucket. Only the bucket owner has write access to it.
    //   - EBucketAcl.PUBLIC_READ_WRITE: All users have read and write access to the bucket.
    const res = await client.putBucket({
      bucket: 'yourBucketName',
      acl: EBucketAcl.PRIVATE,
      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.
putBucketWithAcl();

Reference

For more information about creating a bucket, see PutBucket.