O Object Storage Service (OSS) permite configurar tags para classificação. Este tópico descreve como configurar tags de objetos usando o OSS SDK for Python 2.0.
Observações de uso
O código de exemplo deste tópico usa o ID de região
cn-hangzhou, da região China (Hangzhou). Por padrão, o acesso a recursos em um bucket usa um endpoint público. Ao acessar recursos no bucket a partir de outros serviços da Alibaba Cloud na mesma região, use um endpoint interno. Para mais informações sobre as regiões e os endpoints suportados, consulte Regions and endpoints.Configurar tags exige a permissão
oss:PutObjectTagging. Para obter mais informações, consulte Grant a custom policy.
Sintaxe
put_object_tagging(request: PutObjectTaggingRequest, **kwargs) → PutObjectTaggingResult
Parâmetros da requisição
|
Parâmetro |
Tipo |
Descrição |
|
request |
PutObjectTaggingRequest |
A requisição da operação PutObjectTagging. Para detalhes sobre os parâmetros da requisição, consulte PutObjectTaggingRequest. |
Parâmetros de resposta
|
Tipo |
Descrição |
|
PutObjectTaggingResult |
O valor de retorno. Para ver mais detalhes, consulte PutObjectTaggingResult. |
Consulte put_object_tagging para a definição completa de como configurar tags.
Exemplo
Veja a seguir o código de exemplo para configurar as tags de 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
O código de exemplo completo para configurar as tags de um objeto está disponível em put_object_tagging.py.