Home > Mobile >  How to check if file exists in Google Cloud Storage with the gcloud bash command?
How to check if file exists in Google Cloud Storage with the gcloud bash command?

Time:11-24

I need to check if a file exists in a gitlab deployment pipeline. How to do it efficiently and reliably?

CodePudding user response:

Use gsutil ls gs://bucket/object-name and check the return value for 0.

If the object does not exist, the return value is 1.

CodePudding user response:

You can add the following Shell script in a Gitlab job :

#!/usr/bin/env bash

set -o pipefail
set -u

gsutil -q stat gs://your_bucket/folder/your_file.csv

PATH_EXIST=$?
if [ ${PATH_EXIST} -eq 0 ]; then
  echo "Exist"
else
  echo "Not Exist"
fi

I used gcloud cli and gsutil with stat command with -q option.

In this case, if the file exists the command returns 0 otherwise 1.

  • Related