|
@@ -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)
|