Object Storage Service (OSS) propose la fonctionnalité d'indexation des données, qui vous permet d'interroger les objets correspondant à des conditions de métadonnées spécifiques, telles que le nom, l'ETag, la classe de stockage, la taille et la date de dernière modification. Cette fonctionnalité trie et agrège les résultats de requête selon vos besoins métier, améliorant ainsi l'efficacité de la recherche d'objets spécifiques au sein d'un grand volume de données.
Notes
Seules les versions 2.1.6.0 et ultérieures du SDK OSS pour Python prennent en charge la fonctionnalité d'indexation des données.
L'indexation des données est uniquement disponible pour les buckets situés dans la région Chine (Hangzhou). Pour plus d'informations, consultez la rubrique MetaSearch.
Cette rubrique utilise le point de terminaison public de la région Chine (Hangzhou). Si vous souhaitez accéder à OSS depuis d'autres services Alibaba Cloud situés dans la même région, utilisez un point de terminaison interne. Pour obtenir la liste des régions et des points de terminaison OSS, reportez-vous à la section Régions et points de terminaison.
Dans cet exemple, les identifiants d'accès sont récupérés depuis des variables d'environnement. Pour savoir comment configurer ces identifiants, consultez la page Configurer les identifiants d'accès avec le SDK OSS pour Python 1.0.
Cet article illustre la création d'une instance OSSClient à l'aide d'un point de terminaison OSS. Pour d'autres configurations, comme l'utilisation d'un domaine personnalisé ou l'authentification via des identifiants Security Token Service (STS), voir la rubrique Initialisation.
Activer la fonctionnalité de gestion des métadonnées pour un bucket
L'exemple de code suivant montre comment activer la gestion des métadonnées pour un bucket. Une fois cette fonctionnalité activée, OSS crée une bibliothèque d'index de métadonnées pour le bucket concerné et génère les index pour tous les objets qu'il contient. Après la création de la bibliothèque, OSS continue d'analyser les nouveaux objets en quasi temps réel et crée leurs index de métadonnées.
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# 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.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use the signature algorithm V4.
region = "cn-hangzhou"
# Specify the name of the bucket. Example: examplebucket.
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)
# Enable the metadata management feature for the bucket.
bucket.open_bucket_meta_query()
Interroger la bibliothèque d'index de métadonnées d'un bucket
Le code ci-dessous illustre la procédure pour interroger la bibliothèque d'index de métadonnées d'un bucket :
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# 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.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use the signature algorithm V4.
region = "cn-hangzhou"
# Specify the name of the bucket. Example: examplebucket.
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)
# Query the metadata index library information of a specified bucket.
get_result = bucket.get_bucket_meta_query_status()
# Display the state.
print(get_result.state)
Interroger les objets répondant à des conditions spécifiques
L'exemple de code suivant montre comment interroger les objets satisfaisant certaines conditions et afficher leurs informations selon des champs et des méthodes de tri définis :
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
from oss2.models import MetaQuery, AggregationsRequest
# 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.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use the signature algorithm V4.
region = "cn-hangzhou"
# Specify the name of the bucket. Example: examplebucket.
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)
# Query objects that meet specific conditions and list the object information based on specific fields and sorting methods.
# Query objects that are smaller than 1 MB, return up to 10 objects at a time, and sort the objects in ascending order.
do_meta_query_request = MetaQuery(max_results=10, query='{"Field": "Size","Value": "1048576","Operation": "lt"}', sort='Size', order='asc')
result = bucket.do_bucket_meta_query(do_meta_query_request)
# Display the object names.
print(result.files[0].file_name)
# Display the ETags of the objects.
print(result.files[0].etag)
# Display the types of the objects.
print(result.files[0].oss_object_type)
# Display the storage classes of the objects.
print(result.files[0].oss_storage_class)
# Display the CRC-64 values of the objects.
print(result.files[0].oss_crc64)
# Display the access control lists (ACLs) of the objects.
print(result.files[0].object_acl)
Désactiver la fonctionnalité de gestion des métadonnées pour un bucket
Le code suivant montre comment désactiver la gestion des métadonnées pour un bucket donné :
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# 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.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use the signature algorithm V4.
region = "cn-hangzhou"
# Specify the name of the bucket. Example: examplebucket.
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)
# Disable the metadata management feature for the bucket.
bucket.close_bucket_meta_query()
Références
Pour plus d'informations sur l'opération API permettant d'activer la gestion des métadonnées, consultez la rubrique OpenMetaQuery.
Pour en savoir plus sur l'opération API utilisée pour interroger les informations d'une bibliothèque d'index de métadonnées, voir GetMetaQueryStatus.
Pour obtenir des détails sur l'opération API qui permet d'interroger les objets répondant à des conditions spécifiques et de lister leurs informations selon des champs et des méthodes de tri donnés, reportez-vous à la page DoMetaQuery.
Pour plus d'informations sur l'opération API permettant de désactiver la gestion des métadonnées, consultez la documentation relative à CloseMetaQuery.