Este tópico descreve como consultar as tags de um objeto usando o Go SDK V2.
Observações de uso
O código de exemplo neste tópico utiliza o ID de região
cn-hangzhoucorrespondente à região China (Hangzhou). Por padrão, um endpoint público é usado para acessar recursos em um bucket. Para acessar recursos do bucket a partir de outros serviços Alibaba Cloud na mesma região, utilize um endpoint interno. Para obter mais informações sobre as regiões e endpoints compatíveis com o Object Storage Service (OSS), consulte OSS regions and endpoints.Neste tópico, as credenciais de acesso são obtidas de variáveis de ambiente. Para obter mais informações sobre como configurar credenciais de acesso, consulte Configure access credentials.
Para consultar as tags de um objeto, você precisa ter a permissão
oss:GetObjectTagging. Para obter mais informações, consulte Grant custom policy to RAM users.
O tagging de objetos utiliza pares de chave-valor para identificar objetos. Para obter mais informações sobre tagging de objetos, consulte Object tagging no OSS Developer Guide.
Para obter mais informações sobre como consultar as tags de um objeto, consulte GetObjectTagging.
Código de exemplo
O código a seguir demonstra como consultar as tags de um objeto em um bucket.
package main
import (
"context"
"flag"
"log"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)
// Define global variables.
var (
region string // The region in which the bucket is located.
bucketName string // The name of the bucket.
objectName string // The name of the object.
)
// Specify the init function used to initialize command line parameters.
func init() {
flag.StringVar(®ion, "region", "", "The region in which the bucket is located.")
flag.StringVar(&bucketName, "bucket", "", "The name of the bucket.")
flag.StringVar(&objectName, "object", "", "The name of the object.")
}
func main() {
// Parse command line parameters.
flag.Parse()
// Check whether the region is specified.
if len(region) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, region required")
}
// Check whether the name of the bucket is specified.
if len(bucketName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, bucket name required")
}
// Check whether the name of the object is specified.
if len(objectName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, object name required")
}
// Load the default configurations and specify the credential provider and region.
cfg := oss.LoadDefaultConfig().
WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
WithRegion(region)
// Create an OSS client.
client := oss.NewClient(cfg)
// Create a request to query the tags of the object.
getRequest := &oss.GetObjectTaggingRequest{
Bucket: oss.Ptr(bucketName), // The name of the bucket.
Key: oss.Ptr(objectName), // The name of the object.
}
// Make the query request and process the results.
getResult, err := client.GetObjectTagging(context.TODO(), getRequest)
if err != nil {
log.Fatalf("failed to get object tagging %v", err)
}
// Display the number of tags.
log.Printf("get object tagging result:%#v\n", len(getResult.Tags))
}
Referências
Para obter o código de exemplo completo usado para consultar as tags de um objeto, visite o GitHub.
Para obter mais informações sobre a operação de API usada para consultar tags de objetos, consulte GetObjectTagging.