如何使用 Boto3 在指定的 AWS 秘密中添加标签
问题陈述:使用Python中的boto3库在AWSSecret中添加标签。
解决这个问题的方法/算法
第一步:导入boto3和botocore异常处理异常。
步骤2:secret_location和tags_dict是该函数中的必需参数。tags_dict应该是{“key”:”value”,..}
步骤3:使用boto3lib创建AWS会话。确保在默认配置文件中提到region_name。如果未提及,则在创建会话时显式传递region_name。
步骤4:为secretmanager创建一个AWS客户端。
第5步:现在使用tag_resource函数并将参数secret_location作为SecretId和tags_dict作为标签传递。
第6步:它返回响应元数据并在资源中添加标签。
第7步:如果在添加标签时出现问题,则处理通用异常。
示例代码
使用以下代码添加标签-
import boto3
frombotocore.exceptionsimport ClientError
def add_tags_in_resource(secret_location, tags_dict)
session = boto3.session.Session()
client = session.client('secretmanager')
try:
response = client.tag_resource(SecretId= secret_location,Tags=tags_dict)
return response
except ClientError as e:
raise Exception("boto3 client error in add_tags_in_resource: " + e.__str__())
except Exception as e:
raise Exception("Unexpected error in add_tags_in_resource: " + e.__str__())
tags_dict = [{"Key":"secret-test","Value":"test"}]
print(add_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}}热门推荐
10 八一幼儿祝福语大全简短
11 公司乔迁食堂祝福语简短
12 婚礼结束聚餐祝福语简短
13 儿媳买车妈妈祝福语简短
14 毕业送礼老师祝福语简短
15 同事辞职正常祝福语简短
16 恭贺新婚文案祝福语简短
17 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短