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/7] 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/7] 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/7] 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/7] 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/7] 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): From 296f6f14ed6a066755d2ad2264e7ccd416e78b0e Mon Sep 17 00:00:00 2001 From: Guillaume Rochette Date: Tue, 28 Apr 2026 16:12:56 +0100 Subject: [PATCH 6/7] Fixed linting. --- packages/google-cloud-storage/google/cloud/storage/blob.py | 4 ++-- .../google-cloud-storage/google/cloud/storage/bucket.py | 4 ++-- packages/google-cloud-storage/tests/unit/test_blob.py | 6 ++++-- packages/google-cloud-storage/tests/unit/test_bucket.py | 6 ++++-- 4 files changed, 12 insertions(+), 8 deletions(-) diff --git a/packages/google-cloud-storage/google/cloud/storage/blob.py b/packages/google-cloud-storage/google/cloud/storage/blob.py index 05a87f07d492..9adf75b32d60 100644 --- a/packages/google-cloud-storage/google/cloud/storage/blob.py +++ b/packages/google-cloud-storage/google/cloud/storage/blob.py @@ -421,7 +421,7 @@ 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: """Get the URI associated to the blob object. @@ -434,7 +434,7 @@ def uri(self) -> str: uri = "gs://bucket/object" blob = Blob.from_uri(uri, client=client) assert blob.uri == uri - + :rtype: str :returns: The blob uri. """ diff --git a/packages/google-cloud-storage/google/cloud/storage/bucket.py b/packages/google-cloud-storage/google/cloud/storage/bucket.py index 13a35c094bb3..af334c979da1 100644 --- a/packages/google-cloud-storage/google/cloud/storage/bucket.py +++ b/packages/google-cloud-storage/google/cloud/storage/bucket.py @@ -815,7 +815,7 @@ def from_uri(cls, uri, client=None): raise ValueError("URI scheme must be gs") return cls(client, name=netloc) - + @property def uri(self) -> str: """Get the URI associated to the bucket object. @@ -828,7 +828,7 @@ def uri(self) -> str: uri = "gs://bucket" bucket = Bucket.from_uri(uri, client=client) assert bucket.uri == uri - + :rtype: str :returns: The bucket uri. """ diff --git a/packages/google-cloud-storage/tests/unit/test_blob.py b/packages/google-cloud-storage/tests/unit/test_blob.py index a379a986eefb..9f0d82556605 100644 --- a/packages/google-cloud-storage/tests/unit/test_blob.py +++ b/packages/google-cloud-storage/tests/unit/test_blob.py @@ -6034,13 +6034,15 @@ 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."): + with pytest.raises( + ValueError, match="Bucket name must be set to generate a URI." + ): blob.uri @mock.patch("warnings.warn") diff --git a/packages/google-cloud-storage/tests/unit/test_bucket.py b/packages/google-cloud-storage/tests/unit/test_bucket.py index 2655406ff0e1..71404d6ecaa5 100644 --- a/packages/google-cloud-storage/tests/unit/test_bucket.py +++ b/packages/google-cloud-storage/tests/unit/test_bucket.py @@ -4577,13 +4577,15 @@ 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."): + with pytest.raises( + ValueError, match="Bucket name must be set to generate a URI." + ): bucket.uri @mock.patch("warnings.warn") From 59a8e3eda333c9a139aada2350a41a2a8ee59420 Mon Sep 17 00:00:00 2001 From: Guillaume Rochette Date: Tue, 28 Apr 2026 17:23:37 +0100 Subject: [PATCH 7/7] Fixed testing. --- packages/google-cloud-storage/google/cloud/storage/blob.py | 4 ++++ .../google-cloud-storage/google/cloud/storage/bucket.py | 2 ++ packages/google-cloud-storage/tests/unit/test_blob.py | 7 +------ packages/google-cloud-storage/tests/unit/test_bucket.py | 3 --- 4 files changed, 7 insertions(+), 9 deletions(-) diff --git a/packages/google-cloud-storage/google/cloud/storage/blob.py b/packages/google-cloud-storage/google/cloud/storage/blob.py index 9adf75b32d60..dc3aae9c833e 100644 --- a/packages/google-cloud-storage/google/cloud/storage/blob.py +++ b/packages/google-cloud-storage/google/cloud/storage/blob.py @@ -435,9 +435,13 @@ def uri(self) -> str: blob = Blob.from_uri(uri, client=client) assert blob.uri == uri + :raises :exc:`ValueError` if bucket is not set. :rtype: str :returns: The blob uri. """ + if self.bucket is None: + raise ValueError("Bucket must be set to generate a 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 af334c979da1..d2f12ce0c583 100644 --- a/packages/google-cloud-storage/google/cloud/storage/bucket.py +++ b/packages/google-cloud-storage/google/cloud/storage/bucket.py @@ -829,11 +829,13 @@ def uri(self) -> str: bucket = Bucket.from_uri(uri, client=client) assert bucket.uri == uri + :raises: :exc:`ValueError` if name is not set. :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 diff --git a/packages/google-cloud-storage/tests/unit/test_blob.py b/packages/google-cloud-storage/tests/unit/test_blob.py index 9f0d82556605..2744165050a6 100644 --- a/packages/google-cloud-storage/tests/unit/test_blob.py +++ b/packages/google-cloud-storage/tests/unit/test_blob.py @@ -6036,13 +6036,8 @@ def test_from_uri_w_domain_name_bucket(self): 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." - ): + with pytest.raises(ValueError, match="Bucket must be set to generate a URI."): blob.uri @mock.patch("warnings.warn") diff --git a/packages/google-cloud-storage/tests/unit/test_bucket.py b/packages/google-cloud-storage/tests/unit/test_bucket.py index 71404d6ecaa5..8cc2734c2974 100644 --- a/packages/google-cloud-storage/tests/unit/test_bucket.py +++ b/packages/google-cloud-storage/tests/unit/test_bucket.py @@ -4579,9 +4579,6 @@ def test_get_bucket_from_uri_w_domain_name_bucket(self): 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."