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: LEAP-860: have GCS generate data URLs if presign is disabled #5968

Merged
merged 5 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions label_studio/io_storages/gcs/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ def get_data(self, key):
def generate_http_url(self, url):
return GCS.generate_http_url(
url=url,
presign=self.presign,
google_application_credentials=self.google_application_credentials,
google_project_id=self.google_project_id,
presign_ttl=self.presign_ttl,
Expand Down
15 changes: 12 additions & 3 deletions label_studio/io_storages/gcs/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,15 @@
def generate_http_url(
cls,
url: str,
presign: bool,
google_application_credentials: Union[str, dict] = None,
google_project_id: str = None,
presign_ttl: int = 1,
) -> str:
"""
Gets gs:// like URI string and returns presigned https:// URL
:param url: input URI
:param presign: Whether to generate presigned URL. If false, will generate base64 encoded data URL
:param google_application_credentials:
:param google_project_id:
:param presign_ttl: Presign TTL in minutes
Expand Down Expand Up @@ -211,17 +213,24 @@
if settings.GCS_CLOUD_STORAGE_FORCE_DEFAULT_CREDENTIALS:
# google_application_credentials has higher priority,
# use Application Default Credentials (ADC) when google_application_credentials is empty only
kwargs = {} if google_application_credentials else cls._get_default_credentials()
maybe_credentials = {} if google_application_credentials else cls._get_default_credentials()
maybe_client = None if google_application_credentials else cls.get_client()

Check warning on line 217 in label_studio/io_storages/gcs/utils.py

View check run for this annotation

Codecov / codecov/patch

label_studio/io_storages/gcs/utils.py#L216-L217

Added lines #L216 - L217 were not covered by tests
else:
kwargs = {}
maybe_credentials = {}
maybe_client = None

if not presign:
blob.reload(client=maybe_client) # needed to know the content type
blob_bytes = blob.download_as_bytes(client=maybe_client)
return f'data:{blob.content_type};base64,{base64.b64encode(blob_bytes).decode("utf-8")}'

Check warning on line 225 in label_studio/io_storages/gcs/utils.py

View check run for this annotation

Codecov / codecov/patch

label_studio/io_storages/gcs/utils.py#L223-L225

Added lines #L223 - L225 were not covered by tests

url = blob.generate_signed_url(
version='v4',
# This URL is valid for 15 minutes
expiration=timedelta(minutes=presign_ttl),
# Allow GET requests using this URL.
method='GET',
**kwargs,
**maybe_credentials,
)

logger.debug('Generated GCS signed url: ' + url)
Expand Down
Loading