Created
April 13, 2020 15:23
-
-
Save warrenronsiek/76315b32c35fdf56e9491c79f0dcf640 to your computer and use it in GitHub Desktop.
Recursively count root directories in an S3 bucket with assumed role in python
This file contains hidden or 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
| import boto3 | |
| from collections import Counter | |
| BUCKET = '<YOUR BUCKET NAME>' | |
| ROLE = '<YOUR ROLE TO ASSUME>' | |
| ITEM_LIMIT = 10 # you probably want this to be at least 1 | |
| sts = boto3.client('sts') | |
| creds = sts.assume_role(RoleArn=ROLE, RoleSessionName='list-s3') | |
| access_key = creds['Credentials']['AccessKeyId'] | |
| secret_key = creds['Credentials']['SecretAccessKey'] | |
| session_token = creds['Credentials']['SessionToken'] | |
| s3 = boto3.client('s3', aws_access_key_id=access_key, aws_secret_access_key=secret_key, aws_session_token=session_token) | |
| def recursive_list(continuation_token="tkn", objects=Counter()): | |
| if objects == Counter(): | |
| resp = s3.list_objects_v2(Bucket=BUCKET) | |
| else: | |
| resp = s3.list_objects_v2(Bucket=BUCKET, ContinuationToken=continuation_token) | |
| keys = [obj['Key'] for obj in resp['Contents']] | |
| roots = [key.split('/')[0] for key in keys] | |
| root_count = Counter(roots) | |
| if resp['IsTruncated']: | |
| return recursive_list(resp['NextContinuationToken'], objects + root_count) | |
| return objects | |
| cnt = recursive_list() | |
| for (key, val) in cnt.items(): | |
| if val <= ITEM_LIMIT: | |
| print(f"root {key} contains {val} objects") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment