如何使用 Boto3 獲取 AWS 賬戶中所有觸發器的列表


在本文中,我們將瞭解使用者如何獲取 AWS 賬戶中所有現有觸發器的列表。

示例

獲取 AWS Glue 資料目錄中所有可用觸發器的列表。

問題陳述:使用 Python 中的 boto3 庫獲取所有觸發器的列表。

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

  • 步驟 1:匯入 boto3botocore 異常以處理異常。

  • 步驟 2:此函式沒有引數。

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

  • 步驟 4:glue 建立 AWS 客戶端。

  • 步驟 5:現在使用 list_triggers 函式。

  • 步驟 6:它返回 AWS Glue 資料目錄中所有現有觸發器的列表。如果沒有觸發器,則返回一個空字典。但是,此函式採用可選引數 Tags,以便使用者可以篩選觸發器並僅返回與標籤關聯的觸發器。

  • 步驟 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}}

更新於:2021年4月15日

386 次瀏覽

啟動您的 職業生涯

透過完成課程獲得認證

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