如何使用 Boto3 獲取 AWS 賬戶中所有爬蟲的列表


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

示例

問題陳述: 使用 Python 中的 boto3 庫獲取所有爬蟲的列表。

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

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

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

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

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

  • 步驟 5: 現在使用 list_crawlers

  • 步驟 6: 它返回 AWS Glue 資料目錄中所有現有爬蟲的列表。

  • 步驟 7: 如果檢查作業時出現錯誤,則處理通用異常。

示例程式碼

以下程式碼獲取所有爬蟲的列表:

import boto3
from botocore.exceptions import ClientError

def list_of_crawlers()
   session = boto3.session.Session()
   glue_client = session.client('glue')
   try:
      crawler_details = glue_client.list_crawlers()
      return crawler_details
   except ClientError as e:
      raise Exception("boto3 client error in list_of_crawlers: " + e.__str__())
   except Exception as e:
      raise Exception("Unexpected error in list_of_crawlers: " + e.__str__())
print(list_of_crawlers())

輸出

{'CrawlerNames': ['crawler_for_s3_file_job', 'crawler_for_employee_data', 'crawler_for_security_data'], 'ResponseMetadata': {'RequestId': 'a498ba4a-7ba4-47d3-ad81-d86287829c1d', 'HTTPStatusCode': 200, 'HTTPHeaders': {'date': 'Sat, 13 Feb 2021 14:04:03 GMT', 'content-type': 'application/x-amz-json-1.1', 'content-length': '830', 'connection': 'keep-alive', 'x-amzn-requestid': 'a498ba4a-7ba4-47d3-ad81-d86287829c1d'}, 'RetryAttempts': 0}}

更新於:2021年4月15日

391 次瀏覽

啟動您的職業生涯

透過完成課程獲得認證

開始學習
廣告