All Products
Search
Document Center

Object Storage Service:Use MetaSearch to query files in OSS based on metadata properties

Last Updated:Dec 08, 2025

MetaSearch is an Object Storage Service (OSS) feature that indexes object metadata. You can use custom conditions to quickly filter and retrieve lists of objects. This feature helps you better manage and understand your data structures, which simplifies subsequent queries, statistical analysis, and object management.

Scenarios

Data auditing

MetaSearch lets you quickly locate files to meet data auditing or regulatory requirements. For example, in the financial services industry, you can filter files by metadata such as custom tags and access permissions. This helps you locate files with specific sensitivity levels or permissions and improves the efficiency of data audits.

Enterprise data backup and archiving

When you back up and archive enterprise data, you can use MetaSearch to quickly retrieve files from a specific date or of a specific type. You can filter by metadata such as creation time, storage class, or custom tags. This lets you quickly recover historical data or archived records.

Limits

  • Region limits

    The MetaSearch feature is available for buckets in the China (Hangzhou), China (Shanghai), China (Qingdao), China (Beijing), China (Zhangjiakou), China (Shenzhen), China (Guangzhou), China (Chengdu), China (Ulanqab), China (Hong Kong), Singapore, Indonesia (Jakarta), Germany (Frankfurt), US (Virginia), US (Silicon Valley), and UK (London) regions.

  • Bucket limits

    A bucket with MetaSearch enabled can contain a maximum of 50 billion files. If the number of files in a bucket exceeds 50 billion, retrieval performance may degrade. To process larger-scale data, contact Technical Support for an evaluation.

  • Multipart upload

    For objects created using multipart upload, query results display only complete objects that are assembled from parts using the CompleteMultipartUpload operation. Parts that are initialized but not completed or aborted are not included in the results.

Performance reference

The following performance metrics for the OSS MetaSearch mode are for reference only.

  • Reference for existing file index generation time

    • 100 million files in a single bucket: 4 hours

    • 1 billion files in a single bucket: about 10 hours

    • 10 billion files in a single bucket: about 1 to 3 days

    • 20 billion files in a single bucket: about 2 to 4 days

    • 30 billion files in a single bucket: about 3 to 6 days

    • 50 billion files in a single bucket: about 6 to 10 days

    • If your bucket contains more than 1 billion files and the files have tags, index generation takes longer than the times listed above.

  • Reference for incremental file index update time

    By default, OSS provides an additional 5,000 queries per second (QPS) for MetaSearch mode. This means that OSS can process 5,000 file index update requests per second. This QPS does not affect your bucket's Quality of Service (QoS). If the QPS for additions, modifications, or deletions in the bucket is less than the default of 5,000, the latency from the time a file is uploaded or modified to the time the file is retrievable is typically several minutes. If the QPS exceeds the default of 5,000, contact Technical Support. We will evaluate your situation and provide technical assistance.

  • File retrieval response performance

    Retrieval results are returned in seconds. The default timeout period is 30 seconds.

Enable MetaSearch

Use the OSS console

For buckets in the China (Hangzhou), China (Shanghai), China (Qingdao), China (Beijing), China (Zhangjiakou), China (Shenzhen), China (Guangzhou), China (Chengdu), China (Hong Kong), Singapore, Indonesia (Jakarta), and Germany (Frankfurt) regions

  1. Log on to the OSS console.

  2. In the left-side navigation pane, click Buckets. On the Buckets page, find and click the desired bucket.

  3. In the navigation pane on the left, choose Object Management > Data Indexing.

  4. On the Data Indexing page, if you are using the data indexing feature for the first time, follow the prompts to grant permissions to the AliyunMetaQueryDefaultRole role. This allows the OSS service to manage data in your bucket. After you grant the permissions, click Enable Data Indexing.

  5. Select MetaSearch, and then click Enable.

    Note

    Enabling MetaSearch takes some time. The exact duration depends on the number of objects in the bucket.

For buckets in the UK (London), China (Ulanqab), US (Virginia), and US (Silicon Valley) regions

  1. Log on to the OSS console.

  2. In the left-side navigation pane, click Buckets. On the Buckets page, find and click the desired bucket.

  3. In the navigation pane on the left, choose Object Management > Data Indexing. If you are using the data indexing feature for the first time, follow the prompts to grant permissions to the AliyunMetaQueryDefaultRole role. This allows the OSS service to manage data in your bucket.

  4. Turn on Metadata Management.

    Note

    Enabling metadata management takes some time. The exact duration depends on the number of objects in the bucket.

