如何使用 Boto3 獲取 AWS 賬戶中所有登錄檔列表


本文將介紹使用者如何獲取 AWS 賬戶中所有登錄檔的列表。

示例

獲取 AWS Glue 資料目錄中所有可用的登錄檔列表。

問題陳述:使用 Python 中的 boto3 庫獲取所有登錄檔的列表。

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

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

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

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

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

  • 步驟 5:現在使用 list_registries

  • 步驟 6:它返回 AWS Glue 資料目錄中所有登錄檔的列表,其中包含登錄檔的有限詳細資訊。它不包括狀態為“正在刪除”的登錄檔。它僅包含可用登錄檔的列表。如果沒有登錄檔,則它返回一個空字典。

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

程式碼示例

以下程式碼獲取所有登錄檔的列表:-

import boto3
from botocore.exceptions import ClientError

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

輸出

{
   'Registries':[
   {
      'RegistryName': 'employee_details',
      'RegistryArn': 'string',
      'Description': 'Registry for employees record',
      'Status': 'AVAILABLE',
      'CreatedTime': 'string',
      'UpdatedTime': 'string'
   },
   {
      'RegistryName': 'security_details',
      'RegistryArn': 'string',
      'Description': 'Registry for security record',
      'Status': 'AVAILABLE',
      'CreatedTime': 'string',
      'UpdatedTime': 'string'
   },
],
'Request': ……
}

更新於: 2021-04-15

107 次檢視

開啟您的 職業生涯

透過完成課程獲得認證

開始學習
廣告