Home > Blockchain >  get only the list of S3 buckets that are having bucket policy
get only the list of S3 buckets that are having bucket policy

Time:02-01

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.

CodePudding user response:

Unfortunately, you will have to retrieve the policy for each bucket one by one and do the filtering yourself:

import boto3

if __name__ == "__main__":

    client = boto3.client("s3", region_name='us-east-2')
    bucket_list = client.list_buckets()
    buckets_with_policies = []

    for bucket in map(lambda b: b['Name'], bucket_list['Buckets']):
        try:
            print(f'Trying to retrieve policy for bucket {bucket}...')
            policy = client.get_bucket_policy(Bucket=bucket)
            buckets_with_policies.append(bucket)
        except client.exceptions.from_code('NoSuchBucketPolicy'):
            print(f'No policy for {bucket}')

    print(f'Buckets with policies: {buckets_with_policies}')
  •  Tags:  
  • Related