Use an Alibaba Cloud SDK

Before you use the MetaSearch feature, you must enable the Metadata Management feature for the specified bucket. The following code provides an example.

Java

import com.aliyun.oss.*;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.model.MetaQueryMode;

public class OpenMetaQuery {
    public static void main(String[] args) throws com.aliyuncs.exceptions.ClientException {
        // In this example, the endpoint of the China (Hangzhou) region is used.
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        // Specify the name of the bucket. Example: examplebucket.
        String bucketName = "examplebucket";
        // Obtain access credentials from environment variables. 
        // Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
        EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
        // Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou.
        String region = "cn-hangzhou";

        // Create an OSSClient instance.
        //Call the shutdown method to release associated resources when the OSSClient instance is no longer used.
        ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
        clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
        OSS ossClient = OSSClientBuilder.create()
                .endpoint(endpoint)
                .credentialsProvider(credentialsProvider)
                .clientConfiguration(clientBuilderConfiguration)
                .region(region)
                .build();

        try {
            // Enable the MetaSearch feature.
            ossClient.openMetaQuery(bucketName);
        } catch (OSSException oe) {
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("Error Message: " + ce.getMessage());
        } finally {
            // Shut down the OSSClient instance.
            if(ossClient != null){
                ossClient.shutdown();
            }
        }
    }
}

Python

import argparse
import alibabacloud_oss_v2 as oss

# Create a command-line parameter parser and describe the purpose of the script.
parser = argparse.ArgumentParser(description="open meta query sample")
# Specify the --region parameter to indicate the region in which the bucket is located. This parameter is required.
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
# Specify the --bucket parameter to indicate the bucket. This parameter is required.
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
# Specify the --endpoint parameter to indicate the endpoint for accessing OSS. This parameter is required.
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')

def main():
    # Parse the command-line arguments.
    args = parser.parse_args()

    # From the environment variables, load the authentication information required to access OSS.
    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
    # Set the region to the one provided from the command line.
    cfg.region = args.region
    # If a custom endpoint is provided, update the endpoint attribute of the cfg object with the provided endpoint.
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    # Create an OSS client.
    client = oss.Client(cfg)

    # Initiate a request to enable metadata-based query.
    result = client.open_meta_query(oss.OpenMetaQueryRequest(
            bucket=args.bucket,
    ))

    # Display the HTTP status code and request ID.
    print(f'status code: {result.status_code},'
          f' request id: {result.request_id},'
          )

# Call the main function when the script is directly run.
if __name__ == "__main__":
    main()

Go

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(&region, "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.
}

PHP

Use ossutil

The following example shows how to enable the metadata management feature for the bucket named examplebucket.

ossutil api open-meta-query --bucket examplebucket

For more information about this command, see open-meta-query.

Initiate a MetaSearch query

Use the OSS console

The following example shows how to find all files that are smaller than 500 KB and were last updated between 00:00 on September 11, 2024, and 00:00 on September 12, 2024. The output is sorted by file size in ascending order, and the maximum file size is calculated.

  1. Log on to the OSS console.

  2. In the left-side navigation pane, click Buckets. On the Buckets page, find and click the desired bucket.

  3. In the navigation pane on the left, choose Object Management > Data Indexing.

  4. Configure the following parameters and keep the default settings for the other parameters.

    • Set Last Modified At to the range from 00:00 on September 11, 2024, to 00:00 on September 12, 2024.

    • Set Object Size to less than 500 KB.

  5. Click Search Result Settings.

    • For Object Sort Order, select Ascending and Object Size.

    • For Data Aggregation, select Object Size and Maximum.

    image

  6. Click Query Now. Two files meet the query conditions. As shown in the following figure, the maximum file size is 434 KB.

    image

    For more information about all query conditions and output settings, see Query conditions and output settings.

Use an Alibaba Cloud SDK

The following code shows how to use the MetaSearch feature to query objects that meet specified conditions.

Java

For more code examples, see MetaSearch using OSS SDK for Java.

import com.aliyun.oss.*;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.model.*;
import java.util.ArrayList;
import java.util.List;

