Object Storage Service (OSS) permite configurar etiquetas con fines de clasificación. Este tema describe cómo configurar etiquetas para objetos mediante OSS SDK for Python 2.0.
Notas de uso
El código de ejemplo de este tema utiliza el ID de región
cn-hangzhoucorrespondiente a la región China (Hangzhou). De forma predeterminada, se usa un endpoint público para acceder a los recursos de un bucket. Si desea acceder a los recursos del bucket desde otros servicios de Alibaba Cloud en la misma región, utilice un endpoint interno. Para obtener más información sobre las regiones y endpoints compatibles, consulte Regions and endpoints.El permiso
oss:PutObjectTagginges obligatorio para configurar etiquetas. Para obtener más información, consulte Grant a custom policy.
Sintaxis
put_object_tagging(request: PutObjectTaggingRequest, **kwargs) → PutObjectTaggingResult
Parámetros de solicitud
|
Parámetro |
Tipo |
Descripción |
|
request |
PutObjectTaggingRequest |
Solicitud para la operación PutObjectTagging. Para consultar los parámetros de la solicitud, véase PutObjectTaggingRequest. |
Parámetros de respuesta
|
Tipo |
Descripción |
|
PutObjectTaggingResult |
Valor devuelto. Para más detalles, consulte PutObjectTaggingResult. |
Consulte put_object_tagging para ver la definición completa sobre cómo configurar etiquetas.
Ejemplo
A continuación se muestra el código de ejemplo para configurar las etiquetas de un objeto específico en un bucket:
import argparse
import alibabacloud_oss_v2 as oss
# Create a command-line parameter parser and define the parameters.
parser = argparse.ArgumentParser(description="put object tagging sample")
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
parser.add_argument('--key', help='The name of the object.', required=True)
parser.add_argument('--tag_key', help='The name of the tag key.', required=True)
parser.add_argument('--tag_value', help='The name of the tag value.', required=True)
def main():
# Parse the command-line parameters.
args = parser.parse_args()
# Load access credentials from environment variables.
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
# Use the default configuration of the SDK.
cfg = oss.config.load_default()
# Specify the credential provider.
cfg.credentials_provider = credentials_provider
# Specify the region.
cfg.region = args.region
# Specify the endpoint if one is provided.
if args.endpoint is not None:
cfg.endpoint = args.endpoint
# Create an OSS client.
client = oss.Client(cfg)
# Specify a tag.
# Add more oss.Tag objects in the following format if you need to configure multiple tags.
# tags = [oss.Tag(key=args.tag_key, value=args.tag_value), oss.Tag(key=args.tag_key2, value=args.tag_value2)]
tags = [oss.Tag(
key=args.tag_key,
value=args.tag_value,
)]
# Configure the tags for the object.
result = client.put_object_tagging(oss.PutObjectTaggingRequest(
bucket=args.bucket,
key=args.key,
tagging=oss.Tagging(
tag_set=oss.TagSet(
tags=tags,
),
),
))
# Print the response.
print(f'status code: {result.status_code},'
f' request id: {result.request_id},'
f' version id: {result.version_id},'
)
if __name__ == "__main__":
main()
Referencia
Para consultar el código de ejemplo completo para configurar las etiquetas de un objeto, visite put_object_tagging.py.