Boto3 和 S3:在循环遍历列表中 200 个 S3 桶中的 6 个后完成循环

Boto3 and S3: For loop finishing after iterating through only 6 out of 200 S3 buckets in the list

我正在尝试在列表中列出并记录所有 S3 存储桶 PublicAccessBlockConfiguration。问题是,循环完成并打印 'No Public Access' 在迭代了 ~200 个桶中的 6 个之后。

我静态地测试了代码,写入了我知道存在且具有 PublicAccessBlockConfiguration 的存储桶的名称,并且它有效。

但是在遍历列表时,没有出现相同的桶。这是为什么?

def check_bucket_access_block():
    try:
        for bucket in filtered_buckets:
            response = s3client.get_public_access_block(Bucket=bucket['Name'])
            for key, value in response['PublicAccessBlockConfiguration'].items():
                logger.info('Bucket Name: {}, {}: {}'.format(bucket['Name'], key, value))
    except botocore.exceptions.ClientError as e:
        if e.response['Error']['Code'] == 'NoSuchPublicAccessBlockConfiguration':
            print('\t no Public Access')
        else:
            print("unexpected error: %s" % (e.response))

check_bucket_access_block()

您的 try/except 块在 for 循环的 外部 。因此当产生错误时,退出循环。

尝试将 try/except 放在 for 循环中,像这样:

def check_bucket_access_block():
    for bucket in filtered_buckets:
        try:
            response = s3client.get_public_access_block(Bucket=bucket['Name'])
            for key, value in response['PublicAccessBlockConfiguration'].items():
                logger.info('Bucket Name: {}, {}: {}'.format(bucket['Name'], key, value))
        except botocore.exceptions.ClientError as e:
            if e.response['Error']['Code'] == 'NoSuchPublicAccessBlockConfiguration':
                print('\t no Public Access')
            else:
                print("unexpected error: %s" % (e.response))