Use tags para marcar buckets com diferentes finalidades e gerencie por categoria.
Observações
Este tópico usa o endpoint público da região China (Hangzhou). Para acessar o oss a partir de outros serviços da Alibaba Cloud na mesma região, use um endpoint interno. Para mais informações sobre regiões e endpoints do oss, consulte Regions and endpoints.
Este tópico demonstra como crie uma instância do OSSClient com um endpoint do oss. Para configurações alternativas, como usar um domínio personalizado ou autenticar com credenciais do Security Token service (STS), consulte Create an OssClient instance.
Somente o proprietário do bucket e os usuários com a permissão
oss:PutBucketTaggingpodem definir tags para um bucket. Caso contrário, a solicitação retorna um erro 403 Forbidden com o código de erro AccessDenied.Cada bucket pode ter até 20 tags (pares de chave-valor).
As chaves e os valores das tags devem estar codificados em UTF-8.
A chave de uma tag pode ter até 64 caracteres, faz distinção entre maiúsculas e minúsculas e não pode ser vazia. A chave não pode começar com
http://,https://ouAliyun(sem distinção entre maiúsculas e minúsculas).O valor de uma tag pode ter até 128 caracteres e pode ser uma string vazia.
defina tags de bucket
Configure as tags de um bucket chamado examplebucket conforme o exemplo de código a seguir:
#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;
}
Obtenha tags de bucket
execute o código para obter as tags de um bucket chamado examplebucket conforme o exemplo a seguir:
#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;
}
Liste buckets com uma tag específica
especifique a tag e liste os buckets conforme o exemplo de código a seguir:
#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;
}
exclua todas as tags de um bucket
Exclua todas as tags de um bucket conforme o exemplo de código a seguir:
#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;
}
exclua tags específicas de um bucket
Exclua tags específicas de um bucket conforme o exemplo de código a seguir:
#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;
}
Referências
Para mais informações sobre a operação de API para definir tags de bucket, consulte PutBucketTags.
Para obter detalhes sobre a operação de API para consultar tags de bucket, consulte GetBucketTags.
Para saber mais sobre a operação de API para excluir tags de bucket, consulte DeleteBucketTags.