Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: from_string method of blob and bucket class #290

Merged
merged 7 commits into from Nov 11, 2020
10 changes: 6 additions & 4 deletions google/cloud/storage/blob.py
Expand Up @@ -330,15 +330,15 @@ def public_url(self):
)

@classmethod
def from_string(cls, uri, client):
def from_string(cls, uri, client=None):
"""Get a constructor for blob object by URI.

:type uri: str
:param uri: The blob uri pass to get blob object.

:type client: :class:`~google.cloud.storage.client.Client` or
``NoneType``
:param client: The client to use.
:param client: (Optional) The client to use.

:rtype: :class:`google.cloud.storage.blob.Blob`
:returns: The blob object created.
Expand Down Expand Up @@ -1598,6 +1598,7 @@ def _do_multipart_upload(
:raises: :exc:`ValueError` if ``size`` is not :data:`None` but the
``stream`` has fewer than ``size`` bytes remaining.
"""
client = self._require_client(client)
if size is None:
data = stream.read()
else:
Expand All @@ -1611,7 +1612,7 @@ def _do_multipart_upload(
headers, object_metadata, content_type = info

base_url = _MULTIPART_URL_TEMPLATE.format(
hostname=self.client._connection.API_BASE_URL, bucket_path=self.bucket.path
hostname=client._connection.API_BASE_URL, bucket_path=self.bucket.path
)
name_value_pairs = []

Expand Down Expand Up @@ -1768,6 +1769,7 @@ def _initiate_resumable_upload(
that was created
* The ``transport`` used to initiate the upload.
"""
client = self._require_client(client)
if chunk_size is None:
chunk_size = self.chunk_size
if chunk_size is None:
Expand All @@ -1780,7 +1782,7 @@ def _initiate_resumable_upload(
headers.update(extra_headers)

base_url = _RESUMABLE_URL_TEMPLATE.format(
hostname=self.client._connection.API_BASE_URL, bucket_path=self.bucket.path
hostname=client._connection.API_BASE_URL, bucket_path=self.bucket.path
)
name_value_pairs = []

Expand Down
4 changes: 2 additions & 2 deletions google/cloud/storage/bucket.py
Expand Up @@ -612,15 +612,15 @@ def user_project(self):
return self._user_project

@classmethod
def from_string(cls, uri, client):
def from_string(cls, uri, client=None):
"""Get a constructor for bucket object by URI.

:type uri: str
:param uri: The bucket uri pass to get bucket object.

:type client: :class:`~google.cloud.storage.client.Client` or
``NoneType``
:param client: The client to use.
:param client: (Optional) The client to use.

:rtype: :class:`google.cloud.storage.bucket.Bucket`
:returns: The bucket object created.
Expand Down
2 changes: 1 addition & 1 deletion google/cloud/storage/client.py
Expand Up @@ -579,7 +579,7 @@ def download_blob_to_file(self, blob_or_uri, file_obj, start=None, end=None):
try:
blob_or_uri.download_to_file(file_obj, client=self, start=start, end=end)
except AttributeError:
blob = Blob.from_string(blob_or_uri, self)
blob = Blob.from_string(blob_or_uri)
tseaver marked this conversation as resolved.
Show resolved Hide resolved
blob.download_to_file(file_obj, client=self, start=start, end=end)

def list_blobs(
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/test_blob.py
Expand Up @@ -1974,6 +1974,7 @@ def test__do_multipart_upload_with_generation_not_match(self, mock_get_boundary)

def test__do_multipart_upload_bad_size(self):
blob = self._make_one(u"blob-name", bucket=None)
client = mock.Mock()

data = b"data here hear hier"
stream = io.BytesIO(data)
Expand All @@ -1982,7 +1983,7 @@ def test__do_multipart_upload_bad_size(self):

with self.assertRaises(ValueError) as exc_info:
blob._do_multipart_upload(
None, stream, None, size, None, None, None, None, None, None
client, stream, None, size, None, None, None, None, None, None
)
tseaver marked this conversation as resolved.
Show resolved Hide resolved

exc_contents = str(exc_info.exception)
Expand Down