bashgoogle-cloud-platformgcloudgoogle-bucket

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


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


Solution

  • This answer evolved from the answer of Mazlum Tosun. Because I think it is a substantial improvement with less lines and no global settings switching it needs to be a separate answer.

    Ideally the answer would be something like this

    - gsutil stat $BUCKET_PATH
    - if [ $? -eq 0 ]; then  
      ... # do if file exists
      else
      ... # do if file does not exists
      fi
    

    $? stores the exit_status of the previous command. 0 if success. This works fine in a local console. The problem with Gitlab will be that if the file does not exists, then "gsutil stat $BUCKET_PATH" will produce a non-zero exit code and the whole pipeline will stop at that line with an error. We need to catch the error, while still storing the exit code.

    We will use the or operator || to suppress the error. FILE_EXISTS=false will only be executed if gsutil stat fails.

    - gsutil stat $BUCKET_PATH || FILE_EXISTS=false
    - if [ "$FILE_EXISTS" = false ]; then 
      ... # do stuff if file does not exist
      else
      ... # do stuff if file exists
      fi
    

    Also we can use the -q flag to let the command stats be silent if that is desired.

    - gsutil -q stat $BUCKET_PATH || FILE_EXISTS=false