A marcação de objetos utiliza pares de chave-valor para identificá-los. Este tópico descreve como obter as tags de um objeto.
Notas
Este tópico usa o endpoint público da região China (Hangzhou). Se precisar acessar o OSS 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 do OSS, consulte Regions and endpoints.
Você deve ter a permissão
oss:GetObjectTaggingpara consultar as tags de um objeto. Para mais informações, consulte Grant a custom policy.
Código de exemplo
O código de exemplo a seguir demonstra como obter as tags de um objeto.
using OSS = AlibabaCloud.OSS.V2; // Create an alias for the Alibaba Cloud OSS SDK to simplify subsequent use.
var region = "cn-hangzhou"; // Required. Specify the region where the bucket is located. In this example, the China (Hangzhou) region is used. Set region to cn-hangzhou.
var endpoint = null as string; // Optional. Specify the endpoint used to access the OSS service. In this example, the endpoint for the China (Hangzhou) region is used. Set endpoint to https://oss-cn-hangzhou.aliyuncs.com.
var bucket = "your bucket name"; // Required. The name of the destination bucket.
var key = "your object name"; // Required. The name of the destination object. The format is folder/objectName.
// Load the default configurations of the OSS SDK. The configurations automatically read credential information such as the AccessKey from environment variables.
var cfg = OSS.Configuration.LoadDefault();
// Explicitly set the use of environment variables to obtain credentials for identity verification. The format is OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET.
cfg.CredentialsProvider = new OSS.Credentials.EnvironmentVariableCredentialsProvider();
// Set the region in the configuration.
cfg.Region = region;
// If an endpoint is specified, overwrite the default endpoint.
if(endpoint != null)
{
cfg.Endpoint = endpoint;
}
// Create an OSS client instance based on the configuration information.
using var client = new OSS.Client(cfg);
// Call the GetObjectTaggingAsync method to obtain the tag information of the destination object.
var result = await client.GetObjectTaggingAsync(new()
{
Bucket = bucket,
Key = key
});
// Print the upload result.
Console.WriteLine("GetObjectTagging done"); // A message indicating that the operation is complete.
Console.WriteLine($"StatusCode: {result.StatusCode}"); // The HTTP status code.
Console.WriteLine($"RequestId: {result.RequestId}"); // The request ID, which is used for troubleshooting in Alibaba Cloud.
Console.WriteLine("Response Headers:"); // The response header information.
result.Headers.ToList().ForEach(x => Console.WriteLine(x.Key + " : " + x.Value)); // Traverse and print all response headers.
result.Tagging?.TagSet?.Tags?.ForEach(x => Console.WriteLine(x.Key + " : " + x.Value)); // If the tag information of the destination object exists, print the information.
Referências
Consulte GetObjectTagging.cs para acessar o código de exemplo completo.