Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: google storage init with sa and download #5054

Merged
merged 3 commits into from
Jun 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions api/extensions/storage/google_storage.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import base64
import io
import json
from collections.abc import Generator
from contextlib import closing

Expand All @@ -20,7 +21,9 @@ def __init__(self, app: Flask):
# 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)
# convert str to object
service_account_obj = json.loads(service_account_json)
self.client = GoogleCloudStorage.Client.from_service_account_info(service_account_obj)
else:
self.client = GoogleCloudStorage.Client()

Expand Down Expand Up @@ -48,9 +51,7 @@ def generate(filename: str = filename) -> Generator:
def download(self, filename, target_filepath):
bucket = self.client.get_bucket(self.bucket_name)
blob = bucket.get_blob(filename)
with open(target_filepath, "wb") as my_blob:
blob_data = blob.download_blob()
blob_data.readinto(my_blob)
blob.download_to_filename(target_filepath)

def exists(self, filename):
bucket = self.client.get_bucket(self.bucket_name)
Expand Down