通過C++配置Bucket Policy
Bucket Policy是OSS提供的一種針對存儲空間(Bucket)的授權(quán)策略,使您可以精細化地授權(quán)或限制有身份的訪問者(阿里云賬號、RAM用戶、RAM角色)或匿名訪問者對指定OSS資源的訪問。例如,您可以為其他阿里云賬號的RAM用戶授予指定OSS資源的只讀權(quán)限。
注意事項
在配置Bucket Policy之前,請確保您已了解該功能。詳情請參見Bucket Policy。
本文以華東1(杭州)外網(wǎng)Endpoint為例。如果您希望通過與OSS同地域的其他阿里云產(chǎn)品訪問OSS,請使用內(nèi)網(wǎng)Endpoint。關(guān)于OSS支持的Region與Endpoint的對應關(guān)系,請參見OSS地域和訪問域名。
本文以OSS域名新建OSSClient為例。如果您希望通過自定義域名、STS等方式新建OSSClient,請參見新建OssClient。
要設(shè)置Bucket Policy,您必須有
oss:PutBucketPolicy
權(quán)限;要獲取Bucket Policy,您必須有oss:GetBucketPolicy
權(quán)限;要刪除Bucket Policy,您必須有oss:DeleteBucketPolicy
權(quán)限。具體操作,請參見為RAM用戶授權(quán)自定義的權(quán)限策略。
設(shè)置Bucket Policy
以下代碼用于設(shè)置Bucket Policy:
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* 初始化OSS賬號信息。*/
/* yourEndpoint填寫B(tài)ucket所在地域?qū)腅ndpoint。以華東1(杭州)為例,Endpoint填寫為https://oss-cn-hangzhou.aliyuncs.com。*/
std::string Endpoint = "yourEndpoint";
/ *yourRegion填寫B(tài)ucket所在地域?qū)腞egion。以華東1(杭州)為例,Region填寫為cn - hangzhou。 * /
std::string Region = "yourRegion";
/* 填寫B(tài)ucket名稱,例如examplebucket。*/
std::string BucketName = "examplebucket";
/* 初始化網(wǎng)絡等資源。*/
InitializeSdk();
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
/* 從環(huán)境變量中獲取訪問憑證。運行本代碼示例之前,請確保已設(shè)置環(huán)境變量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。*/
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
client.SetRegion(Region);
/* 以下示例用于資源擁有者(即UID為174649585760xxxx的Bucket Owner)通過Bucket Policy授權(quán)指定用戶(UID為20214760404935xxxx的RAM用戶)擁有列舉examplebucket下所有文件的權(quán)限。*/
std::string policy =
R"(
{
"Statement": [
{
"Action": [
"oss:GetObject",
"oss:ListObjects"
],
"Principal": [
"20214760404935xxxx"
],
"Effect" : "Allow",
"Resource" : ["acs:oss:*:174649585760xxxx:examplebucket/*"]
}
],
"Version": "1"
}
)";
SetBucketPolicyRequest request(BucketName);
request.setPolicy(policy);
auto outcome = client.SetBucketPolicy(request);
if (!outcome.isSuccess()) {
/* 異常處理。*/
std::cout << "Set Bucket Policy fail" <<
",code:" << outcome.error().Code() <<
",message:" << outcome.error().Message() <<
",requestId:" << outcome.error().RequestId() << std::endl;
}
/* 釋放網(wǎng)絡等資源。*/
ShutdownSdk();
return 0;
}
獲取Bucket Policy
以下代碼用于獲取Bucket Policy信息:
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* 初始化OSS賬號信息。*/
/* yourEndpoint填寫B(tài)ucket所在地域?qū)腅ndpoint。以華東1(杭州)為例,Endpoint填寫為https://oss-cn-hangzhou.aliyuncs.com。*/
std::string Endpoint = "yourEndpoint";
/ *yourRegion填寫B(tài)ucket所在地域?qū)腞egion。以華東1(杭州)為例,Region填寫為cn - hangzhou。 * /
std::string Region = "yourRegion";
/* 填寫B(tài)ucket名稱,例如examplebucket。*/
std::string BucketName = "examplebucket";
/* 初始化網(wǎng)絡等資源。*/
InitializeSdk();
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
/* 從環(huán)境變量中獲取訪問憑證。運行本代碼示例之前,請確保已設(shè)置環(huán)境變量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。*/
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
client.SetRegion(Region);
/* 獲取Bucket Policy配置信息。*/
GetBucketPolicyRequest request(BucketName);
auto outcome = client.GetBucketPolicy(request);
if (!outcome.isSuccess()) {
/* 異常處理。*/
std::cout << "Get Bucket Policy fail" <<
",code:" << outcome.error().Code() <<
",message:" << outcome.error().Message() <<
",requestId:" << outcome.error().RequestId() << std::endl;
return -1;
}
/* 打印配置信息。*/
std::cout << outcome.result().Policy() << std::endl;
/* 釋放網(wǎng)絡等資源。*/
ShutdownSdk();
return 0;
}
刪除Bucket Policy
以下代碼用于刪除Bucket Policy:
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* 初始化OSS賬號信息。*/
/* yourEndpoint填寫B(tài)ucket所在地域?qū)腅ndpoint。以華東1(杭州)為例,Endpoint填寫為https://oss-cn-hangzhou.aliyuncs.com。*/
std::string Endpoint = "yourEndpoint";
/ *yourRegion填寫B(tài)ucket所在地域?qū)腞egion。以華東1(杭州)為例,Region填寫為cn - hangzhou。 * /
std::string Region = "yourRegion";
/* 填寫B(tài)ucket名稱,例如examplebucket。*/
std::string BucketName = "examplebucket";
/* 初始化網(wǎng)絡等資源。*/
InitializeSdk();
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
/* 從環(huán)境變量中獲取訪問憑證。運行本代碼示例之前,請確保已設(shè)置環(huán)境變量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。*/
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
client.SetRegion(Region);
/* 刪除Bucket Policy。*/
DeleteBucketPolicyRequest request(BucketName);
auto outcome = client.DeleteBucketPolicy(request);
if (!outcome.isSuccess()) {
/* 異常處理。*/
std::cout << "Delete Bucket Policy fail" <<
",code:" << outcome.error().Code() <<
",message:" << outcome.error().Message() <<
",requestId:" << outcome.error().RequestId() << std::endl;
}
/* 釋放網(wǎng)絡等資源。*/
ShutdownSdk();
return 0;
}
相關(guān)文檔
關(guān)于設(shè)置Bucket Policy的API接口說明,請參見PutBucketPolicy。
關(guān)于獲取Bucket Policy的API接口說明,請參見GetBucketPolicy。
關(guān)于刪除Bucket Policy的API接口說明,請參見DeleteBucketPolicy。