如何使用 Boto3 从 AWS Glue 数据目录中获取目录的安全配置/加密设置?
boto3pythonserver side programmingprogramming
问题陈述 − 使用 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}}