Vous pouvez utiliser des tags pour identifier les buckets selon différents objectifs et les gérer par catégorie.
Notes
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 Régions et points de terminaison.
Cette rubrique illustre la création d'une instance OSSClient avec un point de terminaison OSS. Pour d'autres configurations, telles que l'utilisation d'un domaine personnalisé ou l'authentification via des identifiants du Security Token Service (STS), consultez Créer une instance OssClient.
Seul le propriétaire du bucket et les utilisateurs disposant de l'autorisation
oss:PutBucketTaggingpeuvent définir des tags pour un bucket. Dans le cas contraire, une erreur 403 Forbidden est renvoyée avec le code d'erreur AccessDenied.
Définir des tags de bucket
Le code suivant montre comment configurer des tags pour un bucket nommé examplebucket :
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* Initialize OSS account information. */
/* Set yourEndpoint to the Endpoint of the bucket's region. For example, for 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, for 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);
/* Set bucket tags. */
SetBucketTaggingRequest request(BucketName);
Tag tag1("yourTagkey1","yourTagValue1");
Tag tag2("yourTagkey2", "yourTagValue2");
TagSet tagset;
tagset.push_back(tag1);
tagset.push_back(tag2);
Tagging taging;
taging.setTags(tagset);
request.setTagging(taging);
auto outcome = client.SetBucketTagging(request);
if (outcome.isSuccess()) {
std::cout << " SetBucketTagging success " << std::endl;
}
else {
/* Handle exceptions. */
std::cout << "SetBucketTagging fail" <<
",code:" << outcome.error().Code() <<
",message:" << outcome.error().Message() <<
",requestId:" << outcome.error().RequestId() << std::endl;
return -1;
}
/* Release network resources. */
ShutdownSdk();
return 0;
}
Obtenir les tags d'un bucket
Le code suivant montre comment interroger les tags d'un bucket nommé examplebucket :
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* Initialize OSS account information. */
/* Set yourEndpoint to the Endpoint of the bucket's region. For example, for 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, for 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 bucket tags. */
GetBucketTaggingRequest request(BucketName);
auto outcome = client.GetBucketTagging(request);
if (outcome.isSuccess()) {
std::cout << " GetBucketTagging success " << std::endl;
for (const auto& tag : outcome.result().Tagging().Tags()) {
std::cout << "tag key:" << tag.Key() <<
"tag value:" << tag.Value() << std::endl;
}
}
else {
/* Handle exceptions. */
std::cout << "GetBucketTagging fail" <<
",code:" << outcome.error().Code() <<
",message:" << outcome.error().Message() <<
",requestId:" << outcome.error().RequestId() << std::endl;
return -1;
}
/* Release network resources. */
ShutdownSdk();
return 0;
}
Répertorier les buckets avec un tag spécifique
Le code suivant montre comment répertorier les buckets associés à un tag spécifique :
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* Initialize OSS account information. */
/* Set yourEndpoint to the Endpoint of the bucket's region. For example, for 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, for the China (Hangzhou) region, set the region to cn-hangzhou. */
std::string Region = "yourRegion";
/* 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);
/* List buckets that have a specified tag. */
ListBucketsRequest request;
Tag tag1("yourTagkey1","yourTagValue1");
request.setTag(tag1);
auto outcome = client.ListBuckets(request);
if (outcome.isSuccess()) {
std::cout << "ListBuckets success" << std::endl;
for (const auto& bucket : outcome.result().Buckets()) {
std::cout << "bucket name:" << bucket.Name() << std::endl;
}
}
else {
/* Handle exceptions. */
std::cout << "ListBuckets fail" <<
",code:" << outcome.error().Code() <<
",message:" << outcome.error().Message() <<
",requestId:" << outcome.error().RequestId() << std::endl;
return -1;
}
/* Release network resources. */
ShutdownSdk();
return 0;
}
Supprimer tous les tags d'un bucket
Le code suivant montre comment supprimer tous les tags d'un bucket :
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* Initialize OSS account information. */
/* Set yourEndpoint to the Endpoint of the bucket's region. For example, for 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, for 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 bucket tags. */
DeleteBucketTaggingRequest request(BucketName);
auto outcome = client.DeleteBucketTagging(request);
if (outcome.isSuccess()) {
std::cout << " DeleteBucketTagging success " << std::endl;
}
else {
/* Handle exceptions. */
std::cout << "DeleteBucketTagging fail" <<
",code:" << outcome.error().Code() <<
",message:" << outcome.error().Message() <<
",requestId:" << outcome.error().RequestId() << std::endl;
return -1;
}
/* Release network resources. */
ShutdownSdk();
return 0;
}
Supprimer des tags spécifiques d'un bucket
Le code suivant montre comment supprimer des tags spécifiques d'un bucket :
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* Initialize OSS account information. */
/* Set yourEndpoint to the Endpoint of the bucket's region. For example, for 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, for 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 bucket tags based on specified keys. */
DeleteBucketTaggingRequest request(BucketName);
Tagging tagging;
TagSet tagset;
Tag tag("project","projectone");
tagset.push_back(tag);
tagging.setTags(tagset);
request.setTagging(tagging);
auto outcome = client.DeleteBucketTagging(request);
if (outcome.isSuccess()) {
std::cout << " DeleteBucketTagging success " << std::endl;
}
else {
/* Handle exceptions. */
std::cout << "DeleteBucketTagging fail" <<
",code:" << outcome.error().Code() <<
",message:" << outcome.error().Message() <<
",requestId:" << outcome.error().RequestId() << std::endl;
return -1;
}
/* Release network resources. */
ShutdownSdk();
return 0;
}
Références
Pour plus d'informations sur l'opération API permettant de définir des tags de bucket, consultez PutBucketTags.
Pour plus d'informations sur l'opération API permettant d'obtenir les tags d'un bucket, consultez GetBucketTags.
Pour plus d'informations sur l'opération API permettant de supprimer les tags d'un bucket, consultez DeleteBucketTags.