Puede eliminar las etiquetas innecesarias de un objeto según sus necesidades. Si el versionado está habilitado en el bucket que almacena el objeto cuyas etiquetas desea eliminar, Object Storage Service (OSS) elimina de forma predeterminada las etiquetas de la versión actual del objeto. Para eliminar las etiquetas de una versión específica, especifique el identificador de versión del objeto.
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 eliminar las etiquetas de un objeto, es obligatorio contar con el permiso
oss:DeleteObjectTagging. Para obtener más información, consulte Grant a custom policy.
Eliminar las etiquetas de un objeto
Si el versionado no está habilitado en el bucket que almacena el objeto cuyas etiquetas desea eliminar, puede borrar dichas etiquetas según sus necesidades. Cuando el versionado sí está habilitado, OSS elimina de forma predeterminada las etiquetas de la versión actual del objeto.
A continuación se muestra un ejemplo de código para eliminar 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 the 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'
# Delete the tags of the object.
result = bucket.delete_object_tagging(object_name)
print('http response status: ', result.status)
Eliminar las etiquetas de una versión específica de un objeto
Si el versionado está habilitado en el bucket que almacena el objeto cuyas etiquetas desea eliminar, puede borrar las etiquetas de una versión específica al indicar su identificador de versión.
A continuación se muestra un ejemplo de código para eliminar 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 the 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.
version_id = 'CAEQMxiBgICAof2D0BYiIDJhMGE3N2M1YTI1NDQzOGY5NTkyNTI3MGYyMzJm****'
params = dict()
params['versionId'] = version_id
bucket.delete_object_tagging(object_name, params=params)
Referencias
Para consultar el código de ejemplo completo sobre cómo eliminar etiquetas de objetos, visite Ejemplo de etiquetado de objetos del SDK de Python para Alibaba Cloud OSS.
Para conocer la operación de API correspondiente a la eliminación de etiquetas de objetos, consulte DeleteObjectTagging.