Skip to content

Instantly share code, notes, and snippets.

@stahnma
Created March 31, 2025 03:09
Show Gist options
  • Save stahnma/e5112d00ddd018a44a709b973198a6a8 to your computer and use it in GitHub Desktop.
Save stahnma/e5112d00ddd018a44a709b973198a6a8 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
set -euo pipefail
if ! command -v aws &> /dev/null; then
echo "Error: The 'aws' CLI is not installed or not in your PATH." >&2
exit 3
fi
BUCKET_NAME="${1:-}"
if [ -z "$BUCKET_NAME" ]; then
echo "Usage: $0 <bucket-name>"
exit 1
fi
read -r -p "Are you sure you want to make bucket '$BUCKET_NAME' publicly readable? (yes/no): " confirm
if [[ "$confirm" != "yes" ]]; then
echo "Aborting."
exit 2
fi
set -euo pipefail
aws s3api put-public-access-block \
--bucket "$BUCKET_NAME" \
--public-access-block-configuration "BlockPublicAcls=false,IgnorePublicAcls=false,BlockPublicPolicy=false,RestrictPublicBuckets=false"
policy_file=$(mktemp -t public-read-policy.json.XXXXXX)
cat <<EOF > "$policy_file"
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AllowPublicRead",
"Effect": "Allow",
"Principal": "*",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::$BUCKET_NAME/*"
}
]
}
EOF
aws s3api put-bucket-policy \
--bucket "$BUCKET_NAME" \
--policy file://"$policy_file"
rm -f "$policy_file"
echo "Bucket '$BUCKET_NAME' is now publicly readable."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment