如何使用 Boto3 同時獲取多個觸發器的詳細資訊?


問題陳述 - 使用 Python 中的 boto3 庫獲取帳戶中可用的觸發器。例如,獲取帳戶中允許的觸發器的詳細資訊。

解決此問題的方法/演算法

步驟 1 - 匯入 boto3 和 botocore 異常以處理異常。

步驟 2 - 此函式不需要引數。它將獲取使用者帳戶中列出的所有觸發器,然後顯示每個觸發器的元資料。

步驟 3 - 使用 boto3 庫建立 AWS 會話。確保在預設配置檔案中提到了 region_name。如果未提及,則在建立會話時顯式傳遞 region_name。

步驟 4 - 為 Glue 建立 AWS 客戶端。

步驟 5 - 現在使用 list_triggers 函式獲取使用者帳戶中列出的所有作業。

步驟 6 - 呼叫 batch_get_triggers 並傳遞在先前函式中獲取的作業名稱。

步驟 7 - 它返回 list_of_triggers 和每個觸發器的元資料。

步驟 8 - 如果在檢查作業時出現問題,請處理通用異常。

示例

使用以下程式碼獲取使用者帳戶中列出的每個觸發器的詳細資訊 -

import boto3
from botocore.exceptions import ClientError

def get_resource_maetadata_of_triggers():
   session = boto3.session.Session()
   glue_client = session.client('glue')
   try:
      list_of_triggers = glue_client.list_triggers()
      response = glue_client.batch_get_triggers(TriggerNames=list_of_triggers['TriggerNames'])

      return list_of_triggers, response
   except ClientError as e:
      raise Exception( "boto3 client error in get_resource_maetadata_of_triggers: " + e.__str__())
   except Exception as e:
      raise Exception( "Unexpected error in get_resource_maetadata_of_triggers: " + e.__str__())

a, b = get_resource_metadat_of_triggers()
#List of Triggers
print(a)
#Resource metadata of each Triggers
print(b)

輸出

#List of Triggers
{'TriggersNames': ['01_PythonShellTest1'],
'NextToken':
'eyJleHBpcmF0aW9uIjp7InNlY29uZHMiOjE2MTQxNzE2OTksIm5hbm9zIjo1MTYwMDAwMDB
9LCJsYXN0RXZhbHV
zFiMzAzNzAxMzRmNDk3NWM3M2MyMjhjYTk5MDgzZTA3YjQ0ZWEyOTZlIn19fQ==',
'ResponseMetadata': {'RequestId': '5d3eb19a-41f5-b24e-2d59ed9664b5',
'HTTPStatusCode': 200, 'HTTPHeaders': {'date': 'Tue, 23 Feb 2021
13:01:39 GMT', 'content-type': 'application/x-amz-json-1.1', 'contentlength': '1134', 'connection': 'keep-alive', 'x-amzn-requestid':
'5d3eb19a-41f5-b24e-2d59ed9664b5'}, 'RetryAttempts': 0}}

#Resource metadata of each Triggers
{'Triggers': [{'Name': '01_PythonShellTest1', 'WorkflowName':
'arn:aws:iam::1234:role/dev-edl, 'Id': 'string', 'Type': 'string',
'State':
'CREATING'|'CREATED'|'ACTIVATING'|'ACTIVATED'|'DEACTIVATING'|'DEACTIVATE
D'|'DELETING'|'UPDATING', 'Description': 'string', 'Schedule': 'string'
}]}

更新於: 2021-03-22

102 次檢視

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告
© . All rights reserved.