Skip to content

Commit

Permalink
refactor: remove deprecated method calls (#347)
Browse files Browse the repository at this point in the history
  • Loading branch information
HemangChothani committed Jan 5, 2021
1 parent 9d5bb56 commit 6d5a667
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 40 deletions.
4 changes: 2 additions & 2 deletions docs/snippets.py
Expand Up @@ -95,7 +95,7 @@ def download_to_file(to_delete):
blob = Blob("secure-data", bucket, encryption_key=encryption_key)
blob.upload_from_string("my secret message.")
with open("/tmp/my-secure-file", "wb") as file_obj:
blob.download_to_file(file_obj)
client.download_to_file(blob, file_obj)
# [END download_to_file]

to_delete.append(blob)
Expand Down Expand Up @@ -140,7 +140,7 @@ def delete_blob(to_delete):

client = storage.Client()
bucket = client.get_bucket("my-bucket")
blobs = list(bucket.list_blobs())
blobs = list(client.list_blobs(bucket))
assert len(blobs) > 0
# [<Blob: my-bucket, my-file.txt>]
bucket.delete_blob("my-file.txt")
Expand Down
10 changes: 6 additions & 4 deletions google/cloud/storage/blob.py
Expand Up @@ -1178,11 +1178,12 @@ def download_to_filename(
:raises: :class:`google.cloud.exceptions.NotFound`
"""
client = self._require_client(client)
try:
with open(filename, "wb") as file_obj:
self.download_to_file(
client.download_blob_to_file(
self,
file_obj,
client=client,
start=start,
end=end,
raw_download=raw_download,
Expand Down Expand Up @@ -1285,10 +1286,11 @@ def download_as_bytes(
:raises: :class:`google.cloud.exceptions.NotFound`
"""
client = self._require_client(client)
string_buffer = BytesIO()
self.download_to_file(
client.download_blob_to_file(
self,
string_buffer,
client=client,
start=start,
end=end,
raw_download=raw_download,
Expand Down
64 changes: 38 additions & 26 deletions tests/system/test_system.py
Expand Up @@ -63,9 +63,9 @@ def _bad_copy(bad_request):
retry_bad_copy = RetryErrors(exceptions.BadRequest, error_predicate=_bad_copy)


def _empty_bucket(bucket):
def _empty_bucket(client, bucket):
"""Empty a bucket of all existing blobs (including multiple versions)."""
for blob in list(bucket.list_blobs(versions=True)):
for blob in list(client.list_blobs(bucket, versions=True)):
try:
blob.delete()
except exceptions.NotFound:
Expand Down Expand Up @@ -96,7 +96,7 @@ def setUpModule():
def tearDownModule():
errors = (exceptions.Conflict, exceptions.TooManyRequests)
retry = RetryErrors(errors, max_tries=15)
retry(_empty_bucket)(Config.TEST_BUCKET)
retry(_empty_bucket)(Config.CLIENT, Config.TEST_BUCKET)
retry(Config.TEST_BUCKET.delete)(force=True)


Expand Down Expand Up @@ -622,7 +622,7 @@ def test_large_encrypted_file_write_from_stream(self):

with tempfile.NamedTemporaryFile() as temp_f:
with open(temp_f.name, "wb") as file_obj:
blob.download_to_file(file_obj)
Config.CLIENT.download_blob_to_file(blob, file_obj)

with open(temp_f.name, "rb") as file_obj:
md5_temp_hash = _base64_md5hash(file_obj)
Expand Down Expand Up @@ -718,11 +718,15 @@ def test_crud_blob_w_user_project(self):
self.assertIsNone(blob1.metadata)
finally:
# Exercise 'objects.delete' (metadata) w/ userProject.
blobs = with_user_project.list_blobs(prefix=blob.name, versions=True)
blobs = Config.CLIENT.list_blobs(
with_user_project, prefix=blob.name, versions=True
)
self.assertEqual([each.generation for each in blobs], [gen0, gen1])

blob0.delete()
blobs = with_user_project.list_blobs(prefix=blob.name, versions=True)
blobs = Config.CLIENT.list_blobs(
with_user_project, prefix=blob.name, versions=True
)
self.assertEqual([each.generation for each in blobs], [gen1])

blob1.delete()
Expand Down Expand Up @@ -859,7 +863,7 @@ def test_direct_write_and_read_into_file(self):
with tempfile.NamedTemporaryFile() as temp_f:

with open(temp_f.name, "wb") as file_obj:
same_blob.download_to_file(file_obj)
Config.CLIENT.download_blob_to_file(same_blob, file_obj)

with open(temp_f.name, "rb") as file_obj:
stored_contents = file_obj.read()
Expand All @@ -881,11 +885,12 @@ def test_download_w_generation_match(self):

with open(temp_f.name, "wb") as file_obj:
with self.assertRaises(google.api_core.exceptions.PreconditionFailed):
same_blob.download_to_file(
file_obj, if_generation_match=WRONG_GENERATION_NUMBER
Config.CLIENT.download_blob_to_file(
same_blob, file_obj, if_generation_match=WRONG_GENERATION_NUMBER
)

same_blob.download_to_file(
Config.CLIENT.download_blob_to_file(
same_blob,
file_obj,
if_generation_match=blob.generation,
if_metageneration_match=blob.metageneration,
Expand Down Expand Up @@ -1068,7 +1073,7 @@ class TestStorageListFiles(TestStorageFiles):
def setUpClass(cls):
super(TestStorageListFiles, cls).setUpClass()
# Make sure bucket empty before beginning.
_empty_bucket(cls.bucket)
_empty_bucket(Config.CLIENT, cls.bucket)

logo_path = cls.FILES["logo"]["path"]
blob = storage.Blob(cls.FILENAMES[0], bucket=cls.bucket)
Expand All @@ -1089,7 +1094,7 @@ def tearDownClass(cls):

@RetryErrors(unittest.TestCase.failureException)
def test_list_files(self):
all_blobs = list(self.bucket.list_blobs())
all_blobs = list(Config.CLIENT.list_blobs(self.bucket))
self.assertEqual(
sorted(blob.name for blob in all_blobs), sorted(self.FILENAMES)
)
Expand All @@ -1100,7 +1105,7 @@ def test_list_files_with_user_project(self):
with_user_project = Config.CLIENT.bucket(
self.bucket.name, user_project=USER_PROJECT
)
all_blobs = list(with_user_project.list_blobs())
all_blobs = list(Config.CLIENT.list_blobs(with_user_project))
self.assertEqual(
sorted(blob.name for blob in all_blobs), sorted(self.FILENAMES)
)
Expand All @@ -1109,7 +1114,7 @@ def test_list_files_with_user_project(self):
def test_paginate_files(self):
truncation_size = 1
count = len(self.FILENAMES) - truncation_size
iterator = self.bucket.list_blobs(max_results=count)
iterator = Config.CLIENT.list_blobs(self.bucket, max_results=count)
page_iter = iterator.pages

page1 = six.next(page_iter)
Expand All @@ -1133,7 +1138,8 @@ def test_paginate_files_with_offset(self):
exclusive_end_offset = self.FILENAMES[-1]
desired_files = self.FILENAMES[1:-1]
count = len(desired_files) - truncation_size
iterator = self.bucket.list_blobs(
iterator = Config.CLIENT.list_blobs(
self.bucket,
max_results=count,
start_offset=inclusive_start_offset,
end_offset=exclusive_end_offset,
Expand Down Expand Up @@ -1173,7 +1179,7 @@ class TestStoragePseudoHierarchy(TestStorageFiles):
def setUpClass(cls):
super(TestStoragePseudoHierarchy, cls).setUpClass()
# Make sure bucket empty before beginning.
_empty_bucket(cls.bucket)
_empty_bucket(Config.CLIENT, cls.bucket)

cls.suite_blobs_to_delete = []
simple_path = cls.FILES["simple"]["path"]
Expand All @@ -1197,7 +1203,7 @@ def test_blob_get_w_delimiter(self):

@RetryErrors(unittest.TestCase.failureException)
def test_root_level_w_delimiter(self):
iterator = self.bucket.list_blobs(delimiter="/")
iterator = Config.CLIENT.list_blobs(self.bucket, delimiter="/")
page = six.next(iterator.pages)
blobs = list(page)
self.assertEqual([blob.name for blob in blobs], ["file01.txt"])
Expand All @@ -1206,7 +1212,9 @@ def test_root_level_w_delimiter(self):

@RetryErrors(unittest.TestCase.failureException)
def test_first_level(self):
iterator = self.bucket.list_blobs(delimiter="/", prefix="parent/")
iterator = Config.CLIENT.list_blobs(
self.bucket, delimiter="/", prefix="parent/"
)
page = six.next(iterator.pages)
blobs = list(page)
self.assertEqual(
Expand All @@ -1219,7 +1227,9 @@ def test_first_level(self):
def test_second_level(self):
expected_names = ["parent/child/file21.txt", "parent/child/file22.txt"]

iterator = self.bucket.list_blobs(delimiter="/", prefix="parent/child/")
iterator = Config.CLIENT.list_blobs(
self.bucket, delimiter="/", prefix="parent/child/"
)
page = six.next(iterator.pages)
blobs = list(page)
self.assertEqual([blob.name for blob in blobs], expected_names)
Expand All @@ -1234,7 +1244,9 @@ def test_third_level(self):
# of 1024 characters in the UTF-8 encoded name:
# https://cloud.google.com/storage/docs/bucketnaming#objectnames
# Exercise a layer deeper to illustrate this.
iterator = self.bucket.list_blobs(delimiter="/", prefix="parent/child/grand/")
iterator = Config.CLIENT.list_blobs(
self.bucket, delimiter="/", prefix="parent/child/grand/"
)
page = six.next(iterator.pages)
blobs = list(page)
self.assertEqual(
Expand All @@ -1245,8 +1257,8 @@ def test_third_level(self):

@RetryErrors(unittest.TestCase.failureException)
def test_include_trailing_delimiter(self):
iterator = self.bucket.list_blobs(
delimiter="/", include_trailing_delimiter=True
iterator = Config.CLIENT.list_blobs(
self.bucket, delimiter="/", include_trailing_delimiter=True
)
page = six.next(iterator.pages)
blobs = list(page)
Expand All @@ -1273,7 +1285,7 @@ def setUpClass(cls):

@classmethod
def tearDownClass(cls):
_empty_bucket(cls.bucket)
_empty_bucket(Config.CLIENT, cls.bucket)
errors = (exceptions.Conflict, exceptions.TooManyRequests)
retry = RetryErrors(errors, max_tries=6)
retry(cls.bucket.delete)(force=True)
Expand Down Expand Up @@ -1961,7 +1973,7 @@ class TestAnonymousClient(unittest.TestCase):
def test_access_to_public_bucket(self):
anonymous = storage.Client.create_anonymous_client()
bucket = anonymous.bucket(self.PUBLIC_BUCKET)
(blob,) = retry_429_503(bucket.list_blobs)(max_results=1)
(blob,) = retry_429_503(anonymous.list_blobs)(bucket, max_results=1)
with tempfile.TemporaryFile() as stream:
retry_429_503(blob.download_to_file)(stream)

Expand All @@ -1988,7 +2000,7 @@ def _kms_key_name(self, key_name=None):
@classmethod
def setUpClass(cls):
super(TestKMSIntegration, cls).setUpClass()
_empty_bucket(cls.bucket)
_empty_bucket(Config.CLIENT, cls.bucket)

def setUp(self):
super(TestKMSIntegration, self).setUp()
Expand Down Expand Up @@ -2048,7 +2060,7 @@ def test_blob_w_explicit_kms_key_name(self):
# We don't know the current version of the key.
self.assertTrue(blob.kms_key_name.startswith(kms_key_name))

(listed,) = list(self.bucket.list_blobs())
(listed,) = list(Config.CLIENT.list_blobs(self.bucket))
self.assertTrue(listed.kms_key_name.startswith(kms_key_name))

@RetryErrors(unittest.TestCase.failureException)
Expand Down
16 changes: 8 additions & 8 deletions tests/unit/test_blob.py
Expand Up @@ -1627,18 +1627,18 @@ def test_download_as_bytes_w_generation_match(self):
GENERATION_NUMBER = 6
MEDIA_LINK = "http://example.com/media/"

client = mock.Mock(spec=["_http"])
client = self._make_client()
blob = self._make_one(
"blob-name", bucket=_Bucket(client), properties={"mediaLink": MEDIA_LINK}
)
blob.download_to_file = mock.Mock()
client.download_blob_to_file = mock.Mock()

fetched = blob.download_as_bytes(if_generation_match=GENERATION_NUMBER)
self.assertEqual(fetched, b"")

blob.download_to_file.assert_called_once_with(
client.download_blob_to_file.assert_called_once_with(
blob,
mock.ANY,
client=None,
start=None,
end=None,
raw_download=False,
Expand Down Expand Up @@ -1810,18 +1810,18 @@ def test_download_as_text_w_non_ascii_wo_explicit_encoding_w_charset(self):
def test_download_as_string(self, mock_warn):
MEDIA_LINK = "http://example.com/media/"

client = mock.Mock(spec=["_http"])
client = self._make_client()
blob = self._make_one(
"blob-name", bucket=_Bucket(client), properties={"mediaLink": MEDIA_LINK}
)
blob.download_to_file = mock.Mock()
client.download_blob_to_file = mock.Mock()

fetched = blob.download_as_string()
self.assertEqual(fetched, b"")

blob.download_to_file.assert_called_once_with(
client.download_blob_to_file.assert_called_once_with(
blob,
mock.ANY,
client=None,
start=None,
end=None,
raw_download=False,
Expand Down

0 comments on commit 6d5a667

Please sign in to comment.