如何使用 Boto3 对 AWS Glue 中的安全配置进行分页
awsboto3pythonserver side programmingprogramming更新于 2023/12/24 12:58:00
在本文中,我们将了解如何对 AWS Glue 中的安全配置进行分页。
示例
问题陈述:使用 Python 中的 boto3 库对账户中创建的 AWS Glue 数据目录中的安全配置进行分页
解决此问题的方法/算法
步骤 1:导入 boto3 和 botocore 异常来处理异常。
步骤 2: max_items、page_size 和 starting_token 是此操作的可选参数函数。
max_items 表示要返回的记录总数。如果可用记录数 > max_items,则响应中将提供 NextToken 以恢复分页。
page_size 表示每页的大小。
starting_token 有助于分页,它使用来自上一个响应的 NextToken。
步骤 3:使用 boto3 lib 创建 AWS 会话。确保在默认配置文件中提到 region_name。如果没有提及,则在创建会话时明确传递 region_name。
步骤 4: 为 glue 创建 AWS 客户端。
步骤 5: 使用 get_security_configurations
创建包含所有爬虫详细信息的 paginator 对象。
步骤 6: 调用 paginate 函数并传递 max_items、page_size 和 starting_token 作为 PaginationConfig
步骤 7: 根据 max_size 和 page_size 返回记录数。
步骤 8: 处理如果分页时出现问题,则会出现一般异常。
示例代码
使用以下代码分页显示用户帐户中创建的所有安全配置 −
import boto3 from botocore.exceptions import ClientError def paginate_through_security_configuration(max_items=None:int,page_size=None:int, starting_token=None:string): session = boto3.session.Session() glue_client = session.client('glue') try: paginator = glue_client.get_paginator('get_security_configuration') response = paginator.paginate(PaginationConfig={ 'MaxItems':max_items, 'PageSize':page_size, 'StartingToken':starting_token} ) return response except ClientError as e: raise Exception("boto3 client error in paginate_through_security_configuration: " + e.__str__()) except Exception as e: raise Exception("Unexpected error in paginate_through_security_configuration: " + e.__str__()) a = paginate_through_security_configuration(2,5) print(*a)
输出
{'SecurityConfigurations': [ {'Name': 'test-sc', 'CreatedTimeStamp': datetime.datetime(2020, 9, 24, 1, 53, 21, 265000, tzinfo=tzlocal()), 'EncryptionConfiguration': {'S3Encryption': [{'S3EncryptionMode': 'SSE-KMS', 'KmsKeyArn': 'arn:aws:kms:us-east-1:*************:key/***************'}]}}, {'Name': 'port-sc', 'CreatedTimeStamp': datetime.datetime(2020, 11, 6, 0, 38, 3, 753000, tzinfo=tzlocal()), 'EncryptionConfiguration': {'S3Encryption': [{'S3EncryptionMode': 'SSE-KMS', 'KmsKeyArn': 'arn:aws:kms:us-east-1:********:key/***************'}]}}], 'NextToken': '', 'ResponseMetadata': {'RequestId': **********, 'HTTPStatusCode': 200, 'HTTPHeaders': {'date': 'Fri, 02 Apr 2021 13:19:57 GMT', 'content-type': 'application/x-amz-json-1.1', 'content-length': '826', 'connection': 'keep-alive', 'x-amzn-requestid': *********}, 'RetryAttempts': 0}}