public class DoMetaQuery {
    public static void main(String[] args) throws Exception {
        // In this example, the endpoint of the China (Hangzhou) region is used.
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        // Specify the name of the bucket. Example: examplebucket.
        String bucketName = "examplebucket";
        // Obtain access credentials from environment variables. 
        // Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
        EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
        // Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou.
        String region = "cn-hangzhou";

        // Create an OSSClient instance.
        // Call the shutdown method to release associated resources when the OSSClient instance is no longer used.
        ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
        clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
        OSS ossClient = OSSClientBuilder.create()
                .endpoint(endpoint)
                .credentialsProvider(credentialsProvider)
                .clientConfiguration(clientBuilderConfiguration)
                .region(region)
                .build();

        try {
            // Query objects that meet specific conditions and return objects based on specific fields and in the specified sorting order.
            int maxResults = 20;
            // Query objects that are smaller than 1,048,576 bytes in size, return up to 20 objects at a time, and sort the objects in ascending order.
            String query = "{\"Field\": \"Size\",\"Value\": \"1048576\",\"Operation\": \"lt\"}";
            String sort = "Size";
            DoMetaQueryRequest doMetaQueryRequest = new DoMetaQueryRequest(bucketName, maxResults, query, sort);
            Aggregation aggregationRequest = new Aggregation();
            Aggregations aggregations = new Aggregations();
            List<Aggregation> aggregationList = new ArrayList<Aggregation>();
            // Specify the name of the field that is used in the aggregate operation.
            aggregationRequest.setField("Size");
            // Specify the operator that is used in the aggregate operation. max indicates the maximum value.
            aggregationRequest.setOperation("max");
            aggregationList.add(aggregationRequest);
            aggregations.setAggregation(aggregationList);

            // Specify the aggregate operation.
            doMetaQueryRequest.setAggregations(aggregations);
            doMetaQueryRequest.setOrder(SortOrder.ASC);
            DoMetaQueryResult doMetaQueryResult = ossClient.doMetaQuery(doMetaQueryRequest);
            if(doMetaQueryResult.getFiles() != null){
                for(ObjectFile file : doMetaQueryResult.getFiles().getFile()){
                    System.out.println("Filename: " + file.getFilename());
                    // Query the ETag value that is used to identify the content of the object.
                    System.out.println("ETag: " + file.getETag());
                    // Query the access control list (ACL) of the object.
                    System.out.println("ObjectACL: " + file.getObjectACL());
                    // Query the type of the object.
                    System.out.println("OssObjectType: " + file.getOssObjectType());
                    // Query the storage class of the object.
                    System.out.println("OssStorageClass: " + file.getOssStorageClass());
                    // Query the number of tags of the object.
                    System.out.println("TaggingCount: " + file.getOssTaggingCount());
                    if(file.getOssTagging() != null){
                        for(Tagging tag : file.getOssTagging().getTagging()){
                            System.out.println("Key: " + tag.getKey());
                            System.out.println("Value: " + tag.getValue());
                        }
                    }
                    if(file.getOssUserMeta() != null){
                        for(UserMeta meta : file.getOssUserMeta().getUserMeta()){
                            System.out.println("Key: " + meta.getKey());
                            System.out.println("Value: " + meta.getValue());
                        }
                    }
                }
            } else if(doMetaQueryResult.getAggregations() != null){
                for(Aggregation aggre : doMetaQueryResult.getAggregations().getAggregation()){
                    // Query the field by which the aggregation is performed.
                    System.out.println("Field: " + aggre.getField());
                    // Query the aggregation operator.
                    System.out.println("Operation: " + aggre.getOperation());
                    // Query the result of the aggregate operation.
                    System.out.println("Value: " + aggre.getValue());
                    if(aggre.getGroups() != null && aggre.getGroups().getGroup().size() > 0){
                        // Query the value of the aggregation.
                        System.out.println("Groups value: " + aggre.getGroups().getGroup().get(0).getValue());
                        // Query the total number of the aggregations.
                        System.out.println("Groups count: " + aggre.getGroups().getGroup().get(0).getCount());
                    }
                }
            } else {
                System.out.println("NextToken: " + doMetaQueryResult.getNextToken());
            }
        } catch (OSSException oe) {
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("Error Message: " + ce.getMessage());
        } finally {
            // Shut down the OSSClient instance.
            ossClient.shutdown();
        }
    }
}

Python

For more code examples, see MetaSearch.

import argparse
import alibabacloud_oss_v2 as oss

