我想将标记添加到s3桶、新创建的和使用pythonboto3的现有存储桶中。
import boto3
from botocore.exceptions import ClientError
s3bucket=boto3.client("s3")
s3=boto3.resource('s3')
falcon_s3=False
print ("=================Adding tags to S3 Buckets==================")
list_bucket=s3bucket.list_buckets()
for buckets in list_bucket['Buckets']:
try:
Bucket_Existing_tags = s3.BucketTagging(buckets['Name']).tag_set
for tags in Bucket_Existing_tags:
if tags["Key"]=="Falcon Managed" and tags["Value"] =="true":
print("This Bucket is belonged to Falcons -->"+ buckets["Name"])
falcon_s3=True
break
else:
bucket_tagging = s3.BucketTagging(buckets['Name'])
tags = bucket_tagging.tag_set
tags.append({'Key':'Technical:Patcher', 'Value': 'goats'})
Set_Tag = bucket_tagging.put(Tagging={'TagSet':tags})
print('Tags added for ---> ' + buckets['Name'])
except ClientError as e:
print("Unexpected error: %s" % e)为此,我创建了这个脚本,它正在检查已经有标记的存储桶,并在其中检查是否管理了falocon。如果不是,它会放置另一个标记集。当重新运行该方法时,由于多次不允许添加相同的标记而无法工作(但这应该是可能的,因为我们使用其他资源)。但是这个脚本不适合最初没有标记的s3桶(新创建的标签)。
因此,我需要添加标签到所有的桶在这个特定的帐户(包括新的没有标签),如果它是猎鹰管理,它不需要添加任何标签。但是,如果不对falcon进行管理,则必须添加新的标记(而不删除现有的标记)。为此,我如何修改此代码?
产出:
=================Adding tags to S3 Buckets==================
Unexpected error: An error occurred (NoSuchTagSet) when calling the GetBucketTagging operation: The TagSet does not exist
Unexpected error: An error occurred (InvalidTag) when calling the PutBucketTagging operation: Cannot provide multiple Tags with the same key
This Bucket is belonged to Falcons -->test-1-test-tag-adder
Tags added for ---> test-2-test-tag-adder
Unexpected error: An error occurred (NoSuchTagSet) when calling the GetBucketTagging operation: The TagSet does not exist
Unexpected error: An error occurred (NoSuchTagSet) when calling the GetBucketTagging operation: The TagSet does not exist
Unexpected error: An error occurred (InvalidTag) when calling the PutBucketTagging operation: Cannot provide multiple Tags with the same key
Unexpected error: An error occurred (InvalidTag) when calling the PutBucketTagging operation: Cannot provide multiple Tags with the same key发布于 2022-07-08 20:20:22
当没有标记时,我捕获抛出的NoSuchTagSet错误,从而使它正常工作。然后使用默认值创建TagSet。
import boto3
from botocore.exceptions import ClientError
s3bucket = boto3.client("s3")
s3 = boto3.resource('s3')
falcon_s3 = False
print("=================Adding tags to S3 Buckets==================")
list_bucket = s3bucket.list_buckets()
for bucket in list_bucket:
try:
existing_bucket_tagging = s3.BucketTagging(bucket).tag_set
for tags in existing_bucket_tagging:
if tags["Key"] == "Falcon Managed" and tags["Value"] == "true":
print("This Bucket is belonged to Falcons -->" + bucket)
falcon_s3 = True
break
except ClientError as e:
if e.response['Error']['Code'] == 'NoSuchTagSet':
print(bucket + ",does not have tags, adding tag")
bucket_tagging = s3.BucketTagging(bucket)
bucket_tagging.put(
Tagging={
'TagSet': [
{
'Key': 'Technical:Patcher',
'Value': 'goats'
},
]
}
)
print('Tags added for ---> ' + bucket)
else:
print("Unexpected error: %s" % e)https://stackoverflow.com/questions/72915031
复制相似问题