Skip to content

Commit

Permalink
use a different cred for signing blobs from gcs
Browse files Browse the repository at this point in the history
  • Loading branch information
nwaughachukwuma committed Nov 19, 2024
1 parent 9d28f79 commit 16e88aa
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions api/src/services/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from typing import Any, Dict
from uuid import uuid4

from google.auth import compute_engine, default
from google.auth.transport import requests
from google.cloud import storage
from pydub import AudioSegment

Expand Down Expand Up @@ -100,8 +102,24 @@ def get_signed_url(self, blobname, expiration=datetime.timedelta(days=1)):
if not blob.exists():
raise Exception(f"Blob {blobname} does not exist")

return blob.generate_signed_url(
version="v4",
expiration=expiration,
method="GET",
)
if os.environ.get("ENV", "dev") == "prod":
credentials, _ = default()
auth_request = requests.Request()
credentials.refresh(auth_request)

signing_credentials = compute_engine.IDTokenCredentials(
auth_request, "", service_account_email=credentials.service_account_email
)

return blob.generate_signed_url(
version="v4",
expiration=expiration,
method="GET",
credentials=signing_credentials,
)
else:
return blob.generate_signed_url(
version="v4",
expiration=expiration,
method="GET",
)

0 comments on commit 16e88aa

Please sign in to comment.