Skip to content

Commit 58c1d03

Browse files
holtskinnerandrewsg
authored andcommitted
fix: Changed name of methods Blob.from_string() and Bucket.from_string() to from_uri() (#1335)
1 parent df107d2 commit 58c1d03

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
@@ -137,6 +137,10 @@
137137
"Blob.download_as_string() is deprecated and will be removed in future. "
138138
"Use Blob.download_as_bytes() instead."
139139
)
140+
_FROM_STRING_DEPRECATED = (
141+
"Blob.from_string() is deprecated and will be removed in future. "
142+
"Use Blob.from_uri() instead."
143+
)
140144
_GS_URL_REGEX_PATTERN = re.compile(
141145
r"(?P<scheme>gs)://(?P<bucket_name>[a-z0-9_.-]+)/(?P<object_name>.+)"
142146
)
@@ -389,15 +393,15 @@ def public_url(self):
389393
)
390394

391395
@classmethod
392-
def from_string(cls, uri, client=None):
396+
def from_uri(cls, uri, client=None):
393397
"""Get a constructor for blob object by URI.
394398
395399
.. code-block:: python
396400
397401
from google.cloud import storage
398402
from google.cloud.storage.blob import Blob
399403
client = storage.Client()
400-
blob = Blob.from_string("gs://bucket/object", client=client)
404+
blob = Blob.from_uri("gs://bucket/object", client=client)
401405
402406
:type uri: str
403407
:param uri: The blob uri following a gs://bucket/object pattern.
@@ -419,6 +423,35 @@ def from_string(cls, uri, client=None):
419423
bucket = Bucket(client, name=match.group("bucket_name"))
420424
return cls(match.group("object_name"), bucket)
421425

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

google/cloud/storage/bucket.py

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

8992

9093
def _blobs_page_start(iterator, page, response):
@@ -778,15 +781,15 @@ def _query_params(self):
778781
return params
779782

780783
@classmethod
781-
def from_string(cls, uri, client=None):
784+
def from_uri(cls, uri, client=None):
782785
"""Get a constructor for bucket object by URI.
783786
784787
.. code-block:: python
785788
786789
from google.cloud import storage
787790
from google.cloud.storage.bucket import Bucket
788791
client = storage.Client()
789-
bucket = Bucket.from_string("gs://bucket", client=client)
792+
bucket = Bucket.from_uri("gs://bucket", client=client)
790793
791794
:type uri: str
792795
:param uri: The bucket uri pass to get bucket object.
@@ -806,6 +809,34 @@ def from_string(cls, uri, client=None):
806809

807810
return cls(client, name=netloc)
808811