# Create a command-line parameter parser for parsing arguments from the command line.
parser = argparse.ArgumentParser(description="do meta query 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')

def main():
    # Parse the command-line arguments.
    args = parser.parse_args()

    # Obtain 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
    # Set the region to the one provided from the command line.
    cfg.region = args.region
    # If a custom endpoint is provided, update the endpoint attribute of the cfg object with the provided endpoint.
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    # Create an OSS client.
    client = oss.Client(cfg)

    # Perform the metadata-based query.
    result = client.do_meta_query(oss.DoMetaQueryRequest(
            bucket=args.bucket,  # The bucket that stores the objects to be queried.
            meta_query=oss.MetaQuery(  # Specify query settings.
                aggregations=oss.MetaQueryAggregations(  # Specify aggregatios.
                    aggregations=[  # The aggregation list.
                        oss.MetaQueryAggregation(  # The first aggregation: the total object size.
                            field='Size',
                            operation='sum',
                        ),
                        oss.MetaQueryAggregation(  # The second aggregation: the maximum object size.
                            field='Size',
                            operation='max',
                        )
                    ],
                ),
                next_token='',  # The pagination token.
                max_results=80369,  # The maximum number of entries that can be returned.
                query='{"Field": "Size","Value": "1048576","Operation": "gt"}',  # The query condition.
                sort='Size',  # The sorting field.
                order=oss.MetaQueryOrderType.DESC,  # The sorting order.
            ),
    ))

    # Display the basic response information.
    print(f'status code: {result.status_code},'
          f' request id: {result.request_id},'
          # You can uncomment the one or more of the following lines to display more details.
          # f' files: {result.files},'
          # f' file: {result.files.file},'
          # f' file modified time: {result.files.file.file_modified_time},'
          # f' etag: {result.files.file.etag},'
          # f' server side encryption: {result.files.file.server_side_encryption},'
          # f' oss tagging count: {result.files.file.oss_tagging_count},'
          # f' oss tagging: {result.files.file.oss_tagging},'
          # f' key: {result.files.file.oss_tagging.taggings[0].key},'
          # f' value: {result.files.file.oss_tagging.taggings[0].value},'
          # f' key: {result.files.file.oss_tagging.taggings[1].key},'
          # f' value: {result.files.file.oss_tagging.taggings[1].value},'
          # f' oss user meta: {result.files.file.oss_user_meta},'
          # f' key: {result.files.file.oss_user_meta.user_metas[0].key},'
          # f' value: {result.files.file.oss_user_meta.user_metas[0].value},'
          # f' key: {result.files.file.oss_user_meta.user_metas[1].key},'
          # f' value: {result.files.file.oss_user_meta.user_metas[1].value},'
          # f' filename: {result.files.file.filename},'
          # f' size: {result.files.file.size},'
          # f' oss object type: {result.files.file.oss_object_type},'
          # f' oss storage class: {result.files.file.oss_storage_class},'
          # f' object acl: {result.files.file.object_acl},'
          # f' oss crc64: {result.files.file.oss_crc64},'
          # f' server side encryption customer algorithm: {result.files.file.server_side_encryption_customer_algorithm},'
          # f' aggregations: {result.aggregations},'
          f' field: {result.aggregations.aggregations[0].field},'
          f' operation: {result.aggregations.aggregations[0].operation},'
          f' field: {result.aggregations.aggregations[1].field},'
          f' operation: {result.aggregations.aggregations[1].operation},'
          f' next token: {result.next_token},'
    )

    # If matched objects are found, display tags and user metadata.
    if result.files:
        if result.files.file.oss_tagging.taggings:
            for r in result.files.file.oss_tagging.taggings:
                print(f'result: key: {r.key}, value: {r.value}')
        if result.files.file.oss_user_meta.user_metas:
            for r in result.files.file.oss_user_meta.user_metas:
                print(f'result: key: {r.key}, value: {r.value}')
    # Display all aggregations.
    if result.aggregations.aggregations:
        for r in result.aggregations.aggregations:
            print(f'result: field: {r.field}, operation: {r.operation}')

if __name__ == "__main__":
    main()

Go

For more code examples, see MetaSearch using OSS SDK for Go 2.0.

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(&region, "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)
		}
	}
}

PHP

For more code examples, see MetaSearch using OSS SDK for PHP 2.0.

Use ossutil

You can use the ossutil command-line interface (CLI) to query objects that meet specified conditions using the MetaSearch feature. To install ossutil, see Install ossutil.

The following example shows how to query files that meet specified conditions in the bucket named examplebucket.

