如何使用Boto3从AWS Glue资源中删除标签
在本文中,我们将看到如何从AWS Glue资源中删除标签。
更多Python相关文章,请阅读:Python 教程
例子
从AWS Glue数据库中删除标签“ glue-db: tests ”。
问题描述: 使用Python中的boto3库删除AWS Glue资源中的标签。
解决该问题的方法/算法
- 第1步: 导入 boto3 和 botocore 异常以处理异常。
-
第2步: 此函数中的必需参数是 resource_arn 和 tags_list 。
resource_arn 的格式应如下-
| Catalog | arn:aws:glue:region:account-id:catalog |
|---|---|
| Database | arn:aws:glue:region:account-id:database/ database name |
| 表 | arn:aws:glue:region:account-id:table/ database name/ table name |
| 连接 | arn:aws:glue:region:account-id:connection/ connection name |
| 爬虫 | arn:aws:glue:region:account-id:crawler/ crawler-name |
| 任务 | arn:aws:glue:region:account-id:job/ job-name |
| 触发器 | arn:aws:glue:region:account-id:trigger/ trigger-name |
| 开发端点 | arn:aws:glue:region:account-id:devEndpoint/ development-endpoint-name |
| 机器学习变换 | arn:aws:glue:region:account-id:mlTransform/ transform-id |
tags_list 应该是[“key1,key2…]
- 第3步: 使用 boto3 库创建AWS会话。请确保在默认配置文件中提到了 region_name 。如果没有提到,则在创建会话时显式传递 region_name 。
-
第4步: 为 箠胶 创建AWS客户端。
-
第5步: 现在使用 untag_resource 函数,并将 resource_arn 作为ResourceArn传递,并将 tags_list 作为TagsToRemove传递。
-
第6步: 它返回响应元数据并删除资源中的标签。
-
第7步: 如果删除标签时出现错误,请处理通用异常。
例子代码
使用以下代码删除标签-
import boto3
from botocore.exceptions import ClientError
def remove_tags_in_resource(resource_arn, tags_list)
session = boto3.session.Session()
glue_client = session.client('glue')
try:
response = glue_client.untag_resource(ResourceArn=resource_arn, TagsToRemove=tags_list)
return response
except ClientError as e:
raise Exception("boto3 client error in remove_tags_in_resource: " + e.__str__())
except Exception as e:
raise Exception("Unexpected error in remove_tags_in_resource: " + e.__str__())
tags_list = ["glue-db"]
print(remove_tags_in_resource("arn:aws:glue:us-east-1:1122225*****88:database/test- db",tags_list))
输出
{'ResponseMetadata': {'RequestId': 'c9f418b0-***************-fb96', 'HTTPStatusCode': 200, 'HTTPHeaders': {'date': 'Fri, 02 Apr 2021 08:04:54 GMT', 'content-type': 'application/x-amz-json-1.1', 'content-length': '27', 'connection': 'keep-alive', 'x-amzn-requestid': 'c9f418b0-******************-fb96'}, 'RetryAttempts': 0}}
极客教程