Une stratégie de compartiment est une politique d'autorisation fournie par Object Storage Service (OSS) pour les compartiments. Elle permet d'accorder ou de refuser un accès granulaire aux ressources OSS spécifiées aux utilisateurs authentifiés (comptes Alibaba Cloud, utilisateurs Resource Access Management (RAM) et rôles RAM) ou aux visiteurs anonymes. Par exemple, vous pouvez accorder des autorisations en lecture seule sur des ressources OSS spécifiques à un utilisateur RAM appartenant à un autre compte Alibaba Cloud.
Notes
Assurez-vous de bien comprendre le fonctionnement des stratégies de compartiment avant d'en configurer une. Pour plus d'informations, consultez la rubrique Stratégie de compartiment.
Cette rubrique utilise le point de terminaison public de la région Chine (Hangzhou). Si vous souhaitez accéder à OSS depuis d'autres services Alibaba Cloud situés dans la même région, utilisez un point de terminaison interne. Pour plus d'informations sur les régions et les points de terminaison OSS, consultez la rubrique Régions et points de terminaison.
Cette rubrique explique comment créer une instance OssClient avec un point de terminaison OSS. Pour d'autres configurations, telles que l'utilisation d'un nom de domaine personnalisé ou l'authentification via des identifiants du Security Token Service (STS), consultez la rubrique Créer une instance OssClient.
Pour définir une stratégie de compartiment, vous devez disposer de l'autorisation
oss:PutBucketPolicy. Pour obtenir une stratégie de compartiment, vous devez disposer de l'autorisationoss:GetBucketPolicy. Pour supprimer une stratégie de compartiment, vous devez disposer de l'autorisationoss:DeleteBucketPolicy. Pour plus d'informations, consultez la rubrique Accorder une stratégie personnalisée.
Définir une stratégie de compartiment
Voici un exemple de code pour définir une stratégie de compartiment :
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* Initialize OSS account information. */
/* Set yourEndpoint to the Endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
std::string Endpoint = "yourEndpoint";
/* Set yourRegion to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the Region to cn-hangzhou. */
std::string Region = "yourRegion";
/* Specify the bucket name. For example, examplebucket. */
std::string BucketName = "examplebucket";
/* Initialize network resources. */
InitializeSdk();
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
/* Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set. */
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
client.SetRegion(Region);
/* The following example shows how a resource owner (the owner of the bucket with UID 174649585760xxxx) uses a bucket policy to grant a specified user (a RAM user with UID 20214760404935xxxx) the permissions to list all files in the examplebucket. */
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()) {
/* Handle the exception. */
std::cout << "Set Bucket Policy fail" <<
",code:" << outcome.error().Code() <<
",message:" << outcome.error().Message() <<
",requestId:" << outcome.error().RequestId() << std::endl;
}
/* Release network resources. */
ShutdownSdk();
return 0;
}
Obtenir une stratégie de compartiment
Voici un exemple de code pour obtenir une stratégie de compartiment :
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* Initialize OSS account information. */
/* Set yourEndpoint to the Endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
std::string Endpoint = "yourEndpoint";
/* Set yourRegion to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the Region to cn-hangzhou. */
std::string Region = "yourRegion";
/* Specify the bucket name. For example, examplebucket. */
std::string BucketName = "examplebucket";
/* Initialize network resources. */
InitializeSdk();
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
/* Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set. */
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
client.SetRegion(Region);
/* Get the bucket policy configuration. */
GetBucketPolicyRequest request(BucketName);
auto outcome = client.GetBucketPolicy(request);
if (!outcome.isSuccess()) {
/* Handle the exception. */
std::cout << "Get Bucket Policy fail" <<
",code:" << outcome.error().Code() <<
",message:" << outcome.error().Message() <<
",requestId:" << outcome.error().RequestId() << std::endl;
return -1;
}
/* Print the configuration information. */
std::cout << outcome.result().Policy() << std::endl;
/* Release network resources. */
ShutdownSdk();
return 0;
}
Supprimer une stratégie de compartiment
Voici un exemple de code pour supprimer une stratégie de compartiment :
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* Initialize OSS account information. */
/* Set yourEndpoint to the Endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
std::string Endpoint = "yourEndpoint";
/* Set yourRegion to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the Region to cn-hangzhou. */
std::string Region = "yourRegion";
/* Specify the bucket name. For example, examplebucket. */
std::string BucketName = "examplebucket";
/* Initialize network resources. */
InitializeSdk();
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
/* Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set. */
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
client.SetRegion(Region);
/* Delete the bucket policy. */
DeleteBucketPolicyRequest request(BucketName);
auto outcome = client.DeleteBucketPolicy(request);
if (!outcome.isSuccess()) {
/* Handle the exception. */
std::cout << "Delete Bucket Policy fail" <<
",code:" << outcome.error().Code() <<
",message:" << outcome.error().Message() <<
",requestId:" << outcome.error().RequestId() << std::endl;
}
/* Release network resources. */
ShutdownSdk();
return 0;
}
Références
Pour plus d'informations sur l'opération API permettant de définir une stratégie de compartiment, consultez la rubrique PutBucketPolicy.
Pour plus d'informations sur l'opération API permettant de récupérer une stratégie de compartiment, consultez la rubrique GetBucketPolicy.
Pour plus d'informations sur l'opération API permettant de supprimer une stratégie de compartiment, consultez la rubrique DeleteBucketPolicy.