Last active
July 20, 2023 02:17
-
-
Save innovia/218a8214a2a94286ff9e8dd690940960 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python | |
import argparse | |
import sys | |
import boto3 | |
client = boto3.client('s3') | |
def main(args): | |
bucket = args.bucket | |
prefix = args.prefix | |
kwargs = {'Bucket': bucket, 'Prefix': prefix} | |
failures = [] | |
while_true = True | |
while while_true: | |
resp = client.list_objects_v2(**kwargs) | |
for obj in resp['Contents']: | |
try: | |
print(obj['Key']) | |
set_acl(bucket=bucket, key=obj['Key']) | |
kwargs['ContinuationToken'] = resp['NextContinuationToken'] | |
except KeyError: | |
while_true = False | |
except Exception: | |
failures.append(obj["Key"]) | |
continue | |
print "failures :", failures | |
def set_acl(bucket, key): | |
client.put_object_acl( | |
GrantFullControl="id=%s" % get_account_canonical_id, | |
Bucket=bucket, | |
Key=key | |
) | |
def get_account_canonical_id(): | |
return client.list_buckets()["Owner"]["ID"] | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser( | |
description="Set S3 ACL on bucket to current AWS account owner" | |
) | |
parser.add_argument( | |
"--bucket", | |
help="<required> S3 Bucket name.", | |
required=True | |
) | |
parser.add_argument( | |
"--prefix", | |
help="<required> S3 prefix to set permissions recursively on.", | |
required=True | |
) | |
main(parser.parse_args()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you for this! It gave me the starting point I needed.