A bucket is a container for objects stored in Object Storage Service (OSS). All objects in OSS are stored in buckets. This topic describes how to configure and query the access control list (ACL) of 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.
In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient by using custom domain names or Security Token Service (STS), see Create an OSSClient instance.
To configure the ACL of a bucket, you must have the
oss:PutBucketAcl
permission. To query the ACL of a bucket, you must have theoss:GetBucketAcl
permission. For more information, see Attach a custom policy to a RAM user.
Configure the ACL of a bucket
The following table describes the bucket ACLs.
ACL | Description | Method |
Private | Only the bucket owner can perform read and write operations on objects in the bucket. Other users cannot access the objects in the bucket. | CannedAccessControlList::Private |
Public read | Only the bucket owner can write data to objects in the bucket. Other users, including anonymous users, can only read objects in the bucket. Warning If you set the ACL of a bucket to public read, all users can access the objects in the bucket over the Internet. This may result in unauthorized access to the data in your bucket, and you may be charged unexpected fees. We recommend that you exercise caution when you set the ACL of a bucket to this value. | CannedAccessControlList::PublicRead |
Public read/write | All users, including anonymous users, can perform read and write operations on the objects in the bucket. Warning If you set the ACL of a bucket to public read/write, all users can access the objects in the bucket and write data to the bucket over the Internet. This may result in unauthorized access to the data in your bucket, and you may be charged unexpected fees. If a user uploads prohibited data or information, your legitimate interests and rights may be infringed. We recommend that you do not set the ACL of a bucket to this value unless necessary. | CannedAccessControlList::PublicReadWrite |
The following code provides an example on how to configure 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 resources such as network resources. */
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 exceptions. */
std::cout << "SetBucketAcl fail" <<
",code:" << outcome.error().Code() <<
",message:" << outcome.error().Message() <<
",requestId:" << outcome.error().RequestId() << std::endl;
return -1;
}
/* Release resources such as network resources. */
ShutdownSdk();
return 0;
}
Query the ACL of a bucket
The following sample code provides an example on how to query 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 resources such as network resources. */
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);
/* Query 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 exceptions. */
std::cout << "getBucketAcl fail" <<
",code:" << outcome.error().Code() <<
",message:" << outcome.error().Message() <<
",requestId:" << outcome.error().RequestId() << std::endl;
return -1;
}
/* Release resources such as network resources. */
ShutdownSdk();
return 0;
}
References
For the complete sample code that is used to manage bucket ACLs, visit GitHub.
For more information about the API operation that you can call to configure the ACL of a bucket, see PutBucketAcl.
For more information about the API operation that you can call to query the ACL of a bucket, see GetBucketAcl.