How to check if file exists in Google Cloud Storage?
It's as easy as use the exists method within a blob object:
from google.cloud import storage
def blob_exists(projectname, credentials, bucket_name, filename):
client = storage.Client(projectname, credentials=credentials)
bucket = client.get_bucket(bucket_name)
blob = bucket.blob(filename)
return blob.exists()
This post is old, you can actually now check if a file exists on GCP using the blob class, but because it took me a while to find an answer, adding here for the others who are looking for a solution
from google.cloud import storage
name = 'file_i_want_to_check.txt'
storage_client = storage.Client()
bucket_name = 'my_bucket_name'
bucket = storage_client.bucket(bucket_name)
stats = storage.Blob(bucket=bucket, name=name).exists(storage_client)
Documentation is here
Hope this helps!
Edit
As per the comment by @om-prakash, if the file is in a folder, then the name should include the path to the file:
name = "folder/path_to/file_i_want_to_check.txt"