From c996e5071cad1b8dd2ddea8f870b24f9de8947d6 Mon Sep 17 00:00:00 2001 From: Vladimir Rudnykh Date: Fri, 27 Dec 2024 18:30:58 +0700 Subject: [PATCH] Return url for file in GCP public bucket instead of error (#754) --- src/datachain/client/gcs.py | 15 ++++++++------- tests/unit/test_client_gcs.py | 13 +------------ 2 files changed, 9 insertions(+), 19 deletions(-) diff --git a/src/datachain/client/gcs.py b/src/datachain/client/gcs.py index 6eaaf1c67..e79e68fa1 100644 --- a/src/datachain/client/gcs.py +++ b/src/datachain/client/gcs.py @@ -33,13 +33,14 @@ def create_fs(cls, **kwargs) -> GCSFileSystem: return cast(GCSFileSystem, super().create_fs(**kwargs)) def url(self, path: str, expires: int = 3600, **kwargs) -> str: - try: - return self.fs.sign(self.get_full_path(path), expiration=expires, **kwargs) - except AttributeError as exc: - is_anon = self.fs.storage_options.get("token") == "anon" - if is_anon and "you need a private key to sign credentials" in str(exc): - return f"https://storage.googleapis.com/{self.name}/{path}" - raise + """ + Generate a signed URL for the given path. + If the client is anonymous, a public URL is returned instead + (see https://cloud.google.com/storage/docs/access-public-data#api-link). + """ + if self.fs.storage_options.get("token") == "anon": + return f"https://storage.googleapis.com/{self.name}/{path}" + return self.fs.sign(self.get_full_path(path), expiration=expires, **kwargs) @staticmethod def parse_timestamp(timestamp: str) -> datetime: diff --git a/tests/unit/test_client_gcs.py b/tests/unit/test_client_gcs.py index 25a023a9d..8c80db786 100644 --- a/tests/unit/test_client_gcs.py +++ b/tests/unit/test_client_gcs.py @@ -1,17 +1,6 @@ from datachain.client import Client -def test_anon_url(mocker): - def sign(*args, **kwargs): - raise AttributeError( - "you need a private key to sign credentials." - "the credentials you are currently using" - " just contains a token." - " see https://googleapis.dev/python/google-api-core/latest/auth.html" - "#setting-up-a-service-account for more details." - ) - - mocker.patch("gcsfs.GCSFileSystem.sign", side_effect=sign) - +def test_anon_url(): client = Client.get_client("gs://foo", None, anon=True) assert client.url("bar") == "https://storage.googleapis.com/foo/bar"