Este tópico descreve como obter as tags de objeto.
Observações
Os exemplos deste tópico usam 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.
O exemplo demonstra como criar 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.
Para consultar as tags de um objeto, é necessário ter a permissão
oss:GetObjectTagging. Para mais informações, consulte Grant a custom policy.
Código de exemplo
O código a seguir mostra como obter as informações de tags de objeto:
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
#include <iostream>
#include <sstream>
int main(void)
{
/* Initialize OSS account information. */
/* Set yourEndpoint to the endpoint of the region where the bucket is located. 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";
/* Specify the full path of the object. The full path cannot include the bucket name. For example, exampledir/exampleobject.txt. */
std::string ObjectName = "exampledir/exampleobject.txt";
/* Initialize network resources. */
InitializeSdk();
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
/* Obtain access credentials from environment variables. Before running 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);
std::shared_ptr<std::iostream> content = std::make_shared<std::stringstream>();
*content << "test cpp sdk";
PutObjectRequest request(BucketName, ObjectName, content);
/* Upload the file. */
auto outcome = client.PutObject(request);
/* Set object tags. */
Tagging tagging;
tagging.addTag(Tag("key1", "value1"));
tagging.addTag(Tag("key2", "value2"));
auto putTaggingOutcome = client.SetObjectTagging(SetObjectTaggingRequest(BucketName, ObjectName, tagging));
/* Get object tags. */
auto getTaggingOutcome = client.GetObjectTagging(GetObjectTaggingRequest(BucketName, ObjectName));
if (!getTaggingOutcome.isSuccess()) {
/* Handle exceptions. */
std::cout << "getTaggingOutcome fail" <<
",code:" << getTaggingOutcome.error().Code() <<
",message:" << getTaggingOutcome.error().Message() <<
",requestId:" << getTaggingOutcome.error().RequestId() << std::endl;
return -1;
}
else {
auto taglist = getTaggingOutcome.result().Tagging();
for (const auto& tag : taglist.Tags())
{
std::cout <<"GetObjectTagging success, Key:"
<< tag.Key() << "; Value:" << tag.Value() << std::endl;
}
}
/* Release network resources. */
ShutdownSdk();
return 0;
}
Referências
Para o código de exemplo completo sobre como obter tags de objeto, consulte Exemplo no GitHub.
Para mais informações sobre a operação de API para obter tags de objeto, consulte GetObjectTagging.