ossutil api do-meta-query --bucket examplebucket --meta-query "{\"MaxResults\":\"5\",\"Query\":\"{\\\"Field\\\": \\\"Size\\\",\\\"Value\\\": \\\"1048576\\\",\\\"Operation\\\": \\\"gt\\\"}\",\"Sort\":\"Size\",\"Order\":\"asc\",\"Aggregations\":{\"Aggregation\":[{\"Field\":\"Size\",\"Operation\":\"sum\"},{\"Field\":\"Size\",\"Operation\":\"max\"}]}}"

For more information about this command, see do-meta-query.

Disable MetaSearch

  • Disabling this feature does not affect the data that is already stored in OSS. If you re-enable the feature, the system rescans existing files and rebuilds the index. This process takes time, and the duration depends on the number of files in your bucket.

  • Billing stops in the hour after you disable the feature. However, bill generation may be delayed. We recommend that you monitor your bills.

Use the OSS console

Log in to the OSS console. On the Data Indexing page, click Disable next to Metadata Management and follow the prompts to confirm the action.

image

Use an Alibaba Cloud SDK

Java

For more code examples, see MetaSearch using OSS SDK for Java.

import com.aliyun.oss.*;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.common.comm.SignVersion;

public class CloseMetaQuery {
    public static void main(String[] args) throws Exception {
        // In this example, the endpoint of the China (Hangzhou) region is used.
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        // Specify the name of the bucket. Example: examplebucket.
        String bucketName = "examplebucket";
        // Obtain access credentials from environment variables. 
        // Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
        EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
        // Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou.
        String region = "cn-hangzhou";

        // Create an OSSClient instance.
        // Call the shutdown method to release associated resources when the OSSClient instance is no longer used.
        ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
        clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
        OSS ossClient = OSSClientBuilder.create()
                .endpoint(endpoint)
                .credentialsProvider(credentialsProvider)
                .clientConfiguration(clientBuilderConfiguration)
                .region(region)
                .build();

        try {
            // Disable the MetaSearch feature for the bucket.
            ossClient.closeMetaQuery(bucketName);
        } catch (OSSException oe) {
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("Error Message: " + ce.getMessage());
        } finally {
            // Shut down the OSSClient instance.
            if(ossClient != null){
                ossClient.shutdown();
            }
        }
    }
}

Python

For more code examples, see MetaSearch.

import argparse
import alibabacloud_oss_v2 as oss

