Todos os produtos
Search
Central de documentação

Object Storage Service:Consultar etiquetas de objetos (OSS SDK for Python 1.0)

Última atualização: Sep 22, 2026

Após configurar as etiquetas de um objeto, você pode consultá-las. Se o versionamento estiver ativado para o bucket que armazena o objeto do qual deseja consultar as etiquetas, o Object Storage Service (OSS) retorna, por padrão, as etiquetas da versão atual. Para consultar as etiquetas de uma versão específica do objeto, especifique o respectivo ID de versão.

Notas

  • Este tópico emprega 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, utilize um endpoint interno. Para mais informações sobre regiões e endpoints do OSS, consulte Regions and endpoints.

  • Este tópico demonstra como criar uma instância de OSSClient com um endpoint do OSS. Para configurações alternativas, como o uso de um domínio personalizado ou a autenticação com credenciais do Security Token Service (STS), consulte Initialization.

  • Para consultar as etiquetas de um objeto, você deve ter a permissão oss:GetObjectTagging. Para obter mais informações, consulte Grant a custom policy.

Consultar as etiquetas de um objeto

Se o versionamento não estiver ativado para o bucket que armazena o objeto, você pode consultar as etiquetas conforme necessário. Se o versionamento estiver ativado, o OSS retorna as etiquetas da versão atual do objeto por padrão.

O exemplo de código a seguir mostra como consultar as etiquetas do objeto exampleobject.txt no diretório exampledir do bucket examplebucket:

# -*- coding: utf-8 -*-

import oss2
from oss2.models import Tagging, TaggingRule
from oss2.credentials import EnvironmentVariableCredentialsProvider

# Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())

# Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use the signature algorithm V4.
region = "cn-hangzhou"

# Specify the name of your bucket.
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)

# Specify the full path of the object. Do not include the bucket name in the full path. Example: exampledir/exampleobject.txt. 
object_name = 'exampledir/exampleobject.txt'

# Query the tags of the object. 
result = bucket.get_object_tagging(object_name)

# Display the tags of the object. 
for key in result.tag_set.tagging_rule:
    print('tagging key: {}, value: {}'.format(key, result.tag_set.tagging_rule[key]))

Consultar as etiquetas de uma versão específica de um objeto

Se o versionamento estiver ativado para o bucket que armazena o objeto, você pode consultar as etiquetas de uma versão específica ao especificar o ID de versão correspondente.

O exemplo de código a seguir mostra como consultar as etiquetas de uma versão específica do objeto exampleobject.txt no diretório exampledir do bucket examplebucket:

Nota

Para obter mais informações sobre como recuperar os IDs de versão, consulte List objects (OSS SDK for Python 1.0).

# -*- coding: utf-8 -*-

import oss2
from oss2.models import Tagging
from oss2.credentials import EnvironmentVariableCredentialsProvider

# Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())

# Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use the signature algorithm V4.
region = "cn-hangzhou"

# Specify the name of your bucket.
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)

# Specify the full path of the object. Do not include the bucket name in the full path. Example: exampledir/exampleobject.txt. 
object_name = 'exampledir/exampleobject.txt'
# Specify the version ID of the object. Example: CAEQMxiBgICAof2D0BYiIDJhMGE3N2M1YTI1NDQzOGY5NTkyNTI3MGYyMzJm****. 
version_id = 'CAEQMxiBgICAof2D0BYiIDJhMGE3N2M1YTI1NDQzOGY5NTkyNTI3MGYyMzJm****'

params = dict()
params['versionId'] = version_id

result = bucket.get_object_tagging(object_name, params=params)
print(result)

Referências

  • Para acessar o código de exemplo completo que consulta as etiquetas de um objeto, visite o GitHub.

  • Para obter mais informações sobre a operação de API que consulta as etiquetas de um objeto, consulte GetObjectTagging.