Skip to content

Commit ae917ee

Browse files
authored
fix: Changed name of methods Blob.from_string() and Bucket.from_string() to from_uri() (#1335)
1 parent 9845591 commit ae917ee

File tree

6 files changed

+130
-18
lines changed

6 files changed

+130
-18
lines changed

google/cloud/storage/blob.py

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,10 @@
136136
"Blob.download_as_string() is deprecated and will be removed in future. "
137137
"Use Blob.download_as_bytes() instead."
138138
)
139+
_FROM_STRING_DEPRECATED = (
140+
"Blob.from_string() is deprecated and will be removed in future. "
141+
"Use Blob.from_uri() instead."
142+
)
139143
_GS_URL_REGEX_PATTERN = re.compile(
140144
r"(?P<scheme>gs)://(?P<bucket_name>[a-z0-9_.-]+)/(?P<object_name>.+)"
141145
)
@@ -388,15 +392,15 @@ def public_url(self):
388392
)
389393

390394
@classmethod
391-
def from_string(cls, uri, client=None):
395+
def from_uri(cls, uri, client=None):
392396
"""Get a constructor for blob object by URI.
393397
394398
.. code-block:: python
395399
396400
from google.cloud import storage
397401
from google.cloud.storage.blob import Blob
398402
client = storage.Client()
399-
blob = Blob.from_string("gs://bucket/object", client=client)
403+
blob = Blob.from_uri("gs://bucket/object", client=client)
400404
401405
:type uri: str
402406
:param uri: The blob uri following a gs://bucket/object pattern.
@@ -418,6 +422,35 @@ def from_string(cls, uri, client=None):
418422
bucket = Bucket(client, name=match.group("bucket_name"))
419423
return cls(match.group("object_name"), bucket)
420424

425+
@classmethod
426+
def from_string(cls, uri, client=None):
427+
"""(Deprecated) Get a constructor for blob object by URI.
428+
429+
.. note::
430+
Deprecated alias for :meth:`from_uri`.
431+
432+
.. code-block:: python
433+
434+
from google.cloud import storage
435+
from google.cloud.storage.blob import Blob
436+
client = storage.Client()
437+
blob = Blob.from_string("gs://bucket/object", client=client)
438+
439+
:type uri: str
440+
:param uri: The blob uri following a gs://bucket/object pattern.
441+
Both a bucket and object name is required to construct a blob object.
442+
443+
:type client: :class:`~google.cloud.storage.client.Client`
444+
:param client:
445+
(Optional) The client to use. Application code should
446+
*always* pass ``client``.
447+
448+
:rtype: :class:`google.cloud.storage.blob.Blob`
449+
:returns: The blob object created.
450+
"""
451+
warnings.warn(_FROM_STRING_DEPRECATED, PendingDeprecationWarning, stacklevel=2)
452+
return Blob.from_uri(uri=uri, client=client)
453+
421454
def generate_signed_url(
422455
self,
423456
expiration=None,

google/cloud/storage/bucket.py

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@
8484
"valid before the bucket is created. Instead, pass the location "
8585
"to `Bucket.create`."
8686
)
87+
_FROM_STRING_MESSAGE = (
88+
"Bucket.from_string() is deprecated. " "Use Bucket.from_uri() instead."
89+
)
8790

8891

8992
def _blobs_page_start(iterator, page, response):
@@ -726,15 +729,15 @@ def user_project(self):
726729
return self._user_project
727730

728731
@classmethod
729-
def from_string(cls, uri, client=None):
732+
def from_uri(cls, uri, client=None):
730733
"""Get a constructor for bucket object by URI.
731734
732735
.. code-block:: python
733736
734737
from google.cloud import storage
735738
from google.cloud.storage.bucket import Bucket
736739
client = storage.Client()
737-
bucket = Bucket.from_string("gs://bucket", client=client)
740+
bucket = Bucket.from_uri("gs://bucket", client=client)
738741
739742
:type uri: str
740743
:param uri: The bucket uri pass to get bucket object.
@@ -754,6 +757,34 @@ def from_string(cls, uri, client=None):
754757

755758
return cls(client, name=netloc)
756759

760+
@classmethod
761+
def from_string(cls, uri, client=None):
762+
"""Get a constructor for bucket object by URI.
763+
764+
.. note::
765+
Deprecated alias for :meth:`from_uri`.
766+
767+
.. code-block:: python
768+
769+
from google.cloud import storage
770+
from google.cloud.storage.bucket import Bucket
771+
client = storage.Client()
772+
bucket = Bucket.from_string("gs://bucket", client=client)
773+
774+
:type uri: str
775+
:param uri: The bucket uri pass to get bucket object.
776+
777+
:type client: :class:`~google.cloud.storage.client.Client` or
778+
``NoneType``
779+
:param client: (Optional) The client to use. Application code should
780+
*always* pass ``client``.
781+
782+
:rtype: :class:`google.cloud.storage.bucket.Bucket`
783+
:returns: The bucket object created.
784+
"""
785+
warnings.warn(_FROM_STRING_MESSAGE, PendingDeprecationWarning, stacklevel=2)
786+
return Bucket.from_uri(uri=uri, client=client)
787+
757788
def blob(
758789
self,
759790
blob_name,

google/cloud/storage/client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1148,7 +1148,7 @@ def download_blob_to_file(
11481148
"""
11491149

11501150
if not isinstance(blob_or_uri, Blob):
1151-
blob_or_uri = Blob.from_string(blob_or_uri)
1151+
blob_or_uri = Blob.from_uri(blob_or_uri)
11521152

11531153
blob_or_uri._prep_and_do_download(
11541154
file_obj,

tests/unit/test_blob.py

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5902,46 +5902,74 @@ def test_soft_hard_delte_time_unset(self):
59025902
self.assertIsNone(blob.soft_delete_time)
59035903
self.assertIsNone(blob.hard_delete_time)
59045904

5905-
def test_from_string_w_valid_uri(self):
5905+
def test_from_uri_w_valid_uri(self):
59065906
from google.cloud.storage.blob import Blob
59075907

59085908
client = self._make_client()
59095909
basic_uri = "gs://bucket_name/b"
5910-
blob = Blob.from_string(basic_uri, client)
5910+
blob = Blob.from_uri(basic_uri, client)
59115911

59125912
self.assertIsInstance(blob, Blob)
59135913
self.assertIs(blob.client, client)
59145914
self.assertEqual(blob.name, "b")
59155915
self.assertEqual(blob.bucket.name, "bucket_name")
59165916

59175917
nested_uri = "gs://bucket_name/path1/path2/b#name"
5918-
blob = Blob.from_string(nested_uri, client)
5918+
blob = Blob.from_uri(nested_uri, client)
59195919

59205920
self.assertIsInstance(blob, Blob)
59215921
self.assertIs(blob.client, client)
59225922
self.assertEqual(blob.name, "path1/path2/b#name")
59235923
self.assertEqual(blob.bucket.name, "bucket_name")
59245924

5925-
def test_from_string_w_invalid_uri(self):
5925+
def test_from_uri_w_invalid_uri(self):
59265926
from google.cloud.storage.blob import Blob
59275927

59285928
client = self._make_client()
59295929

59305930
with pytest.raises(ValueError):
5931-
Blob.from_string("http://bucket_name/b", client)
5931+
Blob.from_uri("http://bucket_name/b", client)
59325932

5933-
def test_from_string_w_domain_name_bucket(self):
5933+
def test_from_uri_w_domain_name_bucket(self):
59345934
from google.cloud.storage.blob import Blob
59355935

59365936
client = self._make_client()
59375937
uri = "gs://buckets.example.com/b"
5938-
blob = Blob.from_string(uri, client)
5938+
blob = Blob.from_uri(uri, client)
59395939

59405940
self.assertIsInstance(blob, Blob)
59415941
self.assertIs(blob.client, client)
59425942
self.assertEqual(blob.name, "b")
59435943
self.assertEqual(blob.bucket.name, "buckets.example.com")
59445944

5945+
@mock.patch("warnings.warn")
5946+
def test_from_string(self, mock_warn):
5947+
from google.cloud.storage.blob import _FROM_STRING_DEPRECATED
5948+
from google.cloud.storage.blob import Blob
5949+
5950+
client = self._make_client()
5951+
basic_uri = "gs://bucket_name/b"
5952+
blob = Blob.from_string(basic_uri, client)
5953+
5954+
self.assertIsInstance(blob, Blob)
5955+
self.assertIs(blob.client, client)
5956+
self.assertEqual(blob.name, "b")
5957+
self.assertEqual(blob.bucket.name, "bucket_name")
5958+
5959+
nested_uri = "gs://bucket_name/path1/path2/b#name"
5960+
blob = Blob.from_string(nested_uri, client)
5961+
5962+
self.assertIsInstance(blob, Blob)
5963+
self.assertIs(blob.client, client)
5964+
self.assertEqual(blob.name, "path1/path2/b#name")
5965+
self.assertEqual(blob.bucket.name, "bucket_name")
5966+
5967+
mock_warn.assert_any_call(
5968+
_FROM_STRING_DEPRECATED,
5969+
PendingDeprecationWarning,
5970+
stacklevel=2,
5971+
)
5972+
59455973
def test_open(self):
59465974
from io import TextIOWrapper
59475975
from google.cloud.storage.fileio import BlobReader

tests/unit/test_bucket.py

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4325,39 +4325,59 @@ def _generate_signed_url_helper(
43254325
}
43264326
signer.assert_called_once_with(expected_creds, **expected_kwargs)
43274327

4328-
def test_get_bucket_from_string_w_valid_uri(self):
4328+
def test_get_bucket_from_uri_w_valid_uri(self):
43294329
from google.cloud.storage.bucket import Bucket
43304330

43314331
client = self._make_client()
43324332
BUCKET_NAME = "BUCKET_NAME"
43334333
uri = "gs://" + BUCKET_NAME
43344334

4335-
bucket = Bucket.from_string(uri, client)
4335+
bucket = Bucket.from_uri(uri, client)
43364336

43374337
self.assertIsInstance(bucket, Bucket)
43384338
self.assertIs(bucket.client, client)
43394339
self.assertEqual(bucket.name, BUCKET_NAME)
43404340

4341-
def test_get_bucket_from_string_w_invalid_uri(self):
4341+
def test_get_bucket_from_uri_w_invalid_uri(self):
43424342
from google.cloud.storage.bucket import Bucket
43434343

43444344
client = self._make_client()
43454345

43464346
with pytest.raises(ValueError, match="URI scheme must be gs"):
4347-
Bucket.from_string("http://bucket_name", client)
4347+
Bucket.from_uri("http://bucket_name", client)
43484348

4349-
def test_get_bucket_from_string_w_domain_name_bucket(self):
4349+
def test_get_bucket_from_uri_w_domain_name_bucket(self):
43504350
from google.cloud.storage.bucket import Bucket
43514351

43524352
client = self._make_client()
43534353
BUCKET_NAME = "buckets.example.com"
43544354
uri = "gs://" + BUCKET_NAME
43554355

4356+
bucket = Bucket.from_uri(uri, client)
4357+
4358+
self.assertIsInstance(bucket, Bucket)
4359+
self.assertIs(bucket.client, client)
4360+
self.assertEqual(bucket.name, BUCKET_NAME)
4361+
4362+
@mock.patch("warnings.warn")
4363+
def test_get_bucket_from_string(self, mock_warn):
4364+
from google.cloud.storage.bucket import _FROM_STRING_MESSAGE
4365+
from google.cloud.storage.bucket import Bucket
4366+
4367+
client = self._make_client()
4368+
BUCKET_NAME = "BUCKET_NAME"
4369+
uri = "gs://" + BUCKET_NAME
4370+
43564371
bucket = Bucket.from_string(uri, client)
43574372

43584373
self.assertIsInstance(bucket, Bucket)
43594374
self.assertIs(bucket.client, client)
43604375
self.assertEqual(bucket.name, BUCKET_NAME)
4376+
mock_warn.assert_any_call(
4377+
_FROM_STRING_MESSAGE,
4378+
PendingDeprecationWarning,
4379+
stacklevel=2,
4380+
)
43614381

43624382
def test_generate_signed_url_no_version_passed_warning(self):
43634383
self._generate_signed_url_helper()

tests/unit/test_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1784,7 +1784,7 @@ def test_download_blob_to_file_with_uri(self):
17841784
_helpers, "_get_invocation_id", return_value=GCCL_INVOCATION_TEST_CONST
17851785
):
17861786
with mock.patch(
1787-
"google.cloud.storage.client.Blob.from_string", return_value=blob
1787+
"google.cloud.storage.client.Blob.from_uri", return_value=blob
17881788
):
17891789
client.download_blob_to_file(
17901790
"gs://bucket_name/path/to/object", file_obj

0 commit comments

Comments
 (0)