812+
@classmethod
813+
def from_string(cls, uri, client=None):
814+
"""Get a constructor for bucket object by URI.
815+
816+
.. note::
817+
Deprecated alias for :meth:`from_uri`.
818+
819+
.. code-block:: python
820+
821+
from google.cloud import storage
822+
from google.cloud.storage.bucket import Bucket
823+
client = storage.Client()
824+
bucket = Bucket.from_string("gs://bucket", client=client)
825+
826+
:type uri: str
827+
:param uri: The bucket uri pass to get bucket object.
828+
829+
:type client: :class:`~google.cloud.storage.client.Client` or
830+
``NoneType``
831+
:param client: (Optional) The client to use. Application code should
832+
*always* pass ``client``.
833+
834+
:rtype: :class:`google.cloud.storage.bucket.Bucket`
835+
:returns: The bucket object created.
836+
"""
837+
warnings.warn(_FROM_STRING_MESSAGE, PendingDeprecationWarning, stacklevel=2)
838+
return Bucket.from_uri(uri=uri, client=client)
839+
809840
def blob(
810841
self,
811842
blob_name,

google/cloud/storage/client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1202,7 +1202,7 @@ def download_blob_to_file(
12021202
"""
12031203

12041204
if not isinstance(blob_or_uri, Blob):
1205-
blob_or_uri = Blob.from_string(blob_or_uri)
1205+
blob_or_uri = Blob.from_uri(blob_or_uri)
12061206

12071207
blob_or_uri._prep_and_do_download(
12081208
file_obj,

tests/unit/test_blob.py

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5905,46 +5905,74 @@ def test_soft_hard_delte_time_unset(self):
59055905
self.assertIsNone(blob.soft_delete_time)
59065906
self.assertIsNone(blob.hard_delete_time)
59075907

5908-
def test_from_string_w_valid_uri(self):
5908+
def test_from_uri_w_valid_uri(self):
59095909
from google.cloud.storage.blob import Blob
59105910

59115911
client = self._make_client()
59125912
basic_uri = "gs://bucket_name/b"
5913-
blob = Blob.from_string(basic_uri, client)
5913+
blob = Blob.from_uri(basic_uri, client)
59145914

59155915
self.assertIsInstance(blob, Blob)
59165916
self.assertIs(blob.client, client)
59175917
self.assertEqual(blob.name, "b")
59185918
self.assertEqual(blob.bucket.name, "bucket_name")
59195919

59205920
nested_uri = "gs://bucket_name/path1/path2/b#name"
5921-
blob = Blob.from_string(nested_uri, client)
5921+
blob = Blob.from_uri(nested_uri, client)
59225922

59235923
self.assertIsInstance(blob, Blob)
59245924
self.assertIs(blob.client, client)
59255925
self.assertEqual(blob.name, "path1/path2/b#name")
59265926
self.assertEqual(blob.bucket.name, "bucket_name")
59275927

5928-
def test_from_string_w_invalid_uri(self):
5928+
def test_from_uri_w_invalid_uri(self):
59295929
from google.cloud.storage.blob import Blob
59305930

59315931
client = self._make_client()
59325932

59335933
with pytest.raises(ValueError):
5934-
Blob.from_string("http://bucket_name/b", client)
5934+
Blob.from_uri("http://bucket_name/b", client)
59355935

5936-
def test_from_string_w_domain_name_bucket(self):
5936+
def test_from_uri_w_domain_name_bucket(self):
59375937
from google.cloud.storage.blob import Blob
59385938

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

59435943
self.assertIsInstance(blob, Blob)
59445944
self.assertIs(blob.client, client)
59455945
self.assertEqual(blob.name, "b")
59465946
self.assertEqual(blob.bucket.name, "buckets.example.com")
59475947

5948+
@mock.patch("warnings.warn")
5949+
def test_from_string(self, mock_warn):
5950+
from google.cloud.storage.blob import _FROM_STRING_DEPRECATED
5951+
from google.cloud.storage.blob import Blob
5952+
5953+
client = self._make_client()
5954+
basic_uri = "gs://bucket_name/b"
5955+
blob = Blob.from_string(basic_uri, client)
5956+
5957+
self.assertIsInstance(blob, Blob)
5958+
self.assertIs(blob.client, client)
5959+
self.assertEqual(blob.name, "b")
5960+
self.assertEqual(blob.bucket.name, "bucket_name")
5961+
5962+
nested_uri = "gs://bucket_name/path1/path2/b#name"
5963+
blob = Blob.from_string(nested_uri, client)
5964+
5965+
self.assertIsInstance(blob, Blob)
5966+
self.assertIs(blob.client, client)
5967+
self.assertEqual(blob.name, "path1/path2/b#name")
5968+
self.assertEqual(blob.bucket.name, "bucket_name")
5969+
5970+
mock_warn.assert_any_call(
5971+
_FROM_STRING_DEPRECATED,
5972+
PendingDeprecationWarning,
5973+
stacklevel=2,
5974+
)
5975+
59485976
def test_open(self):
59495977
from io import TextIOWrapper
59505978
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
@@ -4386,39 +4386,59 @@ def _generate_signed_url_helper(
43864386
}
43874387
signer.assert_called_once_with(expected_creds, **expected_kwargs)
43884388

4389-
def test_get_bucket_from_string_w_valid_uri(self):
4389+
def test_get_bucket_from_uri_w_valid_uri(self):
43904390
from google.cloud.storage.bucket import Bucket
43914391

43924392
client = self._make_client()
43934393
BUCKET_NAME = "BUCKET_NAME"
43944394
uri = "gs://" + BUCKET_NAME
43954395

4396-
bucket = Bucket.from_string(uri, client)
4396+
bucket = Bucket.from_uri(uri, client)
43974397

43984398
self.assertIsInstance(bucket, Bucket)
43994399
self.assertIs(bucket.client, client)
44004400
self.assertEqual(bucket.name, BUCKET_NAME)
44014401

4402-
def test_get_bucket_from_string_w_invalid_uri(self):
4402+
def test_get_bucket_from_uri_w_invalid_uri(self):
44034403
from google.cloud.storage.bucket import Bucket
44044404

44054405
client = self._make_client()
44064406

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

4410-
def test_get_bucket_from_string_w_domain_name_bucket(self):
4410+
def test_get_bucket_from_uri_w_domain_name_bucket(self):
44114411
from google.cloud.storage.bucket import Bucket
44124412

44134413
client = self._make_client()
44144414
BUCKET_NAME = "buckets.example.com"
44154415
uri = "gs://" + BUCKET_NAME
44164416

4417+
bucket = Bucket.from_uri(uri, client)
4418+
4419+
self.assertIsInstance(bucket, Bucket)
4420+
self.assertIs(bucket.client, client)
4421+
self.assertEqual(bucket.name, BUCKET_NAME)
4422+
4423+
@mock.patch("warnings.warn")
4424+
def test_get_bucket_from_string(self, mock_warn):
4425+
from google.cloud.storage.bucket import _FROM_STRING_MESSAGE
4426+
from google.cloud.storage.bucket import Bucket
4427+
4428+
client = self._make_client()
4429+
BUCKET_NAME = "BUCKET_NAME"
4430+
uri = "gs://" + BUCKET_NAME
4431+
44174432
bucket = Bucket.from_string(uri, client)
44184433

44194434
self.assertIsInstance(bucket, Bucket)
44204435
self.assertIs(bucket.client, client)
44214436
self.assertEqual(bucket.name, BUCKET_NAME)
4437+
mock_warn.assert_any_call(
4438+
_FROM_STRING_MESSAGE,
4439+
PendingDeprecationWarning,
4440+
stacklevel=2,
4441+
)
44224442

44234443
def test_generate_signed_url_no_version_passed_warning(self):
44244444
self._generate_signed_url_helper()

tests/unit/test_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1837,7 +1837,7 @@ def test_download_blob_to_file_with_uri(self):
18371837
_helpers, "_get_invocation_id", return_value=GCCL_INVOCATION_TEST_CONST
18381838
):
18391839
with mock.patch(
1840-
"google.cloud.storage.client.Blob.from_string", return_value=blob
1840+
"google.cloud.storage.client.Blob.from_uri", return_value=blob
18411841
):
18421842
client.download_blob_to_file(
18431843
"gs://bucket_name/path/to/object", file_obj

0 commit comments

Comments
 (0)