瀏覽代碼

Fix: setup google-storage client (#4296)

Co-authored-by: kotamat <kota1681@gmail.com>
Co-authored-by: crazywoola <427733928@qq.com>
Kota Matsumoto 11 月之前
父節點
當前提交
e2a78888b9
共有 2 個文件被更改,包括 12 次插入4 次删除
  1. 10 4
      api/extensions/storage/google_storage.py
  2. 2 0
      docker/docker-compose.yaml

+ 10 - 4
api/extensions/storage/google_storage.py

@@ -1,4 +1,5 @@
 import base64
+import io
 from collections.abc import Generator
 from contextlib import closing
 
@@ -15,14 +16,19 @@ class GoogleStorage(BaseStorage):
         super().__init__(app)
         app_config = self.app.config
         self.bucket_name = app_config.get('GOOGLE_STORAGE_BUCKET_NAME')
-        service_account_json = base64.b64decode(app_config.get('GOOGLE_STORAGE_SERVICE_ACCOUNT_JSON_BASE64')).decode(
-            'utf-8')
-        self.client = GoogleCloudStorage.Client().from_service_account_json(service_account_json)
+        service_account_json_str = app_config.get('GOOGLE_STORAGE_SERVICE_ACCOUNT_JSON_BASE64')
+        # if service_account_json_str is empty, use Application Default Credentials
+        if service_account_json_str:
+            service_account_json = base64.b64decode(service_account_json_str).decode('utf-8')
+            self.client = GoogleCloudStorage.Client.from_service_account_info(service_account_json)
+        else:
+            self.client = GoogleCloudStorage.Client()
 
     def save(self, filename, data):
         bucket = self.client.get_bucket(self.bucket_name)
         blob = bucket.blob(filename)
-        blob.upload_from_file(data)
+        with io.BytesIO(data) as stream:
+            blob.upload_from_file(stream)
 
     def load_once(self, filename: str) -> bytes:
         bucket = self.client.get_bucket(self.bucket_name)

+ 2 - 0
docker/docker-compose.yaml

@@ -88,6 +88,7 @@ services:
       AZURE_BLOB_ACCOUNT_URL: 'https://<your_account_name>.blob.core.windows.net'
       # The Google storage configurations, only available when STORAGE_TYPE is `google-storage`.
       GOOGLE_STORAGE_BUCKET_NAME: 'yout-bucket-name'
+      # if you want to use Application Default Credentials, you can leave GOOGLE_STORAGE_SERVICE_ACCOUNT_JSON_BASE64 empty.
       GOOGLE_STORAGE_SERVICE_ACCOUNT_JSON_BASE64: 'your-google-service-account-json-base64-string'
       # The type of vector store to use. Supported values are `weaviate`, `qdrant`, `milvus`, `relyt`.
       VECTOR_STORE: weaviate
@@ -226,6 +227,7 @@ services:
       AZURE_BLOB_ACCOUNT_URL: 'https://<your_account_name>.blob.core.windows.net'
       # The Google storage configurations, only available when STORAGE_TYPE is `google-storage`.
       GOOGLE_STORAGE_BUCKET_NAME: 'yout-bucket-name'
+      # if you want to use Application Default Credentials, you can leave GOOGLE_STORAGE_SERVICE_ACCOUNT_JSON_BASE64 empty.
       GOOGLE_STORAGE_SERVICE_ACCOUNT_JSON_BASE64: 'your-google-service-account-json-base64-string'
       # The type of vector store to use. Supported values are `weaviate`, `qdrant`, `milvus`, `relyt`, `pgvector`.
       VECTOR_STORE: weaviate