Skip to content

Instantly share code, notes, and snippets.

@shaposhnikoff
Created October 15, 2023 08:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shaposhnikoff/4fdba8b56d14d7e6f50663a8f82c3c22 to your computer and use it in GitHub Desktop.
Save shaposhnikoff/4fdba8b56d14d7e6f50663a8f82c3c22 to your computer and use it in GitHub Desktop.
AWS S3 bucket policy script
import boto3
import json,re
bucket_name_regexp = '.*-data-.*'
def list_all_s3_buckets_and_acls():
s3_client = boto3.client('s3')
response = s3_client.list_buckets()
for bucket in response['Buckets']:
if re.match(bucket_name_regexp , bucket['Name']):
bucket_name = bucket['Name']
print(bucket_name)
# Print Bucket Policy
try:
bucket_policy = s3_client.get_bucket_policy(Bucket=bucket_name)
if 'Policy' in bucket_policy:
policy = json.loads(bucket_policy['Policy'])
print('Bucket Policy:', policy)
except:
print('No Bucket Policy')
# Print ACLs
acl_response = s3_client.get_bucket_acl(Bucket=bucket_name)
for acl in acl_response['Grants']:
permission = acl['Permission']
grantee = acl['Grantee']
print('Permission:', permission, 'Grantee:', grantee)
print('Owner:', acl_response['Owner'])
print('------------------')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment