Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions packages/google-cloud-storage/google/cloud/storage/blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Comment thread
guillaume-rochette-oxb marked this conversation as resolved.

@classmethod
def from_string(cls, uri, client=None):
Expand Down
20 changes: 20 additions & 0 deletions packages/google-cloud-storage/google/cloud/storage/bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Comment thread
guillaume-rochette-oxb marked this conversation as resolved.

@classmethod
def from_string(cls, uri, client=None):
Expand Down
11 changes: 11 additions & 0 deletions packages/google-cloud-storage/tests/unit/test_blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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):
Expand Down
10 changes: 10 additions & 0 deletions packages/google-cloud-storage/tests/unit/test_bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down
Loading