Skip to content

Commit

Permalink
Return url for file in GCP public bucket instead of error
Browse files Browse the repository at this point in the history
  • Loading branch information
dreadatour committed Dec 25, 2024
1 parent 60256d6 commit 4c7b667
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/datachain/client/gcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,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:
if "you need a private key to sign credentials" in str(exc):
return f"https://storage.googleapis.com/{self.name}/{path}"
raise

@staticmethod
def parse_timestamp(timestamp: str) -> datetime:
"""
Expand Down
6 changes: 6 additions & 0 deletions tests/unit/test_client_gcs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from datachain.client import Client


def test_anon_url():
client = Client.get_client("gs://foo", None, anon=True)
assert client.url("bar") == "https://storage.googleapis.com/foo/bar"

0 comments on commit 4c7b667

Please sign in to comment.