Manage bucket ACLs (C++ SDK V2)
Bucket access control lists (ACLs) control who can read and write objects in a bucket. You can use the OSS C++ SDK V2 to set and get bucket ACLs.
Usage notes
-
Before you run the sample code, replace
<region>with an actual region ID, such ascn-hangzhou. -
The sample code in this topic gets access credentials from environment variables.
-
To set a bucket ACL, you must have the
oss:PutBucketAclpermission. To get a bucket ACL, you must have theoss:GetBucketAclpermission.
Bucket ACLs
|
ACL |
Value |
Description |
|
Private |
|
Only the bucket owner can read and write objects in the bucket. |
|
Public-read |
|
Only the bucket owner can write objects to the bucket. Anyone, including anonymous users, can read objects in the bucket. |
|
Public-read-write |
|
Anyone, including anonymous users, can read and write objects in the bucket. |
Setting the ACL to public-read-write grants public access to all data in the bucket, which poses a security risk. Use this setting with caution.
Set a bucket ACL
The following code sets a bucket ACL:
#include <iostream>
#include "alibabacloud/oss2/ClientConfiguration.h"
#include "alibabacloud/oss2/OSSClient.h"
#include "alibabacloud/oss2/credentials/CredentialsProvider.h"
namespace oss = alibabacloud::oss2;
int main() {
auto conf = oss::ClientConfiguration::loadDefault();
conf.region = "<region>";
conf.credentialsProvider = std::make_shared<oss::EnvironmentVariableCredentialsProvider>();
oss::OSSClient client(conf);
auto outcome = client.putBucketAcl(
oss::models::PutBucketAclRequest()
.setBucket("examplebucket")
.setAcl("public-read"));
if (!outcome.has_value()) {
auto& e = outcome.error();
std::cerr << "Failed to set the bucket ACL"
<< ", code: " << e.getCode()
<< ", message: " << e.getMessage()
<< ", requestId: " << e.getRequestId() << std::endl;
return 1;
}
auto& result = outcome.value();
std::cout << "Bucket ACL set successfully"
<< ", status: " << result.getStatusCode()
<< ", requestId: " << result.getRequestId() << std::endl;
return 0;
}
Get a bucket ACL
The following code gets a bucket ACL:
#include <iostream>
#include "alibabacloud/oss2/ClientConfiguration.h"
#include "alibabacloud/oss2/OSSClient.h"
#include "alibabacloud/oss2/credentials/CredentialsProvider.h"
namespace oss = alibabacloud::oss2;
int main() {
auto conf = oss::ClientConfiguration::loadDefault();
conf.region = "<region>";
conf.credentialsProvider = std::make_shared<oss::EnvironmentVariableCredentialsProvider>();
oss::OSSClient client(conf);
auto outcome = client.getBucketAcl(
oss::models::GetBucketAclRequest()
.setBucket("examplebucket"));
if (!outcome.has_value()) {
auto& e = outcome.error();
std::cerr << "Failed to get the bucket ACL"
<< ", code: " << e.getCode()
<< ", message: " << e.getMessage()
<< ", requestId: " << e.getRequestId() << std::endl;
return 1;
}
auto& result = outcome.value();
auto& acp = result.getAccessControlPolicy();
std::cout << "Bucket ACL retrieved successfully"
<< ", status: " << result.getStatusCode() << std::endl;
if (acp.owner) {
std::cout << "Owner Id: " << acp.owner->id.value_or("") << std::endl;
}
if (acp.accessControlList) {
std::cout << "ACL Grant: " << acp.accessControlList->grant.value_or("") << std::endl;
}
return 0;
}
References
-
For the complete sample code for setting a bucket ACL, see PutBucketAcl.cpp.
-
For the complete sample code for getting a bucket ACL, see GetBucketAcl.cpp.