Google Cloud Storage Bucket creation
Asked Answered
T

4

7

I want to create a google bucket if it doesn't exist. Otherwise, I want to reuse the bucket name. How to do it? Its equivalent of the unix command

mkdir -p dir_name

I used the command but my shell script crashes when I run this next time.

gsutil mb -l ASIA gs://my_bucket_name_blah_blah

Tours answered 31/10, 2015 at 13:2 Comment(0)
B
15

You could check for the existence of the bucket first. I think something like this would work:

gsutil ls -b gs://my_bucket_name_blah_blah || gsutil mb -l ASIA gs://my_bucket_name_blah_blah

Since the first command will return a 0 error code if the bucket already exists, the second command will only be executed if the bucket does not exist.

Note however that the first command will also return a non-zero exit code in the case of errors (transient error or permission denied). So you might need a more robust way of creating buckets.

Bastien answered 2/11, 2015 at 16:48 Comment(0)
L
2

Following BoHuang answer I could came up with this:

# This function create a bucket
function create_bucket {

#PROJECT=$1
#BUCKET=$2
#REGION=$3
echo "Creating bucket [$2] in region [$3] for project [$1]"

if ! gsutil ls -p $1 gs://$2 &> /dev/null;
    then
        echo creating gs://$2 ... ;
        gsutil mb -p $1 -c regional -l $3 gs://$2;
        sleep 5;
    else
        echo "Bucket $2 already exists!"
        echo "Please revise the bucket and delete manually or rerun the code"
        exit 1
fi

}

Lope answered 20/6, 2022 at 9:30 Comment(0)
P
1

After six years ....

I found this GitLab script can use as reference to solve this problem

The key part is

if ! gsutil ls -p ${GCP_PROJECT_ID} gs://${BUCKET} &> /dev/null; \
    then \
        echo creating gs://${BUCKET} ... ; \
        gsutil mb -p ${GCP_PROJECT_ID} -c regional -l ${GCP_REGION} gs://${BUCKET}; \
        sleep 10; \
    fi

That's a block of Makefile, but it's almost same as a shell script

Perreault answered 22/5, 2022 at 10:21 Comment(5)
I checked the above code block, it does not workPerreault
It does work nicely.Lope
It does not work.Perreault
I just followed your code and It worked for me. To be honest I just create a shell function to use it but the base is just your posted code :) Hope it helps. What is exactly the error you found? Which version of gcloud SDK are you using? I'd like to replicate itLope
Nice to hear it works on your side. I have solved the problem with another method.Perreault
Z
0

You can relay on gcloud also

function ensure_gcp_bucket_exists {
  echo "Running "ensure_gcp_bucket_exists
  BUCKET_NAME="gs://test-bucket"
  if gcloud storage buckets describe "${BUCKET_NAME}" &> /dev/null ; then
     "GCP Bucket ${BUCKET_NAME} already exists"
  else
    echo "Creating GCP Bucket ${BUCKET_NAME}"
    gcloud storage buckets create "${BUCKET_NAME}" 
  fi
}
Zabaglione answered 7/12, 2023 at 15:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.