diff --git a/packages/google-cloud-storage/google/cloud/storage/blob.py b/packages/google-cloud-storage/google/cloud/storage/blob.py index c6fbcf4c12b7..05a87f07d492 100644 --- a/packages/google-cloud-storage/google/cloud/storage/blob.py +++ b/packages/google-cloud-storage/google/cloud/storage/blob.py @@ -421,6 +421,24 @@ 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. + + .. 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 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..13a35c094bb3 100644 --- a/packages/google-cloud-storage/google/cloud/storage/bucket.py +++ b/packages/google-cloud-storage/google/cloud/storage/bucket.py @@ -815,6 +815,26 @@ 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. + + .. 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. + """ + if self.name is None: + raise ValueError("Bucket name must be set to generate a URI.") + return f"gs://{self.name}" @classmethod def from_string(cls, uri, client=None): diff --git a/packages/google-cloud-storage/tests/unit/test_blob.py b/packages/google-cloud-storage/tests/unit/test_blob.py index a218f011dd17..a379a986eefb 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,15 @@ 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) + + 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 76c0eb5104c0..2655406ff0e1 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,15 @@ 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) + + 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):