import os, uuid from google.cloud import storage JSON_CREDENTIALS_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)),"config/be1a_gcs_config.json") _Google_HEAD = 'gs://' os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = JSON_CREDENTIALS_PATH def is_google_path(path): return path.startswith(_Google_HEAD) def parse_google_file(url): container_and_path = url.removeprefix(_Google_HEAD) return container_and_path.split("/", 1) def build_google_url(container_name, path): return f'{_Google_HEAD}{container_name}/{path}' def get_google_target_blob_file_list(url_prefix): try: bucket_path = parse_google_file(url_prefix) bucket_name = bucket_path[0] path_prefix = bucket_path[1] storage_client = storage.Client.from_service_account_json(JSON_CREDENTIALS_PATH) blob_list = list(storage_client.list_blobs(bucket_name, prefix=path_prefix)) # print(f"in total {len(blob_list)} blob files found") res = [] for blob in blob_list: file_name = blob.name if file_name.startswith(path_prefix): res.append(build_google_url(bucket_name, file_name)) except Exception as ex: print('Exception:') print(ex) os._exit(1) print(f'in total {len(res)} files found') return res