Skip to content

Instantly share code, notes, and snippets.

@AstroTom
Last active March 8, 2021 09:50
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 AstroTom/e3f334e0490fdeaefa1636974702b829 to your computer and use it in GitHub Desktop.
Save AstroTom/e3f334e0490fdeaefa1636974702b829 to your computer and use it in GitHub Desktop.
Change the ownership of anonymously owned objects in my Amazon S3 bucket
#!/bin/bash
#
# Change the ownership of anonymously owned objects in my Amazon S3 bucket
# see https://aws.amazon.com/premiumsupport/knowledge-center/s3-object-change-anonymous-ownership/
#
# The -r option passed to read command prevents backslash escapes from being interpreted.
# Add IFS= option before read command to prevent leading/trailing whitespace from being trimmed
#
echo 'NOTE: You can ignore the error:
An error occurred (AccessDenied) when calling the PutObjectAcl operation: Access Denied
'
sleep 5
BUCKET=XXX # replce with your bucket name
KEY= # Init to null
#
# Generate list of keys and pipe it to 'while read KEY'
#
aws s3 ls s3://$BUCKET/ --recursive --output text |awk '{print $4}' |egrep -v '.*/$' |\
while IFS= read -r KEY
do
aws s3api put-object-acl --bucket $BUCKET --key "$KEY" --acl bucket-owner-full-control --no-sign-request
aws s3 cp s3://$BUCKET/$KEY s3://$BUCKET/"$KEY" --metadata-directive REPLACE
# test
#aws s3 cp s3://$BUCKET/$KEY .
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment