Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 15 additions & 20 deletions scripts/set-bucket-acls.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,32 @@

DRY_RUN=${DRY_RUN:-0}

BASEDIR=$(dirname "${0}")
BASEDIR=$(dirname "$0")
NOT_PROCESSED_BUCKETS_FILE="${BASEDIR}/not-processed-buckets.txt"

BUCKETS="put-here-the-first-bucket \
put-here-the-second-bucket"
BUCKETS="put-here-the-first-bucket put-here-the-second-bucket"

echo -n "" >"${NOT_PROCESSED_BUCKETS_FILE}"
for BUCKET in ${BUCKETS}; do
: >"$NOT_PROCESSED_BUCKETS_FILE"

for BUCKET in $BUCKETS; do
Comment on lines +11 to +15
Copy link

Copilot AI Jun 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Consider handling bucket names safely by either quoting the variable or using an array if bucket names might include spaces.

Copilot uses AI. Check for mistakes.
echo "Check for bucket '${BUCKET}'"
if ! gsutil ls -d "gs://${BUCKET}/public" >/dev/null 2>&1 || ! gsutil ls -d "gs://${BUCKET}/private" >/dev/null 2>&1; then
if ! gcloud storage ls "gs://${BUCKET}/public/" >/dev/null 2>&1 ||
! gcloud storage ls "gs://${BUCKET}/private/" >/dev/null 2>&1; then
echo "Bucket '${BUCKET}' not processed"
echo "${BUCKET}" >>"${NOT_PROCESSED_BUCKETS_FILE}"
echo "${BUCKET}" >>"$NOT_PROCESSED_BUCKETS_FILE"
continue
fi

echo "Bucket '${BUCKET}' has public and private folders. Setting ACLs..."
if [ "${DRY_RUN}" -ne "0" ]; then
echo "Exec the dry run commands..."
echo "DRY RUN: gsutil -m acl set private gs://${BUCKET}/*"
echo "DRY RUN: gsutil -m acl set -r public-read gs://${BUCKET}/public/**"
echo "DRY RUN: gsutil -m acl set -r private gs://${BUCKET}/private/**"
echo "End of dry run commands."
if [[ "$DRY_RUN" -ne 0 ]]; then
echo "DRY RUN: gcloud storage objects update --recursive --predefined-acl=private gs://${BUCKET}/*"
echo "DRY RUN: gcloud storage objects update --recursive --predefined-acl=publicRead gs://${BUCKET}/public/**"
echo "DRY RUN: gcloud storage objects update --recursive --predefined-acl=private gs://${BUCKET}/private/**"
continue
fi

echo "Exec the real commands..."
# This gsutil command is useful to set the private ACL to the root level objects.
gsutil -m acl set private "gs://${BUCKET}/*"
# Set public-read ACL to all objects inside the public folder.
gsutil -m acl set -r public-read "gs://${BUCKET}/public/**"
# Set private ACL to all objects inside the private folder.
gsutil -m acl set -r private "gs://${BUCKET}/private/**"
gcloud storage objects update --recursive --predefined-acl=private "gs://${BUCKET}/*"
gcloud storage objects update --recursive --predefined-acl=publicRead "gs://${BUCKET}/public/**"
gcloud storage objects update --recursive --predefined-acl=private "gs://${BUCKET}/private/**"
echo "Bucket '${BUCKET}' processed."
done