All Products
Search
Document Center

Object Storage Service:Manage bucket ACLs (OSS SDK for C++)

Last Updated:Jun 16, 2026

Set and get bucket ACLs to control read and write permissions on the objects in a bucket.

Usage notes

  • In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS from other Alibaba Cloud services in the same region as OSS, 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 or authenticating with credentials from Security Token Service (STS), see Create an OSSClient instance.

  • To set the ACL of a bucket, you must have the oss:PutBucketAcl permission. To get the ACL of a bucket, you must have the oss:GetBucketAcl permission. For more information, see Grant a custom policy.

Set the ACL of a bucket

OSS supports the following bucket ACLs.

ACL

Description

Method

Private

Only the bucket owner can read and write objects in the bucket. Other users cannot access the objects.

CannedAccessControlList::Private

Public read

Only the bucket owner can write objects in the bucket. Other users, including anonymous users, can only read objects.

Warning

Setting a bucket's ACL to public-read allows anyone on the internet to read objects stored within. This can lead to unintended data exposure and unexpected charges. Exercise caution.

CannedAccessControlList::PublicRead

Public read/write

All users, including anonymous users, can read and write objects in the bucket.

Warning

Setting a bucket's ACL to public read/write allows anyone on the internet to read and write objects stored within. This poses a significant security risk, leading to potential data exposure, unexpected charges, and the upload of malicious or illegal content to your bucket. This ACL is strongly discouraged unless absolutely necessary.

CannedAccessControlList::PublicReadWrite

The following sample code sets the ACL of a bucket:

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

int main(void)
{
    /* Initialize information about the account that is used to access OSS. */
            
    /* Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
    std::string Endpoint = "yourEndpoint";
    /* 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 cn-hangzhou. */
    std::string Region = "yourRegion";
    /* Specify the name of the bucket. Example: examplebucket. */
    std::string BucketName = "examplebucket";

    /* Initialize the SDK. This must be called before using the SDK. */
    InitializeSdk();

    ClientConfiguration conf;
    conf.signatureVersion = SignatureVersionType::V4;
    /* Obtain access credentials from environment variables. Before you run the 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);

    /* Set the ACL of the bucket to private. */
    SetBucketAclRequest request(BucketName, CannedAccessControlList::Private);
    auto outcome = client.SetBucketAcl(request);

    if (outcome.isSuccess()) {    
        std::cout << " setBucketAcl successfully " << std::endl;
    }
    else {
        /* Handle the error. */
        std::cout << "SetBucketAcl fail" <<
        ",code:" << outcome.error().Code() <<
        ",message:" << outcome.error().Message() <<
        ",requestId:" << outcome.error().RequestId() << std::endl;
        return -1;
    }

    /* Shut down the SDK to release resources. */
    ShutdownSdk();
    return 0;
}

Get the ACL of a bucket

The following code gets the bucket ACL:

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

int main(void)
{
    /* Initialize information about the account that is used to access OSS. */
            
    /* Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
    std::string Endpoint = "yourEndpoint";
    /* 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 cn-hangzhou. */
    std::string Region = "yourRegion";
    /* Specify the name of the bucket. Example: examplebucket. */
    std::string BucketName = "examplebucket";

    /* Initialize the SDK. This must be called before using the SDK. */
    InitializeSdk();

    ClientConfiguration conf;
    conf.signatureVersion = SignatureVersionType::V4;
    /* Obtain access credentials from environment variables. Before you run the 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);

    /* Get the ACL of the bucket. */
    GetBucketAclRequest request(BucketName);
    auto outcome = client.GetBucketAcl(request);

    if (outcome.isSuccess()) {    
        std::cout << "getBucketAcl success, acl: " << outcome.result().Acl() << std::endl;
    }
    else {
        /* Handle the error. */
        std::cout << "getBucketAcl fail" <<
        ",code:" << outcome.error().Code() <<
        ",message:" << outcome.error().Message() <<
        ",requestId:" << outcome.error().RequestId() << std::endl;
        return -1;
    }

    /* Shut down the SDK to release resources. */
    ShutdownSdk();
    return 0;
}

References

  • For complete sample code for managing bucket ACLs, visit GitHub.

  • For more information about the API operation for setting the ACL of a bucket, see PutBucketAcl.

  • For more information about the API operation for getting the ACL of a bucket, see GetBucketAcl.