问题陈述- 在 Python 中使用 boto3 库来获取迁移操作的状态。
示例- 获取帐户中迁移操作的状态。
步骤 1 - 导入 boto3 和 botocore 异常以处理异常。
步骤 2 - 传递需要检查迁移状态的参数 catalog_id。但是,这是一个可选参数。如果未提供,默认情况下它会检查已登录的用户帐户是否正在进行或已完成迁移。catalog_id 只不过是用户帐户 ID。
步骤 3 - 使用 boto3 库创建 AWS 会话。确保在默认配置文件中提到 region_name。如果未提及,则在创建会话时显式传递 region_name。
第 4 步- 为胶水创建一个 AWS 客户端。
第 5 步- 调用 get_catalog_import_status 并将 catalog_id 作为 CatalogId 参数传递。
第 6 步- 它将获取正在进行的迁移操作的详细信息。否则,它将获取上次迁移详细信息。
第 7 步- 如果检查作业时出现问题,则处理通用异常。
使用以下代码获取迁移操作的状态 -
import boto3 frombotocore.exceptionsimport ClientError def status_of_migration(catalog_id = None): session = boto3.session.Session() glue_client = session.client('glue') try: response = glue_client.get_catalog_import_status(CatalogId = catalog_id) return response except ClientError as e: raise Exception( "boto3 client error in status_of_migration: " + e.__str__()) except Exception as e: raise Exception("Unexpected error in status_of_migration: " + e.__str__()) print(status_of_migration())输出结果
{'ImportStatus': {'ImportCompleted': True, 'ImportTime': datetime.datetime(2017, 11, 17, 1, 32, 44, tzinfo=tzlocal()), 'ImportedBy': 'StatusSetByDefault'}, 'ResponseMetadata': {'RequestId': '7c33d6f9-……………..-3b202961e3e7', 'HTTPStatusCode': 200, 'HTTPHeaders': {'date': 'Sun, 21 Feb 2021 05:40:06 GMT', 'content-type': 'application/x-amz-json-1.1', 'content-length': '102', 'connection': 'keep-alive', 'x-amzn-requestid': '7c33d6f9-…….…………-3b202961e3e7'}, 'RetryAttempts': 0}}