Create a bucket (Harmony SDK)

Updated at:

A bucket is a container for storing objects in Object Storage Service (OSS). All objects in OSS are stored inside buckets.

Usage notes

  • For supported regions and endpoints, see Regions and endpoints.

  • Starting October 13, 2025, at 10:00 (UTC+8), OSS rolled out a phased adjustment across all regions to enable Block public access by default for buckets created through the API, OSS SDKs, or ossutil. Once enabled, you cannot set public ACLs (public read or public read/write) or bucket policies that allow public access. If your use case requires public access, disable Block public access after the bucket is created. For the rollout schedule by region, see the official announcement.

Prerequisites

Before you begin, make sure that you have:

  • An Alibaba Cloud account with the required permissions. RAM users and RAM roles have no permissions by default — grant them access through RAM Policy or bucket policies

  • The oss:PutBucket permission to create a bucket

  • (Optional) The oss:PutBucketAcl permission to modify the bucket ACL after creation

Create a bucket

All examples use Security Token Service (STS) credentials — a temporary AccessKey ID, AccessKey secret, and STS token — to initialize the OSS client. Replace the placeholder values with your actual credentials and bucket name before running.

import Client, { RequestError, EDataRedundancyType } from '@aliyun/oss';

// Initialize the OSS client with STS credentials.
const client = new Client({
  accessKeyId: '<your-sts-access-key-id>',
  accessKeySecret: '<your-sts-access-key-secret>',
  securityToken: '<your-sts-token>',
  // Specify the region where you want to create the bucket.
  // Example: oss-cn-hangzhou for China (Hangzhou).
  region: 'oss-cn-hangzhou',
});

const putBucket = async () => {
  try {
    const res = await client.putBucket({
      bucket: '<your-bucket-name>',
      // Set the data redundancy type.
      // EDataRedundancyType.ZRS: zone-redundant storage (ZRS).
      createBucketConfig: {
        dataRedundancyType: EDataRedundancyType.ZRS,
      },
    });

    console.log(JSON.stringify(res));
  } catch (err) {
    if (err instanceof RequestError) {
      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 {
      console.log('unknown error: ', err);
    }
  }
};

putBucket();

Parameters

The putBucket method accepts the following parameters:

ParameterTypeRequiredDescription
bucketstringYesBucket name
createBucketConfig.storageClassEStorageClassNoStorage class. Valid values: EStorageClass.STANDARD, EStorageClass.IA, EStorageClass.ARCHIVE, EStorageClass.COLD_ARCHIVE
createBucketConfig.dataRedundancyTypeEDataRedundancyTypeNoData redundancy type. Valid values: EDataRedundancyType.LRS (locally redundant), EDataRedundancyType.ZRS (zone-redundant storage)
aclEBucketAclNoBucket access control list (ACL). Valid values: EBucketAcl.PRIVATE, EBucketAcl.PUBLIC_READ, EBucketAcl.PUBLIC_READ_WRITE
resourceGroupIdstringNoID of the resource group to associate with the bucket

ACL values

ValueAccess
EBucketAcl.PRIVATEBucket owner only has read and write access
EBucketAcl.PUBLIC_READAll users have read access; only bucket owner has write access
EBucketAcl.PUBLIC_READ_WRITEAll users have read and write access

Common scenarios

Create a bucket in a specific storage class

Pass storageClass in createBucketConfig to specify the storage class for the bucket.

const res = await client.putBucket({
  bucket: '<your-bucket-name>',
  createBucketConfig: {
    storageClass: EStorageClass.ARCHIVE, // Archive storage class.
    dataRedundancyType: EDataRedundancyType.ZRS,
  },
});

Create a bucket with a specific ACL

Pass acl to set the bucket's access control list (ACL) at creation time.

const res = await client.putBucket({
  bucket: '<your-bucket-name>',
  acl: EBucketAcl.PRIVATE,
  createBucketConfig: {
    dataRedundancyType: EDataRedundancyType.ZRS,
  },
});

Create a bucket in a specific resource group

Pass resourceGroupId to associate the bucket with a resource group.

const res = await client.putBucket({
  bucket: '<your-bucket-name>',
  resourceGroupId: '<your-resource-group-id>',
  createBucketConfig: {
    dataRedundancyType: EDataRedundancyType.ZRS,
  },
});

References