Todos os produtos
Search
Central de documentação

Object Storage Service:Get object tags (C++ SDK)

Última atualização: Jul 03, 2026

Este tópico descreve como recuperar as tags de um objeto.

Observações

  • Este tópico utiliza 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 obter mais informações sobre regiões e endpoints do OSS, consulte Regiões e endpoints.

  • O exemplo a seguir demonstra como criar uma instância OssClient com um endpoint do OSS. Para configurações alternativas, como uso de domínio personalizado ou autenticação via Security Token Service (STS), consulte Criar uma instância OssClient.

  • Para consultar as tags de um objeto, você precisa da permissão oss:GetObjectTagging. Para obter mais informações, consulte Conceder uma política personalizada.

Código de exemplo

O código a seguir mostra como recuperar as informações de tags de um 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 obter o código de exemplo completo sobre recuperação de tags de objeto, consulte o exemplo no GitHub.

  • Para obter mais informações sobre a operação de API para recuperação de tags de objeto, consulte GetObjectTagging.