From ff8295d9faf5d87d1284abcf10e36afeda222843 Mon Sep 17 00:00:00 2001 From: guillaume-rochette-oxb <129938647+guillaume-rochette-oxb@users.noreply.github.com> Date: Thu, 23 Apr 2026 16:09:51 +0100 Subject: [PATCH 1/5] Adding `uri` property to both Bucket and Blob such that for roundtrip consistency with the class method `from_uri`. --- packages/google-cloud-storage/google/cloud/storage/blob.py | 4 ++++ packages/google-cloud-storage/google/cloud/storage/bucket.py | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/packages/google-cloud-storage/google/cloud/storage/blob.py b/packages/google-cloud-storage/google/cloud/storage/blob.py index c6fbcf4c12b7..8c48f3517253 100644 --- a/packages/google-cloud-storage/google/cloud/storage/blob.py +++ b/packages/google-cloud-storage/google/cloud/storage/blob.py @@ -421,6 +421,10 @@ def from_uri(cls, uri, client=None): raise ValueError("URI pattern must be gs://bucket/object") bucket = Bucket(client, name=match.group("bucket_name")) return cls(match.group("object_name"), bucket) + + @property + def uri(self) -> str: + return f"{self.bucket.uri}/{self.name}" @classmethod def from_string(cls, uri, client=None): diff --git a/packages/google-cloud-storage/google/cloud/storage/bucket.py b/packages/google-cloud-storage/google/cloud/storage/bucket.py index 6fd690cf38b2..d164be94a151 100644 --- a/packages/google-cloud-storage/google/cloud/storage/bucket.py +++ b/packages/google-cloud-storage/google/cloud/storage/bucket.py @@ -815,6 +815,10 @@ def from_uri(cls, uri, client=None): raise ValueError("URI scheme must be gs") return cls(client, name=netloc) + + @property + def uri(self) -> str: + return f"gs://{self.name}" @classmethod def from_string(cls, uri, client=None): From c7050bee9db917c49f7d2a81be3602b900bfcecd Mon Sep 17 00:00:00 2001 From: guillaume-rochette-oxb <129938647+guillaume-rochette-oxb@users.noreply.github.com> Date: Thu, 23 Apr 2026 16:15:58 +0100 Subject: [PATCH 2/5] Updated tests to maintain coverage. --- packages/google-cloud-storage/tests/unit/test_blob.py | 3 +++ packages/google-cloud-storage/tests/unit/test_bucket.py | 2 ++ 2 files changed, 5 insertions(+) diff --git a/packages/google-cloud-storage/tests/unit/test_blob.py b/packages/google-cloud-storage/tests/unit/test_blob.py index a218f011dd17..b757cc1dd0b7 100644 --- a/packages/google-cloud-storage/tests/unit/test_blob.py +++ b/packages/google-cloud-storage/tests/unit/test_blob.py @@ -6003,6 +6003,7 @@ def test_from_uri_w_valid_uri(self): self.assertIs(blob.client, client) self.assertEqual(blob.name, "b") self.assertEqual(blob.bucket.name, "bucket_name") + self.assertEqual(blob.uri, basic_uri) nested_uri = "gs://bucket_name/path1/path2/b#name" blob = Blob.from_uri(nested_uri, client) @@ -6011,6 +6012,7 @@ def test_from_uri_w_valid_uri(self): self.assertIs(blob.client, client) self.assertEqual(blob.name, "path1/path2/b#name") self.assertEqual(blob.bucket.name, "bucket_name") + self.assertEqual(blob.uri, nested_uri) def test_from_uri_w_invalid_uri(self): from google.cloud.storage.blob import Blob @@ -6031,6 +6033,7 @@ def test_from_uri_w_domain_name_bucket(self): self.assertIs(blob.client, client) self.assertEqual(blob.name, "b") self.assertEqual(blob.bucket.name, "buckets.example.com") + self.assertEqual(blob.uri, uri) @mock.patch("warnings.warn") def test_from_string(self, mock_warn): diff --git a/packages/google-cloud-storage/tests/unit/test_bucket.py b/packages/google-cloud-storage/tests/unit/test_bucket.py index 76c0eb5104c0..5f6bfbad907a 100644 --- a/packages/google-cloud-storage/tests/unit/test_bucket.py +++ b/packages/google-cloud-storage/tests/unit/test_bucket.py @@ -4554,6 +4554,7 @@ def test_get_bucket_from_uri_w_valid_uri(self): self.assertIsInstance(bucket, Bucket) self.assertIs(bucket.client, client) self.assertEqual(bucket.name, BUCKET_NAME) + self.assertEqual(bucket.uri, uri) def test_get_bucket_from_uri_w_invalid_uri(self): from google.cloud.storage.bucket import Bucket @@ -4575,6 +4576,7 @@ def test_get_bucket_from_uri_w_domain_name_bucket(self): self.assertIsInstance(bucket, Bucket) self.assertIs(bucket.client, client) self.assertEqual(bucket.name, BUCKET_NAME) + self.assertEqual(bucket.uri, uri) @mock.patch("warnings.warn") def test_get_bucket_from_string(self, mock_warn): From 868edbeaa6cbb922d2b1771039715be959ea03ba Mon Sep 17 00:00:00 2001 From: guillaume-rochette-oxb <129938647+guillaume-rochette-oxb@users.noreply.github.com> Date: Thu, 23 Apr 2026 16:22:20 +0100 Subject: [PATCH 3/5] Added missing docstrings. --- .../google/cloud/storage/blob.py | 14 ++++++++++++++ .../google/cloud/storage/bucket.py | 14 ++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/packages/google-cloud-storage/google/cloud/storage/blob.py b/packages/google-cloud-storage/google/cloud/storage/blob.py index 8c48f3517253..05a87f07d492 100644 --- a/packages/google-cloud-storage/google/cloud/storage/blob.py +++ b/packages/google-cloud-storage/google/cloud/storage/blob.py @@ -424,6 +424,20 @@ def from_uri(cls, uri, client=None): @property def uri(self) -> str: + """Get the URI associated to the blob object. + + .. code-block:: python + + from google.cloud import storage + from google.cloud.storage.blob import Blob + client = storage.Client() + uri = "gs://bucket/object" + blob = Blob.from_uri(uri, client=client) + assert blob.uri == uri + + :rtype: str + :returns: The blob uri. + """ return f"{self.bucket.uri}/{self.name}" @classmethod diff --git a/packages/google-cloud-storage/google/cloud/storage/bucket.py b/packages/google-cloud-storage/google/cloud/storage/bucket.py index d164be94a151..b814df3fe3c5 100644 --- a/packages/google-cloud-storage/google/cloud/storage/bucket.py +++ b/packages/google-cloud-storage/google/cloud/storage/bucket.py @@ -818,6 +818,20 @@ def from_uri(cls, uri, client=None): @property def uri(self) -> str: + """Get the URI associated to the bucket object. + + .. code-block:: python + + from google.cloud import storage + from google.cloud.storage.bucket import Bucket + client = storage.Client() + uri = "gs://bucket" + bucket = Bucket.from_uri(uri, client=client) + assert bucket.uri == uri + + :rtype: str + :returns: The bucket uri. + """ return f"gs://{self.name}" @classmethod From 411009c5f03a38eec0155d96f942466da39856a3 Mon Sep 17 00:00:00 2001 From: guillaume-rochette-oxb <129938647+guillaume-rochette-oxb@users.noreply.github.com> Date: Thu, 23 Apr 2026 16:36:32 +0100 Subject: [PATCH 4/5] Update packages/google-cloud-storage/google/cloud/storage/bucket.py Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> --- packages/google-cloud-storage/google/cloud/storage/bucket.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/google-cloud-storage/google/cloud/storage/bucket.py b/packages/google-cloud-storage/google/cloud/storage/bucket.py index b814df3fe3c5..13a35c094bb3 100644 --- a/packages/google-cloud-storage/google/cloud/storage/bucket.py +++ b/packages/google-cloud-storage/google/cloud/storage/bucket.py @@ -832,6 +832,8 @@ def uri(self) -> str: :rtype: str :returns: The bucket uri. """ + if self.name is None: + raise ValueError("Bucket name must be set to generate a URI.") return f"gs://{self.name}" @classmethod From 7f0bff508d10af039013d345d8113235d18e339d Mon Sep 17 00:00:00 2001 From: guillaume-rochette-oxb <129938647+guillaume-rochette-oxb@users.noreply.github.com> Date: Thu, 23 Apr 2026 16:56:34 +0100 Subject: [PATCH 5/5] Added test case, following Gemini's comments. --- packages/google-cloud-storage/tests/unit/test_blob.py | 8 ++++++++ packages/google-cloud-storage/tests/unit/test_bucket.py | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/packages/google-cloud-storage/tests/unit/test_blob.py b/packages/google-cloud-storage/tests/unit/test_blob.py index b757cc1dd0b7..a379a986eefb 100644 --- a/packages/google-cloud-storage/tests/unit/test_blob.py +++ b/packages/google-cloud-storage/tests/unit/test_blob.py @@ -6034,6 +6034,14 @@ def test_from_uri_w_domain_name_bucket(self): self.assertEqual(blob.name, "b") self.assertEqual(blob.bucket.name, "buckets.example.com") self.assertEqual(blob.uri, uri) + + def test_get_uri_from_blob_w_unset_bucket_name(self): + from google.cloud.storage.bucket import Bucket + + client = self._make_client() + blob = self._make_one(name="b", bucket=None) + with pytest.raises(ValueError, match="Bucket name must be set to generate a URI."): + blob.uri @mock.patch("warnings.warn") def test_from_string(self, mock_warn): diff --git a/packages/google-cloud-storage/tests/unit/test_bucket.py b/packages/google-cloud-storage/tests/unit/test_bucket.py index 5f6bfbad907a..2655406ff0e1 100644 --- a/packages/google-cloud-storage/tests/unit/test_bucket.py +++ b/packages/google-cloud-storage/tests/unit/test_bucket.py @@ -4577,6 +4577,14 @@ def test_get_bucket_from_uri_w_domain_name_bucket(self): self.assertIs(bucket.client, client) self.assertEqual(bucket.name, BUCKET_NAME) self.assertEqual(bucket.uri, uri) + + def test_get_uri_from_bucket_w_unset_bucket_name(self): + from google.cloud.storage.bucket import Bucket + + client = self._make_client() + bucket = self._make_one(name=None) + with pytest.raises(ValueError, match="Bucket name must be set to generate a URI."): + bucket.uri @mock.patch("warnings.warn") def test_get_bucket_from_string(self, mock_warn):