Scalar retrieval is an OSS feature that lets you query objects based on their metadata. You can use custom conditions to quickly filter and retrieve lists of objects. This topic describes how to perform scalar retrieval using Go SDK V2.
Precautions
The sample code in this topic uses the region ID
cn-hangzhoufor China (Hangzhou) as an example. A public endpoint is used by default. If you want to access OSS from other Alibaba Cloud services in the same region, use an internal endpoint. For more information about the regions and endpoints that OSS supports, see OSS regions and endpoints.The sample code in this topic reads access credentials from environment variables. For more information about how to configure access credentials, see Configure access credentials.
Sample code
Enable the metadata management feature
The following code shows how to enable the metadata management feature for a specified bucket. After you enable this feature, OSS creates a metadata index library for the bucket and creates metadata indexes for all objects in the bucket. After the metadata index library is created, OSS performs near real-time incremental scans of new files in the bucket and creates metadata indexes for these files.
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 // Define a variable to store the region information obtained from the command line.
bucketName string // Define a variable to store the bucket name obtained from the command line.
)
// The init function is executed before the main function to initialize the program.
func init() {
// Set a command-line parameter to specify the region.
flag.StringVar(®ion, "region", "", "The region in which the bucket is located.")
// Set 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 whether the bucket name is provided. If not, the program outputs the default parameters and exits.
if len(bucketName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, bucket name required") // Log the error and terminate the program.
}
// Check whether the region is provided. If not, the program outputs the default parameters and exits.
if len(region) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, region required") // Log the error and terminate the program.
}
// Create a client configuration and use an environment variable as the credential provider.
cfg := oss.LoadDefaultConfig().
WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
WithRegion(region)
client := oss.NewClient(cfg) // Use the configuration to create an OSS client 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 to operate on.
}
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, log the error message and terminate the program.
}
log.Printf("open meta query result:%#v\n", result) // Print the result of enabling metadata management.
}Obtain information about the metadata index library
The following code shows how to retrieve information about the metadata index library for a specified 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 // Define a variable to store the region information obtained from the command line.
bucketName string // Define a variable to store the bucket name obtained from the command line.
)
// The init function is executed before the main function to initialize the program.
func init() {
// Set a command-line parameter to specify the region.
flag.StringVar(®ion, "region", "", "The region in which the bucket is located.")
// Set 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 whether the bucket name is provided. If not, the program outputs the default parameters and exits.
if len(bucketName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, bucket name required") // Log the error and terminate the program.
}
// Check whether the region is provided. If not, the program outputs the default parameters and exits.
if len(region) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, region required") // Log the error and terminate the program.
}
// Create a client configuration and use an environment variable as the credential provider and the specified region.
cfg := oss.LoadDefaultConfig().
WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
WithRegion(region)
client := oss.NewClient(cfg) // Create an OSS client instance.
// Create a GetMetaQueryStatus request to obtain information about the metadata index library of a specific bucket.
request := &oss.GetMetaQueryStatusRequest{
Bucket: oss.Ptr(bucketName), // Specify the name of the bucket to query.
}
result, err := client.GetMetaQueryStatus(context.TODO(), request) // Execute the request to obtain information about the metadata index library of the bucket.
if err != nil {
log.Fatalf("failed to get meta query status %v", err) // If an error occurs, log the error message and terminate the program.
}
log.Printf("get meta query status result:%#v\n", result)
}Query objects that meet specific conditions
The following code shows how to use the scalar retrieval feature to query objects that meet specific conditions and list information about the objects based on specified fields and sorting methods.
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 // Define a variable to store the region information obtained from the command line.
bucketName string // Define a variable to store the bucket name obtained from the command line.
)
// The init function is executed before the main function to initialize the program.
func init() {
// Set a command-line parameter to specify the region. The default value is an empty string.
flag.StringVar(®ion, "region", "", "The region in which the bucket is located.")
// Set a command-line parameter to specify the bucket name. The default value is an empty string.
flag.StringVar(&bucketName, "bucket", "", "The name of the bucket.")
}
func main() {
flag.Parse() // Parse command-line parameters.
// Check whether the bucket name is provided. If not, the program outputs the default parameters and exits.
if len(bucketName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, bucket name required")
}
// Check whether the region is provided. If not, the program outputs the default parameters and exits.
if len(region) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, region required")
}
// Create a client configuration and use an environment variable as the credential provider and the specified region.
cfg := oss.LoadDefaultConfig().
WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
WithRegion(region)
client := oss.NewClient(cfg) // Use the configuration to create an OSS client instance.
// Create a DoMetaQuery request to query objects that meet specific conditions.
request := &oss.DoMetaQueryRequest{
Bucket: oss.Ptr(bucketName), // Specify the name of the bucket to query.
MetaQuery: &oss.MetaQuery{
Query: oss.Ptr(`{"Field": "Size","Value": "1048576","Operation": "gt"}`), // Query condition: objects that are larger than 1 MB.
Sort: oss.Ptr("Size"), // Sort field: sort by object size.
Order: oss.Ptr(oss.MetaQueryOrderAsc), // Sort order: ascending.
},
}
result, err := client.DoMetaQuery(context.TODO(), request) // Send the request to perform a metadata query.
if err != nil {
log.Fatalf("failed to do meta query %v", err)
}
// Print NextToken, which is used for paged queries to retrieve data from the next page.
fmt.Printf("NextToken:%s\n", *result.NextToken)
// Traverse the returned results and print the details of each file.
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)
}
// Print the tag information of the object.
for _, tagging := range file.OSSTagging {
fmt.Printf("Oss Tagging Key:%s\n", *tagging.Key)
fmt.Printf("Oss Tagging Value:%s\n", *tagging.Value)
}
// Print the user-defined 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)
}
}
}
Disable the metadata management feature
The following code shows how to disable the metadata management feature for a specified 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 // Define a variable to store the region information obtained from the command line.
bucketName string // Define a variable to store the bucket name obtained from the command line.
)
// The init function is executed before the main function to initialize the program.
func init() {
// Set a command-line parameter to specify the region.
flag.StringVar(®ion, "region", "", "The region in which the bucket is located.")
// Set 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 whether the bucket name is provided. If not, the program outputs the default parameters and exits.
if len(bucketName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, bucket name required") // Log the error and terminate the program.
}
// Check whether the region is provided. If not, the program outputs the default parameters and exits.
if len(region) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, region required") // Log the error and terminate the program.
}
// Create a client configuration and use an environment variable as the credential provider and the specified region.
cfg := oss.LoadDefaultConfig().
WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
WithRegion(region)
client := oss.NewClient(cfg) // Create an OSS client instance.
// Create a CloseMetaQuery request to disable the metadata management feature for a specific bucket.
request := &oss.CloseMetaQueryRequest{
Bucket: oss.Ptr(bucketName), // Specify the name of the bucket to operate on.
}
result, err := client.CloseMetaQuery(context.TODO(), request) // Execute the request to disable the metadata management feature for the bucket.
if err != nil {
log.Fatalf("failed to close meta query %v", err) // If an error occurs, log the error message and terminate the program.
}
log.Printf("close meta query result:%#v\n", result)
}References
For more information about scalar retrieval, see Scalar retrieval.
For more information about the API operations related to data indexing, see Data indexing.
For the complete sample code for scalar retrieval, see GitHub example.