Manage Object ACLs (C++ SDK V2)
Object ACLs (Access Control Lists) control access to individual objects. You can use the OSS C++ SDK V2 to set and get Object ACLs.
Usage notes
-
Before you run the sample code, replace
<region>with your region, such ascn-hangzhou. -
The sample code in this topic obtains credentials from environment variables.
-
To set and get an Object ACL, you must have the
oss:PutObjectAclandoss:GetObjectAclpermissions.
Object ACL types
The following table lists the supported Object ACL types.
|
Value |
Description |
|---|---|
|
|
The object inherits the ACL from its bucket. |
|
|
Private. Only the object owner or authorized users can read and write the object. |
|
|
Public read. The object owner and authorized users can read and write the object. All other users, including anonymous users, have read-only access to the object. |
|
|
Public read-write. All users can read and write the object. |
Sample code
Set Object ACL
The following sample code sets an Object 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.putObjectAcl(
oss::models::PutObjectAclRequest()
.setBucket("examplebucket")
.setKey("exampleobject.txt")
.setObjectAcl("private"));
if (!outcome.has_value()) {
auto& e = outcome.error();
std::cerr << "Failed to set the Object ACL"
<< ", code: " << e.getCode()
<< ", message: " << e.getMessage()
<< ", requestId: " << e.getRequestId() << std::endl;
return 1;
}
auto& result = outcome.value();
std::cout << "Object ACL set successfully"
<< ", status: " << result.getStatusCode() << std::endl;
return 0;
}
Get Object ACL
The following sample code gets an Object 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.getObjectAcl(
oss::models::GetObjectAclRequest()
.setBucket("examplebucket")
.setKey("exampleobject.txt"));
if (!outcome.has_value()) {
auto& e = outcome.error();
std::cerr << "Failed to get the Object ACL"
<< ", code: " << e.getCode()
<< ", message: " << e.getMessage()
<< ", requestId: " << e.getRequestId() << std::endl;
return 1;
}
auto& result = outcome.value();
auto& acp = result.getAccessControlPolicy();
std::cout << "Object 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 an Object ACL, see PutObjectAcl.cpp.
-
For the complete sample code for getting an Object ACL, see GetObjectAcl.cpp.