google_cloud_storage.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 # type: ignore
  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: bytes = blob.download_as_bytes()
  31. return data
  32. def load_stream(self, filename: str) -> Generator:
  33. bucket = self.client.get_bucket(self.bucket_name)
  34. blob = bucket.get_blob(filename)
  35. with blob.open(mode="rb") as blob_stream:
  36. while chunk := blob_stream.read(4096):
  37. yield chunk
  38. def download(self, filename, target_filepath):
  39. bucket = self.client.get_bucket(self.bucket_name)
  40. blob = bucket.get_blob(filename)
  41. blob.download_to_filename(target_filepath)
  42. def exists(self, filename):
  43. bucket = self.client.get_bucket(self.bucket_name)
  44. blob = bucket.blob(filename)
  45. return blob.exists()
  46. def delete(self, filename):
  47. bucket = self.client.get_bucket(self.bucket_name)
  48. bucket.delete_blob(filename)