如何使用Boto3获得AWS账户中触发器列表
本文将介绍如何获取AWS账户中所有触发器的列表。
更多Python相关文章,请阅读:Python 教程
示例
获取AWS Glue Data Catalog中所有触发器的列表。
问题陈述: 使用Python中的 boto3 库获取所有触发器的列表。
解决此问题的方法/算法
- 步骤1: 导入 boto3 和 botocore 的exceptions来处理异常。
-
步骤2: 此函数中没有参数。
-
步骤3: 使用 boto3库 创建AWS session。请确保在默认profile中提到了 region_name 。如果没有提到,则在创建session时明确传递 region_name 。
-
步骤4: 为 glue 创建AWS client。
-
步骤5: 现在使用 list_triggers 函数。
-
步骤6: 它返回AWS Glue数据目录中存在的所有触发器的列表。如果没有触发器,则返回空字典。但是,此函数接受可选参数作为标记,以便用户可以过滤并仅返回与标记关联的那些触发器。
-
步骤7: 如果在检查触发器时出现问题,请处理通用异常。
样例代码
以下代码获取所有触发器的列表:
import boto3
from botocore.exceptions import ClientError
def list_of_triggers()
session = boto3.session.Session()
glue_client = session.client('glue')
try:
triggers = glue_client.list_triggers()
return triggers
except ClientError as e:
raise Exception("boto3 client error in list_of_triggers: " + e.__str__())
except Exception as e:
raise Exception("Unexpected error in list_of_triggers: " + e.__str__())
print(list_of_triggers())
输出
{'TriggerNames':
['data-etl-file-passed-to-splitter',
'file-passed-to-worker',
'file-trigger',
'test-daily-jobs',
'test-daily-jobs-copy'
],
'ResponseMetadata': {'RequestId': '8e95115b****************90', 'HTTPStatusCode': 200, 'HTTPHeaders': {'date': 'Sat, 27 Mar 2021 09:14:03 GMT', 'content-type': 'application/x-amz-json-1.1', 'content-length': '304', 'connection': 'keep-alive', 'x-amzn-requestid': '8e95115b*********************90'}, 'RetryAttempts': 0}}
极客教程