Use MetaSearch with OSS SDK for Python 2.0

Updated at:

Provided by Object Storage Service (OSS), Metasearch is an indexing feature based on the metadata of objects, which can be specified as index conditions to query objects. This way, you can manage and learn about data structures, perform queries, collect statistics, and manage objects in an efficient manner.

Usage notes

  • The sample code in this topic uses the region ID cn-hangzhou of the China (Hangzhou) region. By default, the public endpoint is used to access resources in a bucket. If you want to access resources in the bucket from other Alibaba Cloud services in the same region in which the bucket is located, use an internal endpoint. For more information about OSS regions and endpoints, see Regions and endpoints.

Sample code

Enable the metadata management feature

The following sample code enables the metadata management feature for a specific bucket. After you enable the metadata management feature for a bucket, 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 continues to perform quasi-real-time scans on incremental objects in the bucket and creates metadata indexes for the incremental objects.

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()

Query the metadata index library of a bucket

The following sample code queries the metadata index library of a bucket:

import argparse
import alibabacloud_oss_v2 as oss

# Create a command-line parameter parser and define the parameters.
parser = argparse.ArgumentParser(description="get meta query status 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 which the bucket is located.
    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 get_meta_query_status method to obtain the metadata query status of the specified bucket.
    result = client.get_meta_query_status(oss.GetMetaQueryStatusRequest(
            bucket=args.bucket,
    ))

    # Print the response information, including the status code, request ID, creation time, update time, status, and phase.
    print(f'status code: {result.status_code},'
            f' request id: {result.request_id},'
            f' create time: {result.meta_query_status.create_time},'
            f' update time: {result.meta_query_status.update_time},'
            f' state: {result.meta_query_status.state},'
            f' phase: {result.meta_query_status.phase},'
    )

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

Query objects that meet specific conditions

The following sample code queries objects that meet specific conditions and lists the object information based on the specific field and sorting order:

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()

Disable the metadata management feature

The following sample code disables the metadata management feature for a specific bucket:

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()

References

  • For the complete sample code that enables metadata management, see open_meta_query.py.

  • For the complete sample code that queries the metadata index library of a bucket, see get_meta_query_status.py.

  • For the complete sample code that queries objects based on conditions and lists matched object information by the specified field in the specified order, see do_meta_query.py.

  • For the complete sample code that disables metadata management, see close_meta_query.py.