All Products
Search
Document Center

Object Storage Service:Create a bucket (OSS SDK for C++)

Last Updated:Feb 27, 2026

A bucket is a container for storing objects in Object Storage Service (OSS). All objects in OSS are stored in buckets. Learn how to create a bucket by using the OSS C++ SDK.

Prerequisites

  • The OSS C++ SDK is installed. For more information, see Installation.

  • The OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. For more information, see Configure access credentials.

Usage notes

  • This topic uses the public endpoint of the China (Hangzhou) region as an example. If you want to access OSS from other Alibaba Cloud services in the same region, use an internal endpoint. For more information about OSS regions and endpoints, see Regions and endpoints.

  • This topic demonstrates creating an ossclient instance with an OSS endpoint. For alternative configurations, such as using a custom domain name or authenticating with credentials from Security Token Service (STS), see Create an OSSClient instance.

  • From 10:00 (UTC+8) on October 13, 2025, OSS has started to enable Block Public Access by default for all new buckets. This change is rolled out in phases across all regions. For the specific timeline in each region, see the [Official Announcement] Adjustment to the Public Access Blocking Configurations for Newly Created Buckets. When Block Public Access is enabled, you cannot grant public access to a bucket during creation. This includes setting public ACLs (such as public-read or public-read-write) or using bucket policies that allow public access. If you need public access, first create the bucket, then disable Block Public Access before configuring public permissions.

Permissions

By default, an Alibaba Cloud account has full permissions. RAM users and RAM roles do not have any permissions by default. The account owner or a RAM administrator must grant these users or roles the required permissions by using RAM policies or bucket policies.

The following table lists the API operations and RAM actions required for creating a bucket.

API

Action

Description

PutBucket

oss:PutBucket

Creates a bucket.

PutBucket

oss:PutBucketAcl

Required if you specify an ACL other than private when creating the bucket.

Sample code

The following code creates a bucket named examplebucket with the following configurations:

  • Storage class: Infrequent Access (IA). Set the storage class by using StorageClass::IA in the CreateBucketRequest constructor. For other storage classes, see Storage classes.

  • Data redundancy type: zone-redundant storage (ZRS). Set the data redundancy type by calling request.setDataRedundancyType(DataRedundancyType::ZRS).

#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;

int main(void)
{
    /* Set the connection and bucket parameters. */

    /* Specify the endpoint of the region where the bucket is located.
       For example, if the bucket is in the China (Hangzhou) region,
       set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
    std::string Endpoint = "yourEndpoint";

    /* Specify the region where the bucket is located.
       For example, if the bucket is in the China (Hangzhou) region,
       set the region to cn-hangzhou. */
    std::string Region = "yourRegion";

    /* Specify the name of the bucket. Example: examplebucket. */
    std::string BucketName = "examplebucket";

    /* Initialize network resources. */
    InitializeSdk();

    ClientConfiguration conf;
    conf.signatureVersion = SignatureVersionType::V4;
    /* 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. */
    auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
    OssClient client(Endpoint, credentialsProvider, conf);
    client.SetRegion(Region);

    /* Specify the bucket's name and storage class.
    By default, the created bucket is private. No ACL is specified in this request to align with the Block Public Access feature, which is enabled by default for new buckets.
    To create a public bucket, you must first create it as private, then disable Block Public Access, and finally set the desired public ACL. */
    CreateBucketRequest request(BucketName, StorageClass::IA);
    /* Set the data redundancy type to ZRS. */
    request.setDataRedundancyType(DataRedundancyType::ZRS);

    /* Create the bucket. */
    auto outcome = client.CreateBucket(request);

    if (!outcome.isSuccess()) {
        /* Handle exceptions. */
        std::cout << "CreateBucket fail" <<
        ",code:" << outcome.error().Code() <<
        ",message:" << outcome.error().Message() <<
        ",requestId:" << outcome.error().RequestId() << std::endl;
        return -1;
    }

    /* Release network resources. */
    ShutdownSdk();
    return 0;
}

References

  • For the complete sample code for creating buckets, see GitHub.

  • For more information about the PutBucket API operation, see PutBucket.