需求:从帐户中查找未加密的s3存储桶,并向其添加标记。
到目前为止,实现了
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']))
结果:
Bucket with no server-side encryption: xyz1
Bucket with no server-side encryption: xyz2需要对后续的支持,我可以获得未加密的s3存储桶列表,但不确定如何使用except python代码的输出,以及如何使用未加密的桶名来添加标记。
发布于 2022-05-31 15:05:25
如果在尝试捕获之外声明列表,则可以在稍后访问它。
例如。
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)https://stackoverflow.com/questions/72449847
复制相似问题