O MetaSearch permite consultas eficientes com base nos metadados dos objetos e a definição de critérios personalizados para filtrar e recuperar listas de objetos. Este tópico descreve como usar o MetaSearch com o OSS SDK for Go 2.0.
Observações
O código de exemplo deste tópico usa o ID da região
cn-hangzhou, referente à região China (Hangzhou). Por padrão, o acesso a recursos em um bucket usa o endpoint público. Para acessar recursos no bucket usando 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 compatíveis com o Object Storage Service (OSS), consulte OSS regions and endpoints.Neste tópico, o código obtém credenciais de acesso de variáveis de ambiente. Para mais informações sobre como configurar credenciais de acesso, consulte Configure access credentials.
Código de exemplo
Ativar o recurso de gerenciamento de metadados
O código a seguir mostra como ativar o recurso de gerenciamento de metadados de um bucket específico. Após a ativação, o OSS cria uma biblioteca de índices de metadados para o bucket e gera índices para todos os objetos armazenados. Com a biblioteca criada, o OSS varre os objetos incrementais do bucket quase em tempo real e cria índices de metadados para eles.
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"
)
var (
region string // Specify a variable to store the region information obtained from the command lines.
bucketName string // Specify a variable to store the bucket name obtained from the command lines.
)
// The init function is executed before the main function to initialize the program.
func init() {
// Use a command line parameter to specify the region.
flag.StringVar(®ion, "region", "", "The region in which the bucket is located.")
// Use a command line parameter to specify the bucket name.
flag.StringVar(&bucketName, "bucket", "", "The name of the bucket.")
}
func main() {
flag.Parse() // Parse command line parameters.
// Check if the bucket name is specified. If not, the program outputs default parameters and terminates.
if len(bucketName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, bucket name required") // Log the error message and terminate the program.
}
// Check whether the region is specified. If the region is not specified, output the default parameters and exit the program.
if len(region) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, region required") // Log the error message and terminate the program.
}
// Create and configure a client and use environment variables to pass the credential provider.
cfg := oss.LoadDefaultConfig().
WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
WithRegion(region)
client := oss.NewClient(cfg) // Use the client configurations to create a new OSSClient instance.
// Create an OpenMetaQuery request to enable the metadata management feature for a specific bucket.
request := &oss.OpenMetaQueryRequest{
Bucket: oss.Ptr(bucketName), // Specify the name of the bucket.
}
result, err := client.OpenMetaQuery(context.TODO(), request) // Execute the request to enable the metadata management feature for the bucket.
if err != nil {
log.Fatalf("failed to open meta query %v", err) // If an error occurs, record the error message and terminate the program.
}
log.Printf("open meta query result:%#v\n", result) // Display the result.
}
Consultar informações sobre a biblioteca de índices de metadados de um bucket
O código a seguir mostra como consultar as informações da biblioteca de índices de metadados de 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"
)
var (
region string // Specify a variable to store the region information obtained from the command lines.
bucketName string // Specify a variable to store the bucket name obtained from the command lines.
)
// The init function is executed before the main function to initialize the program.
func init() {
// Use a command line parameter to specify the region.
flag.StringVar(®ion, "region", "", "The region in which the bucket is located.")
// Use a command line parameter to specify the bucket name.
flag.StringVar(&bucketName, "bucket", "", "The name of the bucket.")
}
func main() {
flag.Parse() // Parse command line parameters.
// Check if the bucket name is specified. If not, the program outputs default parameters and terminates.
if len(bucketName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, bucket name required") // Log the error message and terminate the program.
}
// Check if the region is specified. If not, the program outputs default parameters and terminates.
if len(region) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, region required") // Log the error message and terminate the program.
}
// Create and configure a client and use environment variables to pass the credential provider and the region.
cfg := oss.LoadDefaultConfig().
WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
WithRegion(region)
client := oss.NewClient(cfg) // Create a new OSSClient instance.
// Create a GetMetaQueryStatus request to query information about the metadata index library of a specific bucket.
request := &oss.GetMetaQueryStatusRequest{
Bucket: oss.Ptr(bucketName), // Specify the name of the bucket.
}
result, err := client.GetMetaQueryStatus(context.TODO(), request) // Execute the query request.
if err != nil {
log.Fatalf("failed to get meta query status %v", err) // If an error occurs, record the error message and terminate the program.
}
log.Printf("get meta query status result:%#v\n", result)
}
Consultar objetos que atendem a condições específicas
O código a seguir mostra como usar o MetaSearch para consultar objetos que atendem a condições específicas e listar suas informações com base em campos e métodos de classificação definidos.
package main
import (
"context"
"flag"
"fmt"
"log"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)
var (
region string // Specify a variable to store the region information obtained from the command lines.
bucketName string // Specify a variable to store the bucket name obtained from the command lines.
)
// The init function is executed before the main function to initialize the program.
func init() {
// Use a command line parameter to specify the region. By default, the parameter is an empty string.
flag.StringVar(®ion, "region", "", "The region in which the bucket is located.")
// Use a command line parameter to specify the bucket name. By default, the parameter is an empty string.
flag.StringVar(&bucketName, "bucket", "", "The name of the bucket.")
}
func main() {
flag.Parse() // Parse command line parameters.
// Check if the bucket name is specified. If not, the program outputs default parameters and terminates.
if len(bucketName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, bucket name required")
}
// Check if the region is specified. If not, the program outputs default parameters and terminates.
if len(region) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, region required")
}
// Create and configure a client and use environment variables to pass the credential provider and the region.
cfg := oss.LoadDefaultConfig().
WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
WithRegion(region)
client := oss.NewClient(cfg) // Use the client configurations to create a new OSSClient instance.
// Create a DoMetaQuery request to query the objects that meet specific conditions.
request := &oss.DoMetaQueryRequest{
Bucket: oss.Ptr(bucketName), // Specify the name of the bucket.
MetaQuery: &oss.MetaQuery{
Query: oss.Ptr(`{"Field": "Size","Value": "1048576","Operation": "gt"}`), // Query objects whose size is larger than 1 MB.
Sort: oss.Ptr("Size"), // List the objects by size.
Order: oss.Ptr(oss.MetaQueryOrderAsc), // Sort the objects in ascending order.
},
}
result, err := client.DoMetaQuery(context.TODO(), request) // Execute the request to query the objects that meet the preceding conditions.
if err != nil {
log.Fatalf("failed to do meta query %v", err)
}
// Display the token used to query data on the next page.
fmt.Printf("NextToken:%s\n", *result.NextToken)
// Traverse the returned results and display the details of each object.
for _, file := range result.Files {
fmt.Printf("File name: %s\n", *file.Filename)
fmt.Printf("size: %d\n", file.Size)
fmt.Printf("File Modified Time:%s\n", *file.FileModifiedTime)
fmt.Printf("Oss Object Type:%s\n", *file.OSSObjectType)
fmt.Printf("Oss Storage Class:%s\n", *file.OSSStorageClass)
fmt.Printf("Object ACL:%s\n", *file.ObjectACL)
fmt.Printf("ETag:%s\n", *file.ETag)
fmt.Printf("Oss CRC64:%s\n", *file.OSSCRC64)
if file.OSSTaggingCount != nil {
fmt.Printf("Oss Tagging Count:%d\n", *file.OSSTaggingCount)
}
// Display the tags of the objects.
for _, tagging := range file.OSSTagging {
fmt.Printf("Oss Tagging Key:%s\n", *tagging.Key)
fmt.Printf("Oss Tagging Value:%s\n", *tagging.Value)
}
// Display the user metadata.
for _, userMeta := range file.OSSUserMeta {
fmt.Printf("Oss User Meta Key:%s\n", *userMeta.Key)
fmt.Printf("Oss User Meta Key Value:%s\n", *userMeta.Value)
}
}
}
Desativar o recurso de gerenciamento de metadados
O código a seguir mostra como desativar o recurso de gerenciamento de metadados de um bucket específico.
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"
)
var (
region string // Specify a variable to store the region information obtained from the command lines.
bucketName string // Specify a variable to store the bucket name obtained from the command lines.
)
// The init function is executed before the main function to initialize the program.
func init() {
// Use a command line parameter to specify the region.
flag.StringVar(®ion, "region", "", "The region in which the bucket is located.")
// Use a command line parameter to specify the bucket name.
flag.StringVar(&bucketName, "bucket", "", "The name of the bucket.")
}
func main() {
flag.Parse() // Parse command line parameters.
// Check if the bucket name is specified. If not, the program outputs default parameters and terminates.
if len(bucketName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, bucket name required") // Log the error message and terminate the program.
}
// Check if the region is specified. If not, the program outputs default parameters and terminates.
if len(region) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, region required") // Log the error message and terminate the program.
}
// Create and configure a client and use environment variables to pass the credential provider and the region.
cfg := oss.LoadDefaultConfig().
WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
WithRegion(region)
client := oss.NewClient(cfg) // Create a new OSSClient instance.
// Create a CloseMetaQuery request to disable the metadata management feature for the bucket.
request := &oss.CloseMetaQueryRequest{
Bucket: oss.Ptr(bucketName), // Specify the name of the bucket.
}
result, err := client.CloseMetaQuery(context.TODO(), request) // Execute the request.
if err != nil {
log.Fatalf("failed to close meta query %v", err) // If an error occurs, record the error message and exit the program.
}
log.Printf("close meta query result:%#v\n", result)
}
Referências
Para mais informações sobre o guia do usuário do MetaSearch, consulte MetaSearch.
Sobre as operações de API relacionadas à indexação de dados, consulte Data indexing.
Para acessar o código de exemplo completo do MetaSearch, consulte GitHub.