This script grants read access to all users on a specified Google Cloud Storage bucket and its contents, provided a bucket name is given as input.
npm run import -- "add public permissions to google cloud storage"
if [[ -n $1 ]]; \
then gsutil defacl ch -u AllUsers:R "$1" && \
gsutil acl ch -u AllUsers:R "$1/**"; \
fi;
#!/bin/bash
# Check if a bucket path is provided as a command-line argument
if [[ -n "$1" ]]; then
# Define a function to configure public read access
configure_public_access() {
# Remove any leading or trailing slashes from the bucket path
bucket_path=${1#"${1%%[![:space:]]*}"}
bucket_path=${bucket_path%"${bucket_path##*[![:space:]]}"}
# Configure default ACL to grant read access to AllUsers
gsutil defacl ch -u AllUsers:R "${bucket_path}"
# Configure ACL for all objects within the bucket to grant read access to AllUsers
gsutil acl ch -u AllUsers:R "${bucket_path}/**"
}
# Call the function to configure public access
configure_public_access
fi
This code snippet uses the gsutil
command-line tool to set read access for all users on a specified Google Cloud Storage bucket and its contents.
Here's a breakdown:
Conditional Check:
if [[ -n $1 ]];
checks if the first command-line argument ($1
) is not empty.Access Control:
then gsutil defacl ch -u AllUsers:R "$1" && gsutil acl ch -u AllUsers:R "$1/**";
executes two gsutil
commands if the condition is true:
gsutil defacl ch -u AllUsers:R "$1"
sets the default access control list (ACL) for the bucket ($1
) to grant read access (R
) to all users (AllUsers
).gsutil acl ch -u AllUsers:R "$1/**"
applies the same read access to all objects (*
) within the bucket.End of Block:
fi
marks the end of the if
statement block.In essence, this script grants read access to all users on a specified Google Cloud Storage bucket and its contents, but only if a bucket name is provided as input.