Google Cloud Storage Bucket Check if File exists

Today in this article we shall see how to verify if files exist in the Google storage bucket programmatically.

We shall use python code to achieve this using Google Cloud Client Libraries for google-cloud-storage.

Please make sure to install google-cloud-storage Python packages using cloud shell or using requirmetns.txt or using setup.py .

pip3 install google-cloud-storage

OR

requirements.txt

Below is sample requirements.txt file,

 google-cloud-storage==1.28.1
 pandas==1.2.2

OR

import setuptools

REQUIRED_PACKAGES = [
    'google-cloud-storage==1.35.0',
    'pandas==1.2.2',
    'gcsfs==0.7.1'
]

PACKAGE_NAME = 'TheCodebuzzDataflow'
PACKAGE_VERSION = '0.0.1'

setuptools.setup(
    name=PACKAGE_NAME,
    version=PACKAGE_VERSION,
    description='Set up file for the required Dataflow packages ',
    install_requires=REQUIRED_PACKAGES,
    packages=setuptools.find_packages(),
)

Please use the below namespace to your python code,

from google.cloud import storage

Here is sample implementation for getting the status on if blob exist or not.


    def process(self, something):
        input_path = 'Input/input.json'   
        storage_client = storage.Client()
        bucket_name = 'test-bucket-13'
        bucket = storage_client.bucket(bucket_name)
        stats = storage.Blob(bucket=bucket, name= input_path).exists(storage_client)
        logging.info(stats)

Above we have used storage.Blob method which takes below inputs,

  • name = The name of the blob. This name should include the path of the blob as well.
  • bucket = The name of bucket to which blob belongs.

Google Bucket Check if File exists

Do you have any comments or ideas or any better suggestions to share?

Please sound off your comments below.

Happy Coding !!



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.



Leave a Reply

Your email address will not be published. Required fields are marked *