Todos los productos
Search
Centro de documentos

Object Storage Service:Uso de MetaSearch con OSS SDK for Python 2.0

Última actualización: Sep 24, 2026

MetaSearch es una función de indexación de Object Storage Service (OSS) basada en los metadatos de los objetos. Estos metadatos se especifican como condiciones de índice para consultar objetos. De esta manera, usted gestiona y conoce las estructuras de datos, ejecuta consultas, recopila estadísticas y administra los objetos de forma eficiente.

Notas de uso

  • El código de ejemplo de este tema utiliza el identificador de región cn-hangzhou, correspondiente a la región China (Hangzhou). De forma predeterminada, se usa el endpoint público para acceder a los recursos de un bucket. Si desea acceder a los recursos del bucket desde otros servicios de Alibaba Cloud ubicados en la misma región que el bucket, utilice un endpoint interno. Para obtener más información sobre las regiones y los endpoints de OSS, consulte Regions and endpoints.

Código de ejemplo

Habilitar la función de gestión de metadatos

A continuación se muestra el código de ejemplo para habilitar la función de gestión de metadatos en un bucket específico. Cuando se habilita esta función en un bucket, OSS crea una biblioteca de índices de metadatos y genera índices de metadatos para todos los objetos del bucket. Una vez creada la biblioteca de índices de metadatos, OSS continúa con escaneos casi en tiempo real sobre los objetos incrementales del bucket y crea sus respectivos índices de metadatos.

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

Consultar la biblioteca de índices de metadatos de un bucket

A continuación se muestra el código de ejemplo para consultar la biblioteca de índices de metadatos de un 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()

Consultar objetos que cumplen condiciones específicas

A continuación se muestra el código de ejemplo para consultar objetos que cumplen ciertas condiciones. La información de los objetos se lista según el campo y el orden de clasificación especificados:

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

Deshabilitar la función de gestión de metadatos

A continuación se muestra el código de ejemplo para deshabilitar la función de gestión de metadatos en un bucket específico:

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

Referencias

  • Para ver el código de ejemplo completo que habilita la gestión de metadatos, consulte open_meta_query.py.

  • El código de ejemplo completo para consultar la biblioteca de índices de metadatos de un bucket está available en get_meta_query_status.py.

  • Si necesita el código de ejemplo completo que busca objetos según ciertas condiciones y lista la información de los objetos coincidentes por el campo y el orden especificados, revise do_meta_query.py.

  • Para ver el código de ejemplo completo que deshabilita la gestión de metadatos, consulte close_meta_query.py.