google_cloud_storage.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import base64
  2. import io
  3. import json
  4. from collections.abc import Generator
  5. from google.cloud import storage as google_cloud_storage
  6. from configs import dify_config
  7. from extensions.storage.base_storage import BaseStorage
  8. class GoogleCloudStorage(BaseStorage):
  9. """Implementation for Google Cloud storage."""
  10. def __init__(self):
  11. super().__init__()
  12. self.bucket_name = dify_config.GOOGLE_STORAGE_BUCKET_NAME
  13. service_account_json_str = dify_config.GOOGLE_STORAGE_SERVICE_ACCOUNT_JSON_BASE64
  14. # if service_account_json_str is empty, use Application Default Credentials
  15. if service_account_json_str:
  16. service_account_json = base64.b64decode(service_account_json_str).decode("utf-8")
  17. # convert str to object
  18. service_account_obj = json.loads(service_account_json)
  19. self.client = google_cloud_storage.Client.from_service_account_info(service_account_obj)
  20. else:
  21. self.client = google_cloud_storage.Client()
  22. def save(self, filename, data):
  23. bucket = self.client.get_bucket(self.bucket_name)
  24. blob = bucket.blob(filename)
  25. with io.BytesIO(data) as stream:
  26. blob.upload_from_file(stream)
  27. def load_once(self, filename: str) -> bytes:
  28. bucket = self.client.get_bucket(self.bucket_name)
  29. blob = bucket.get_blob(filename)
  30. data = blob.download_as_bytes()
  31. return data
  32. def load_stream(self, filename: str) -> Generator:
  33. def generate(filename: str = filename) -> Generator:
  34. bucket = self.client.get_bucket(self.bucket_name)
  35. blob = bucket.get_blob(filename)
  36. with blob.open(mode="rb") as blob_stream:
  37. while chunk := blob_stream.read(4096):
  38. yield chunk
  39. return generate()
  40. def download(self, filename, target_filepath):
  41. bucket = self.client.get_bucket(self.bucket_name)
  42. blob = bucket.get_blob(filename)
  43. blob.download_to_filename(target_filepath)
  44. def exists(self, filename):
  45. bucket = self.client.get_bucket(self.bucket_name)
  46. blob = bucket.blob(filename)
  47. return blob.exists()
  48. def delete(self, filename):
  49. bucket = self.client.get_bucket(self.bucket_name)
  50. bucket.delete_blob(filename)