Đề bài − Sử dụng thư viện boto3 trong Python để xóa một crawler được tạo trong tài khoản.
Ví dụ − Xóa crawler 'Portfolio' được tạo trong tài khoản.
Cách tiếp cận/Thuật toán giải quyết
Bước 1 − Nhập boto3 và botocore exceptions để xử lý ngoại lệ.
Bước 2 − Truyền tham số crawler_name cần xóa từ AWS Glue Catalog.
Bước 3 − Tạo phiên AWS bằng boto3. Đảm bảo region_name trong profile mặc định. Nếu không, truyền rõ ràng region_name khi tạo phiên.
Bước 4 − Tạo client AWS cho glue.
Bước 5 − Sử dụng hàm delete_crawler và truyền crawler_name làm tham số Name.
Bước 6 − Nó sẽ xóa crawler và trả về response metadata.
Bước 7 − Xử lý ngoại lệ chung nếu có lỗi xảy ra khi kiểm tra job.
Ví dụ
Sử dụng code sau để xóa crawler từ AWS Glue Data Catalog:
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.delete_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"))
Kết quả
{'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}}