Skip to content

Instantly share code, notes, and snippets.

@innovia
Last active July 20, 2023 02:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save innovia/218a8214a2a94286ff9e8dd690940960 to your computer and use it in GitHub Desktop.
Save innovia/218a8214a2a94286ff9e8dd690940960 to your computer and use it in GitHub Desktop.
#!/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())
@thedoc31
Copy link

Thank you for this! It gave me the starting point I needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment