from google.cloud import storage class GCSLoader: def __init__(self, bucket_name,project): self.bucket_name = bucket_name self.client = storage.Client(project) def get_file(self, filename): bucket = self.client.get_bucket(self.bucket_name) blob = storage.Blob(filename, bucket) return blob.download_as_text() def get_file_list(self, path): blobs = self.client.list_blobs(self.bucket_name, prefix=path) return [blob.name for blob in blobs] def file_exists(self, path): bucket = self.client.get_bucket(self.bucket_name) blob = storage.Blob(path, bucket) return blob.exists() def save_file(self, filename, content): raise Exception("GCS loader does not support save file") def upload_file(self, filename, gcs_key): bucket = self.client.get_bucket(self.bucket_name) blob = storage.Blob(gcs_key, bucket) blob.upload_from_filename(filename)