A bucket is a container for objects stored in OSS. Every object is contained in a bucket. This topic describes how to configure and obtain the access control list (ACL) of a bucket.

Configure the ACL of a bucket

The following table describes the ACLs for buckets.

ACL Description Value
Private Only the owner or authorized users of the bucket have the read and write permissions on objects in the bucket. OSS_ACL_PRIVATE
Public read Only the owner or authorized users of the bucket have the read and write permissions on objects in the bucket. Other users have only the read permissions on the objects in the bucket. Exercise caution when you grant this permission. OSS_ACL_PUBLIC_READ
Public read/write All users have the read and write permissions on all objects in the bucket. Exercise caution when you grant this permission. OSS_ACL_PUBLIC_READ_WRITE

For more information about ACLs, see Access control in OSS Developer Guide.

The following code provides an example on how to configure the ACL for a bucket:

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

int main(void)
{
    /*Initialize the OSS account information.*/
    std::string AccessKeyId = "yourAccessKeyId";
    std::string AccessKeySecret = "yourAccessKeySecret";
    std::string Endpoint = "yourEndpoint";
    std::string BucketName = "yourBucketName";

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

    ClientConfiguration conf;
    OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);

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

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

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

For more information about how to configure the ACL for a bucket, see PutBucketACL.

Obtain the ACL of a bucket

The following code provides an example on how to obtain the ACL of a bucket:

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

int main(void)
{
    /*Initialize the OSS account information.*/
    std::string AccessKeyId = "yourAccessKeyId";
    std::string AccessKeySecret = "yourAccessKeySecret";
    std::string Endpoint = "yourEndpoint";
    std::string BucketName = "yourBucketName";

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

    ClientConfiguration conf;
    OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);

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

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

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

For more information about how to obtain the ACL of a bucket, see GetBucketAcl.