AWS SDK を使用して特定のタグを持つ Lookout for Vision プロジェクトを検索する - AWS SDK コード例

Doc AWS SDK Examples リポジトリには、他にも SDK の例があります。 AWS GitHub

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

AWS SDK を使用して特定のタグを持つ Lookout for Vision プロジェクトを検索する

次のコード例は、特定のタグを持つ Lookout for Vision プロジェクトを検索する方法を示しています。

詳細については、「ワークグループのタグ付け」を参照してください。

Python
SDK for Python (Boto3)
注記

には他にもがあります GitHub。AWS コード例リポジトリ で全く同じ例を見つけて、設定と実行の方法を確認してください。

import logging import argparse import boto3 from botocore.exceptions import ClientError logger = logging.getLogger(__name__) def find_tag(tags, key, value): """ Finds a tag in the supplied list of tags. :param tags: A list of tags associated with a Lookout for Vision model. :param key: The tag to search for. :param value: The tag key value to search for. :return: True if the tag value exists, otherwise False. """ found = False for tag in tags: if key == tag["Key"]: logger.info("\t\tMatch found for tag: %s value: %s.", key, value) found = True break return found def find_tag_in_projects(lookoutvision_client, key, value): """ Finds Lookout for Vision models tagged with the supplied key and value. :param lookoutvision_client: A Boto3 Lookout for Vision client. :param key: The tag key to find. :param value: The value of the tag that you want to find. return: A list of matching model versions (and model projects) that were found. """ try: found_tags = [] found = False projects = lookoutvision_client.list_projects() # Iterate through each project and models within a project. for project in projects["Projects"]: logger.info("Searching project: %s ...", project["ProjectName"]) response_models = lookoutvision_client.list_models( ProjectName=project["ProjectName"] ) for model in response_models["Models"]: model_description = lookoutvision_client.describe_model( ProjectName=project["ProjectName"], ModelVersion=model["ModelVersion"], ) tags = lookoutvision_client.list_tags_for_resource( ResourceArn=model_description["ModelDescription"]["ModelArn"] ) logger.info( "\tSearching model: %s for tag: %s value: %s.", model_description["ModelDescription"]["ModelArn"], key, value, ) if find_tag(tags["Tags"], key, value) is True: found = True logger.info( "\t\tMATCH: Project: %s: model version %s", project["ProjectName"], model_description["ModelDescription"]["ModelVersion"], ) found_tags.append( { "Project": project["ProjectName"], "ModelVersion": model_description["ModelDescription"][ "ModelVersion" ], } ) if found is False: logger.info("No match for tag %s with value %s.", key, value) except ClientError: logger.exception("Problem finding tags.") raise else: return found_tags def main(): logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s") parser = argparse.ArgumentParser(usage=argparse.SUPPRESS) parser.add_argument("tag", help="The tag that you want to find.") parser.add_argument("value", help="The tag value that you want to find.") args = parser.parse_args() key = args.tag value = args.value session = boto3.Session(profile_name="lookoutvision-access") lookoutvision_client = session.client("lookoutvision") print(f"Searching your models for tag: {key} with value: {value}.") tagged_models = find_tag_in_projects(lookoutvision_client, key, value) print("Matched models\n--------------") if len(tagged_models) > 0: for model in tagged_models: print(f"Project: {model['Project']}. model version:{model['ModelVersion']}") else: print("No matches found.") if __name__ == "__main__": main()