如何使用 Boto3 删除指定 AWS Secret 中的标签
问题陈述:使用Python中的boto3库删除AWSSecret中的标签。
解决这个问题的方法/算法
第一步:导入boto3和botocore异常处理异常。
步骤2:secret_location和tags_list是该函数中的必需参数。tags_list应该是要取消标记的键列表。
步骤3:使用boto3lib创建AWS会话。确保在默认配置文件中提到region_name。如果未提及,则在创建会话时显式传递region_name。
步骤4:为secretmanager创建一个AWS客户端。
第5步:现在使用untag_resource函数并将参数secret_location作为SecretId和tags_list作为TagKeys传递。
步骤6:它返回响应元数据并删除资源中的标签。
第7步:如果在删除标签时出现问题,则处理通用异常。
示例代码
使用以下代码删除标签-
import boto3
frombotocore.exceptionsimport ClientError
def remove_tags_in_resource(secret_location, tags_list)
session = boto3.session.Session()
client = session.client('secretmanager')
try:
response = client.untag_resource(SecretId= secret_location,TagKeys=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_dict = ["secret-test"]
print(remove_tags_in_resource("secrets/aws",tags_dict))输出结果{'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}}热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下