如何使用 Boto3 獲取 AWS Glue 資料目錄中的目錄的安全配置/加密設定?


問題陳述 - 在 Python 中使用 boto3 庫檢索目錄的安全配置/加密設定。

示例 - 檢索目錄的安全配置/加密設定。

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

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

步驟 2 - catalog_id 是可選引數。如果未提供,則採用使用者的 AWS 賬戶詳細資訊。

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

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

步驟 5 - 現在使用 get_data_catalog_encryption_settings 函式並將 catalog_id 作為 CatalogId 引數傳遞。

步驟 6 - 它返回加密設定的詳細資訊。

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

示例

使用以下程式碼檢索目錄的安全配置/加密設定:

import boto3
from botocore.exceptions import ClientError

def retrieves_encryption_setting(catalog_id=None)
   session = boto3.session.Session()
   glue_client = session.client('glue')
   try:
      response = glue_client.get_data_catalog_encryption_settings(CatalogId = catalog_id)
      return response
   except ClientError as e:
      raise Exception("boto3 client error in retrieves_encryption_setting: " + e.__str__())
   except Exception as e:
      raise Exception("Unexpected error in retrieves_encryption_setting: " + e.__str__())
print(retrieves_encryption_setting())

輸出

{'DataCatalogEncryptionSettings': {'EncryptionAtRest':
{'CatalogEncryptionMode': 'SSE-KMS'}, 'ConnectionPasswordEncryption':
{'ReturnConnectionPasswordEncrypted': True}}, 'ResponseMetadata':
{'RequestId': '5ffc0dbb***************7c', 'HTTPStatusCode': 200,
'HTTPHeaders': {'date': 'Sun, 28 Feb 2021 12:22:16 GMT', 'content-type':
'application/x-amz-json-1.1', 'content-length': '166', 'connection':
'keep-alive', 'x-amzn-requestid': '5ffc0dbb********************7c'},
'RetryAttempts': 0}}

更新於: 2021-03-23

217 次檢視

啟動您的 職業生涯

透過完成課程獲得認證

開始
廣告

© . All rights reserved.