如何使用Boto3从AWS Glue数据目录中删除爬虫?
问题陈述 - 使用Python中的boto3库删除已在您的帐户中创建的爬虫。
例子 - 删除已在您的帐户中创建的爬虫“Portfolio”。
更多Python相关文章,请阅读:Python 教程
解决此问题的方法/算法
步骤1 - 导入boto3和botocore异常以处理异常。
步骤2 - 传递应从AWS Glue目录中删除的爬虫名称参数。
步骤3 - 使用boto3库创建AWS会话。请确保在default profile中提到了region_name。如果没有提到,则在创建会话时显式传递region_name。
步骤4 - 为glue创建AWS客户端。
步骤5 - 现在使用delete_crawler函数并将爬虫名称作为Name参数传递。
步骤6 - 它将删除爬虫并返回响应元数据。
步骤7 - 如果在检查作业时出现问题,请处理通用异常。
例子
使用以下代码从AWS Glue数据目录中删除爬虫 –
import boto3
from botocore.exceptions import ClientError
def delete_a_crawler(crawler_name):
session = boto3.session.Session()
glue_client = session.client('glue')
try:
response = glue_client.delelte_crawler(Name=crawler_name)
return response
except ClientError as e:
raise Exception( "boto3 client error in delete_a_crawler: " + e.__str__())
except Exception as e:
raise Exception("Unexpected error in delete_a_crawler: " + e.__str__())
print(delete_a_crawler("Portfolio"))
输出
{'ResponseMetadata': {'RequestId': '067b667f-0a74d4f30a5b',
'HTTPStatusCode': 200, 'HTTPHeaders': {'date': 'Sat, 27 Feb 2021 14:54:30 GMT',
'content-type': 'application/x-amz-json-1.1', 'contentlength': '2', 'connection': 'keep-alive',
'x-amzn-requestid': '067b667f0a10-4f99-91be-0a74d4f30a5b'}, 'RetryAttempts': 0}}
极客教程