首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从零开始使用S3向新创建的boto3桶添加标记(最初还没有标记)?

如何从零开始使用S3向新创建的boto3桶添加标记(最初还没有标记)?
EN

Stack Overflow用户
提问于 2022-07-08 17:34:31
回答 1查看 164关注 0票数 0

我想将标记添加到s3桶、新创建的和使用pythonboto3的现有存储桶中。

代码语言:javascript
复制
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进行管理,则必须添加新的标记(而不删除现有的标记)。为此,我如何修改此代码?

产出:

代码语言:javascript
复制
=================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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-07-08 20:20:22

当没有标记时,我捕获抛出的NoSuchTagSet错误,从而使它正常工作。然后使用默认值创建TagSet。

代码语言:javascript
复制
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)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72915031

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档