'get only the list of S3 buckets that are having bucket policy

I started out with the below boto script:

import boto3
import pprint

from botocore.exceptions import ClientError

boto3.setup_default_session(profile_name='test_terra_profile')

client = boto3.client("s3", region_name='US-EAST-1')

response = client.list_buckets()

print("Listing Amazon S3 Buckets ")

for bucket in response['Buckets']:
    print(f"-- {bucket['Name']}")

I am getting the whole list of s3 buckets and many of them are not having bucket policies. I am new to python and boto and not sure how to retrieve only S3's with bucket policies? Is there a way to filter the records with null bucket policies? Any suggestions will be appreciated.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source