pythongoogle-cloud-storagefile-exists

How to check if file exists in Google Cloud Storage?


I have a script where I want to check if a file exists in a bucket and if it doesn't then create one.

I tried using os.path.exists(file_path) where file_path = "/gs/testbucket", but I got a file not found error.

I know that I can use the files.listdir() API function to list all the files located at a path and then check if the file I want is one of them. But I was wondering whether there is another way to check whether the file exists.


Solution

  • I guess there is no function to check directly if the file exists given its path.
    I have created a function that uses the files.listdir() API function to list all the files in the bucket and match it against the file name that we want. It returns true if found and false if not.