Después de configurar las etiquetas de un objeto, puede consultarlas. Si el control de versiones está habilitado en el bucket que almacena el objeto cuyas etiquetas desea consultar, Object Storage Service (OSS) devuelve de forma predeterminada las etiquetas de la versión actual. Para consultar las etiquetas de una versión específica del objeto, especifique el identificador de versión correspondiente.
Notas
En este tema se utiliza el endpoint público de la región China (Hangzhou). Si necesita acceder a OSS desde otros servicios de Alibaba Cloud ubicados en la misma región, utilice un endpoint interno. Para obtener más información sobre las regiones y los endpoints de OSS, consulte Regions and endpoints.
Este tema muestra cómo crear una instancia de OSSClient con un endpoint de OSS. Para conocer configuraciones alternativas, como usar un dominio personalizado o autenticarse con credenciales de Security Token Service (STS), consulte Initialization.
Para consultar las etiquetas de un objeto, es obligatorio contar con el permiso
oss:GetObjectTagging. Para obtener más información, consulte Grant a custom policy.
Consultar las etiquetas de un objeto
Si el control de versiones no está habilitado en el bucket que almacena el objeto cuyas etiquetas desea consultar, puede hacerlo según sus necesidades. Cuando el control de versiones sí está habilitado, OSS devuelve de forma predeterminada las etiquetas de la versión actual del objeto.
A continuación se muestra un ejemplo de código para consultar las etiquetas del objeto exampleobject.txt, ubicado en el directorio exampledir del 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 las etiquetas de una versión específica de un objeto
Si el control de versiones está habilitado en el bucket que almacena el objeto cuyas etiquetas desea consultar, puede consultar las de una versión específica al indicar su identificador de versión.
El siguiente ejemplo de código muestra cómo consultar las etiquetas de una versión específica del objeto exampleobject.txt, ubicado en el directorio exampledir del bucket examplebucket:
Para obtener más información sobre cómo recuperar los identificadores de versión, 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)
Referencias
Para consultar el código de ejemplo completo que se utiliza para consultar las etiquetas de un objeto, visite GitHub.
Para obtener más información sobre la operación de API que permite consultar las etiquetas de un objeto, consulte GetObjectTagging.