Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6eff22d

Browse files
authored
docs: pass explicit 'client' in '{Blob.Bucket}.from_string' examples (googleapis#545)
See googleapis#540.
1 parent bd72f5d commit 6eff22d

File tree

4 files changed

+73
-25
lines changed

4 files changed

+73
-25
lines changed

google/cloud/storage/blob.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -393,8 +393,8 @@ def from_string(cls, uri, client=None):
393393
394394
:type client: :class:`~google.cloud.storage.client.Client`
395395
:param client:
396-
(Optional) The client to use. If not passed, falls back to the
397-
``client`` stored on the blob's bucket.
396+
(Optional) The client to use. Application code should
397+
*always* pass ``client``.
398398
399399
:rtype: :class:`google.cloud.storage.blob.Blob`
400400
:returns: The blob object created.
@@ -405,7 +405,7 @@ def from_string(cls, uri, client=None):
405405
>>> from google.cloud import storage
406406
>>> from google.cloud.storage.blob import Blob
407407
>>> client = storage.Client()
408-
>>> blob = Blob.from_string("gs://bucket/object")
408+
>>> blob = Blob.from_string("gs://bucket/object", client=client)
409409
"""
410410
from google.cloud.storage.bucket import Bucket
411411

google/cloud/storage/bucket.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -656,7 +656,8 @@ def from_string(cls, uri, client=None):
656656
657657
:type client: :class:`~google.cloud.storage.client.Client` or
658658
``NoneType``
659-
:param client: (Optional) The client to use.
659+
:param client: (Optional) The client to use. Application code should
660+
*always* pass ``client``.
660661
661662
:rtype: :class:`google.cloud.storage.bucket.Bucket`
662663
:returns: The bucket object created.
@@ -667,7 +668,7 @@ def from_string(cls, uri, client=None):
667668
>>> from google.cloud import storage
668669
>>> from google.cloud.storage.bucket import Bucket
669670
>>> client = storage.Client()
670-
>>> bucket = Bucket.from_string("gs://bucket", client)
671+
>>> bucket = Bucket.from_string("gs://bucket", client=client)
671672
"""
672673
scheme, netloc, path, query, frag = urlsplit(uri)
673674

google/cloud/storage/client.py

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -221,26 +221,6 @@ def _pop_batch(self):
221221
"""
222222
return self._batch_stack.pop()
223223

224-
def _bucket_arg_to_bucket(self, bucket_or_name):
225-
"""Helper to return given bucket or create new by name.
226-
227-
Args:
228-
bucket_or_name (Union[ \
229-
:class:`~google.cloud.storage.bucket.Bucket`, \
230-
str, \
231-
]):
232-
The bucket resource to pass or name to create.
233-
234-
Returns:
235-
google.cloud.storage.bucket.Bucket
236-
The newly created bucket or the given one.
237-
"""
238-
if isinstance(bucket_or_name, Bucket):
239-
bucket = bucket_or_name
240-
else:
241-
bucket = Bucket(self, name=bucket_or_name)
242-
return bucket
243-
244224
@property
245225
def current_batch(self):
246226
"""Currently-active batch.
@@ -682,6 +662,28 @@ def _delete_resource(
682662
_target_object=_target_object,
683663
)
684664

665+
def _bucket_arg_to_bucket(self, bucket_or_name):
666+
"""Helper to return given bucket or create new by name.
667+
668+
Args:
669+
bucket_or_name (Union[ \
670+
:class:`~google.cloud.storage.bucket.Bucket`, \
671+
str, \
672+
]):
673+
The bucket resource to pass or name to create.
674+
675+
Returns:
676+
google.cloud.storage.bucket.Bucket
677+
The newly created bucket or the given one.
678+
"""
679+
if isinstance(bucket_or_name, Bucket):
680+
bucket = bucket_or_name
681+
if bucket.client is None:
682+
bucket._client = self
683+
else:
684+
bucket = Bucket(self, name=bucket_or_name)
685+
return bucket
686+
685687
def get_bucket(
686688
self,
687689
bucket_or_name,

tests/unit/test_client.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,51 @@ def test__delete_resource_hit_w_explicit(self):
785785
_target_object=target,
786786
)
787787

788+
def test__bucket_arg_to_bucket_w_bucket_w_client(self):
789+
from google.cloud.storage.bucket import Bucket
790+
791+
project = "PROJECT"
792+
credentials = _make_credentials()
793+
client = self._make_one(project=project, credentials=credentials)
794+
other_client = mock.Mock(spec=[])
795+
bucket_name = "w_client"
796+
797+
bucket = Bucket(other_client, name=bucket_name)
798+
799+
found = client._bucket_arg_to_bucket(bucket)
800+
801+
self.assertIs(found, bucket)
802+
self.assertIs(found.client, other_client)
803+
804+
def test__bucket_arg_to_bucket_w_bucket_wo_client(self):
805+
from google.cloud.storage.bucket import Bucket
806+
807+
project = "PROJECT"
808+
credentials = _make_credentials()
809+
client = self._make_one(project=project, credentials=credentials)
810+
bucket_name = "wo_client"
811+
812+
bucket = Bucket(client=None, name=bucket_name)
813+
814+
found = client._bucket_arg_to_bucket(bucket)
815+
816+
self.assertIs(found, bucket)
817+
self.assertIs(found.client, client)
818+
819+
def test__bucket_arg_to_bucket_w_bucket_name(self):
820+
from google.cloud.storage.bucket import Bucket
821+
822+
project = "PROJECT"
823+
credentials = _make_credentials()
824+
client = self._make_one(project=project, credentials=credentials)
825+
bucket_name = "string-name"
826+
827+
found = client._bucket_arg_to_bucket(bucket_name)
828+
829+
self.assertIsInstance(found, Bucket)
830+
self.assertEqual(found.name, bucket_name)
831+
self.assertIs(found.client, client)
832+
788833
def test_get_bucket_miss_w_string_w_defaults(self):
789834
from google.cloud.exceptions import NotFound
790835
from google.cloud.storage.bucket import Bucket

0 commit comments

Comments
 (0)