首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用python除了输出(AWS )

如何使用python除了输出(AWS )
EN

Stack Overflow用户
提问于 2022-05-31 14:42:04
回答 1查看 39关注 0票数 0

需求:从帐户中查找未加密的s3存储桶,并向其添加标记。

到目前为止,实现了

代码语言:javascript
复制
import boto3
from botocore.exceptions import ClientError

# Retrieve the list of existing buckets
s3 = boto3.client('s3')
response = s3.list_buckets()


# Find out unencrypted bucket list
for bucket in response['Buckets']:
    try:
        enc = s3.get_bucket_encryption(Bucket=bucket["Name"])
    except ClientError as e:
        if e.response['Error']['Code'] == 'ServerSideEncryptionConfigurationNotFoundError':
            print('Bucket with no server-side encryption: %s' % (bucket['Name']))
        else:
            print("Bucket with unexpected error: %s, unexpected error: %s" % (bucket['Name'], e))

下面一行给出了未加密的存储列表:print('Bucket with no server-side encryption: %s' % (bucket['Name']))

结果:

代码语言:javascript
复制
Bucket with no server-side encryption: xyz1
Bucket with no server-side encryption: xyz2

需要对后续的支持,我可以获得未加密的s3存储桶列表,但不确定如何使用except python代码的输出,以及如何使用未加密的桶名来添加标记。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-05-31 15:05:25

如果在尝试捕获之外声明列表,则可以在稍后访问它。

例如。

代码语言:javascript
复制
import boto3
from botocore.exceptions import ClientError

#this is our new list
buckets = []

# Retrieve the list of existing buckets
s3 = boto3.client('s3')
response = s3.list_buckets()


# Find out unencrypted bucket list
for bucket in response['Buckets']:
    try:
        enc = s3.get_bucket_encryption(Bucket=bucket["Name"])
    except ClientError as e:
        if e.response['Error']['Code'] == 'ServerSideEncryptionConfigurationNotFoundError':
            #add the bucket name to our new list
            buckets.append(bucket['Name'])

            print('Bucket with no server-side encryption: %s' % (bucket['Name']))
        else:
            print("Bucket with unexpected error: %s, unexpected error: %s" % (bucket['Name'], e))
    

#now you can use the "buckets" variable and it will contain all the unencrypted buckets
for bucket in buckets:
    print(bucket)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72449847

复制
相关文章

相似问题

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