O Object Storage Service (OSS) permite configurar tags para classificação. Este tópico descreve como configurar tags em objetos com o OSS SDK for Python 2.0.
Observações de uso
Os códigos de exemplo deste tópico usam o ID de região
cn-hangzhou, correspondente à região China (Hangzhou). Por padrão, o acesso aos recursos de um bucket ocorre via endpoint público. Para acessar esses recursos a partir de outros serviços da Alibaba Cloud na mesma região, use um endpoint interno. Para mais informações sobre regiões e endpoints suportados, consulte Regiões e endpoints.A permissão
oss:PutObjectTaggingé obrigatória para configurar tags. Para mais informações, consulte Conceder uma política personalizada.
Sintaxe
put_object_tagging(request: PutObjectTaggingRequest, **kwargs) → PutObjectTaggingResult
Parâmetros da solicitação
|
Parâmetro |
Tipo |
Descrição |
|
request |
PutObjectTaggingRequest |
Solicitação da operação PutObjectTagging. Para consultar os parâmetros desta solicitação, consulte PutObjectTaggingRequest. |
Parâmetros de resposta
|
Tipo |
Descrição |
|
PutObjectTaggingResult |
Valor de retorno. Para mais detalhes, consulte PutObjectTaggingResult. |
Para obter a definição completa sobre como configurar tags, consulte put_object_tagging.
Exemplo
O código abaixo demonstra como configurar tags para um objeto específico em um 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()
Referência
Para obter o código de exemplo completo de configuração de tags de um objeto, visite put_object_tagging.py.