How to find a file with specific file name pattern in GCS bucket using Python

25 Views Asked by At

I have a bucket in GCS which has a folder for every date.

For eg., today I will have a folder with name '2024-03-25'

This folder will have multiple files with a filename suffixed with timestamp.

I am trying to get a particular file of .csv format irrespective of the timestamp in the name.

For eg., file name : 'mytestfile20240321094533.csv'

I would like to have a code that searches for a file starting with mytestfile20240321*.csv.

from google.oauth2 import service_account
from google.cloud import storage
service_account_file = 'mypath/serviceaccount.json'
credentials = service_account.Credentials.from_service_account_file(service_account_file)
gcs_client = storage.Client(credentials=credentials)
bucket = gcs_client.get_bucket('mybucketname')
f_name = 'mytestfile20240321*.csv'
blob = bucket.blob(f_name)
if blob.exists() == True:
    print('file found')
else:
    print('file unavailable')

Any help is appreciated!

0

There are 0 best solutions below