# Create an ArgumentParser object for processing command-line arguments.
parser = argparse.ArgumentParser(description="close meta query 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')

def main():
    # Parse the command-line arguments.
    args = parser.parse_args()

    # Obtain access credentials from environment variables.
    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    # Use the default configuration of the SDK.
    cfg = oss.config.load_default()
    # Set the credential provider to the credential provider obtained from the environment variables.
    cfg.credentials_provider = credentials_provider
    # Set the region in the configuration to the one specified in the command line.
    cfg.region = args.region
    # If a custom endpoint is provided, update the endpoint attribute of the cfg object with the provided endpoint.
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    # Create an OSS client.
    client = oss.Client(cfg)

    # Call the close_meta_query method to disable the metadata query feature for the specified bucket.
    result = client.close_meta_query(oss.CloseMetaQueryRequest(
            bucket=args.bucket,
    ))

    # Display the HTTP status code and request ID.
    print(f'status code: {result.status_code}, request id: {result.request_id}')

# Call the main function when the script is directly run.
if __name__ == "__main__":
    main()

Go

For more code examples, see MetaSearch using OSS SDK for Go 2.0.

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(&region, "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)
}

PHP

For more code examples, see MetaSearch using OSS SDK for PHP 2.0.

Use ossutil

The following sample command shows how to disable the metadata management feature for the bucket named examplebucket.

ossutil api close-meta-query --bucket examplebucket

For more information about this command, see close-meta-query.

Query conditions and output settings

Query conditions

The following table describes all query conditions. You can set them individually or in combination as needed.

OSS Metadata Condition

Query condition

Description

Storage Class

By default, all OSS storage classes are selected: Standard, Infrequent Access (IA), Archive, Cold Archive, and Deep Cold Archive. You can select the storage classes of the objects that you want to display in the query results.

ACL

By default, all four access control lists (ACLs) supported by OSS are selected: Inherit from Bucket, Private, Public Read, and Public Read/Write. You can select the ACLs of the objects that you want to display in the query results.

Object Name

Supports Fuzzy Match and Equal To. If you want to display a specific file, such as exampleobject.txt, in the query results, you can match the file in one of the following ways:

  • Select Equal To, and then enter the full filename exampleobject.txt.

  • Select Fuzzy Match, and then enter a prefix or suffix, such as example or .txt.

    Important

    A fuzzy match can hit any character in the object name. For example, if you enter test, the query results will include localfolder/test/.example.jpg and localfolder/test.jpg.

Upload Type

By default, all four object types supported by OSS are selected. You can select the object types that you want to display in the query results. The object types are described as follows:

  • Normal: An object created by a simple upload.

  • Multipart: An object created by a multipart upload.

  • Appendable: An object created by an append upload.

  • Symlink: A symbolic link created for quick access to an object.

Last Modified At

Specify the Start Date and End Date of the last modification time for the object. The time is accurate to the second.

Object Size

Supports five filter conditions: Equal To, Greater Than, Greater Than or Equal To, Less Than, and Less Than or Equal To. The file size unit is KB.

Object Versions

Only the current version of an object can be queried.

Object ETag and Tag Condition

To filter and find objects based on their ETag and tags, enter the ETag or tag information for the objects that you want to display in the query results.

  • ETag supports only an exact match. The ETag must be enclosed in quotation marks. For example: "5B3C1A2E0563E1B002CC607C6689". You can enter multiple ETags, with one ETag per line.

  • Specify Object Etags as a key-value pair. Both the key and value of an object tag are case-sensitive. For more information about tag rules, see Object tagging.

Search Result Settings

You can sort the output results and perform simple statistical analysis.

  • Object Sort Order: You can sort by last modified time, filename, or file size in ascending, descending, or default order. You can sort the query results as needed to quickly find the required files.

  • Data Aggregation: Supports various output types. You can perform calculations on the query results, such as deduplication, group counting, finding the maximum or minimum value, calculating the average, and summing values. This allows for efficient data analysis and management.

Related API operations

The preceding operations are based on API operations. If your program has high customization requirements, you can directly make REST API requests. If you make REST API requests, you must manually write code to calculate signatures.

For more information about enabling the metadata management feature, see OpenMetaQuery.

To query files that meet specified conditions, see DoMetaQuery.

For more information about disabling the metadata management feature, see CloseMetaQuery.

Billing

  • MetaSearch fees consist of the following two parts:

    • MetaSearch feature fees (free during public preview)

      This includes object metadata management fees. This feature is in public preview and is free of charge. The feature will be officially billed starting August 25, 2025, after the public preview ends. After the public preview, you are charged based on the pricing of OSS data indexing. For more information, see Data indexing fees.

    • API request fees

      API request fees are generated during incremental file index updates. You are charged based on the number of API calls. The related API requests are as follows:

      Behavior

      API

      Count

      Build an index for files in a bucket

      HeadObject and GetObject

      Called once for each file

      A file in the bucket has a tag

      GetObjectTag

      Called once for each file with a tag

      A file in the bucket has custom metadata

      GetObjectMeta

      Called once for each file with custom metadata

      A symbolic link file exists in the bucket

      GetSymlink

      Called once for each symbolic link file

      Scan files in a bucket

      ListObjects

      Called once for every 1,000 files scanned

      For more information about OSS API request fees, see Request fees.

  • To stop billing, you must disable MetaSearch.

FAQ

Why does it take a long time to build a data index for a bucket that contains hundreds of millions of files?

An index can be built for approximately 600 incremental files per second. You can estimate the time required to build the index based on the number of files in your bucket.

How do I query the total size of files in a specified prefix or directory?

  1. Log on to the OSS console.

  2. In the left-side navigation pane, click Buckets. On the Buckets page, find and click the desired bucket.

  3. In the navigation pane on the left, choose Object Management > Data Indexing.

  4. Set Object Name to Prefix Match and enter random_files/ as the prefix. Leave the other parameters at their default settings.

    image

  5. Click Search Result Settings.

    • For Object Sort Order, select Default.

    • For Data Aggregation, select Object Size and Sum.

      image

  6. Click Query Now. You can view the final statistics, which include the total number of files and the total size of all files in the `random_files/` directory.

    image

References

MetaSearch supports multiple filter conditions, such as last modified time, storage class, access control list, and file size. If you want to filter objects within a specific time range from many objects in an OSS bucket, see How to filter files in OSS within a specified time range.