Created
July 4, 2019 16:21
-
-
Save alangixxer/8af66d0b857cda8cc19e4fdbf1ea289e to your computer and use it in GitHub Desktop.
python lambda function to detect empty s3 buckets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
import json | |
import boto3 | |
s3 = boto3.client('s3') | |
# creates a generator for files in bucket, this is a fast approach is not reading each object name. | |
def keys(bucket_name, prefix='/', delimiter='/'): | |
prefix = prefix[1:] if prefix.startswith(delimiter) else prefix | |
bucket = boto3.resource('s3').Bucket(bucket_name) | |
return (_.key for _ in bucket.objects.filter(Prefix=prefix)) | |
def lambda_handler(event, context): | |
# Call S3 to list current buckets | |
response = s3.list_buckets() | |
# Get a list of all bucket names from the response | |
buckets = [bucket['Name'] for bucket in response['Buckets']] | |
# Print out the bucket list | |
print(f"Bucket List: {buckets}") | |
for bucket in buckets: | |
_exhausted = object() | |
try: | |
if next(keys(bucket), _exhausted) == _exhausted: | |
print(f"{bucket}: generator is empty") | |
except Exception as e: | |
print(f"Error: {bucket} - {e}") | |
return { | |
'body': json.dumps(f"Scanned {len(buckets)} buckets") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment