Skip to content

Commit 569cb2f

Browse files
refactor: remove deprecated method calls (#347)
1 parent 187f6c4 commit 569cb2f

File tree

4 files changed

+54
-40
lines changed

4 files changed

+54
-40
lines changed

packages/google-cloud-storage/docs/snippets.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def download_to_file(to_delete):
9595
blob = Blob("secure-data", bucket, encryption_key=encryption_key)
9696
blob.upload_from_string("my secret message.")
9797
with open("/tmp/my-secure-file", "wb") as file_obj:
98-
blob.download_to_file(file_obj)
98+
client.download_to_file(blob, file_obj)
9999
# [END download_to_file]
100100

101101
to_delete.append(blob)
@@ -140,7 +140,7 @@ def delete_blob(to_delete):
140140

141141
client = storage.Client()
142142
bucket = client.get_bucket("my-bucket")
143-
blobs = list(bucket.list_blobs())
143+
blobs = list(client.list_blobs(bucket))
144144
assert len(blobs) > 0
145145
# [<Blob: my-bucket, my-file.txt>]
146146
bucket.delete_blob("my-file.txt")

packages/google-cloud-storage/google/cloud/storage/blob.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1178,11 +1178,12 @@ def download_to_filename(
11781178
11791179
:raises: :class:`google.cloud.exceptions.NotFound`
11801180
"""
1181+
client = self._require_client(client)
11811182
try:
11821183
with open(filename, "wb") as file_obj:
1183-
self.download_to_file(
1184+
client.download_blob_to_file(
1185+
self,
11841186
file_obj,
1185-
client=client,
11861187
start=start,
11871188
end=end,
11881189
raw_download=raw_download,
@@ -1285,10 +1286,11 @@ def download_as_bytes(
12851286
12861287
:raises: :class:`google.cloud.exceptions.NotFound`
12871288
"""
1289+
client = self._require_client(client)
12881290
string_buffer = BytesIO()
1289-
self.download_to_file(
1291+
client.download_blob_to_file(
1292+
self,
12901293
string_buffer,
1291-
client=client,
12921294
start=start,
12931295
end=end,
12941296
raw_download=raw_download,

packages/google-cloud-storage/tests/system/test_system.py

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ def _bad_copy(bad_request):
6363
retry_bad_copy = RetryErrors(exceptions.BadRequest, error_predicate=_bad_copy)
6464

6565

66-
def _empty_bucket(bucket):
66+
def _empty_bucket(client, bucket):
6767
"""Empty a bucket of all existing blobs (including multiple versions)."""
68-
for blob in list(bucket.list_blobs(versions=True)):
68+
for blob in list(client.list_blobs(bucket, versions=True)):
6969
try:
7070
blob.delete()
7171
except exceptions.NotFound:
@@ -96,7 +96,7 @@ def setUpModule():
9696
def tearDownModule():
9797
errors = (exceptions.Conflict, exceptions.TooManyRequests)
9898
retry = RetryErrors(errors, max_tries=15)
99-
retry(_empty_bucket)(Config.TEST_BUCKET)
99+
retry(_empty_bucket)(Config.CLIENT, Config.TEST_BUCKET)
100100
retry(Config.TEST_BUCKET.delete)(force=True)
101101

102102

@@ -622,7 +622,7 @@ def test_large_encrypted_file_write_from_stream(self):
622622

623623
with tempfile.NamedTemporaryFile() as temp_f:
624624
with open(temp_f.name, "wb") as file_obj:
625-
blob.download_to_file(file_obj)
625+
Config.CLIENT.download_blob_to_file(blob, file_obj)
626626

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

724726
blob0.delete()
725-
blobs = with_user_project.list_blobs(prefix=blob.name, versions=True)
727+
blobs = Config.CLIENT.list_blobs(
728+
with_user_project, prefix=blob.name, versions=True
729+
)
726730
self.assertEqual([each.generation for each in blobs], [gen1])
727731

728732
blob1.delete()
@@ -859,7 +863,7 @@ def test_direct_write_and_read_into_file(self):
859863
with tempfile.NamedTemporaryFile() as temp_f:
860864

861865
with open(temp_f.name, "wb") as file_obj:
862-
same_blob.download_to_file(file_obj)
866+
Config.CLIENT.download_blob_to_file(same_blob, file_obj)
863867

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

882886
with open(temp_f.name, "wb") as file_obj:
883887
with self.assertRaises(google.api_core.exceptions.PreconditionFailed):
884-
same_blob.download_to_file(
885-
file_obj, if_generation_match=WRONG_GENERATION_NUMBER
888+
Config.CLIENT.download_blob_to_file(
889+
same_blob, file_obj, if_generation_match=WRONG_GENERATION_NUMBER
886890
)
887891

888-
same_blob.download_to_file(
892+
Config.CLIENT.download_blob_to_file(
893+
same_blob,
889894
file_obj,
890895
if_generation_match=blob.generation,
891896
if_metageneration_match=blob.metageneration,
@@ -1068,7 +1073,7 @@ class TestStorageListFiles(TestStorageFiles):
10681073
def setUpClass(cls):
10691074
super(TestStorageListFiles, cls).setUpClass()
10701075
# Make sure bucket empty before beginning.
1071-
_empty_bucket(cls.bucket)
1076+
_empty_bucket(Config.CLIENT, cls.bucket)
10721077

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

10901095
@RetryErrors(unittest.TestCase.failureException)
10911096
def test_list_files(self):
1092-
all_blobs = list(self.bucket.list_blobs())
1097+
all_blobs = list(Config.CLIENT.list_blobs(self.bucket))
10931098
self.assertEqual(
10941099
sorted(blob.name for blob in all_blobs), sorted(self.FILENAMES)
10951100
)
@@ -1100,7 +1105,7 @@ def test_list_files_with_user_project(self):
11001105
with_user_project = Config.CLIENT.bucket(
11011106
self.bucket.name, user_project=USER_PROJECT
11021107
)
1103-
all_blobs = list(with_user_project.list_blobs())
1108+
all_blobs = list(Config.CLIENT.list_blobs(with_user_project))
11041109
self.assertEqual(
11051110
sorted(blob.name for blob in all_blobs), sorted(self.FILENAMES)
11061111
)
@@ -1109,7 +1114,7 @@ def test_list_files_with_user_project(self):
11091114
def test_paginate_files(self):
11101115
truncation_size = 1
11111116
count = len(self.FILENAMES) - truncation_size
1112-
iterator = self.bucket.list_blobs(max_results=count)
1117+
iterator = Config.CLIENT.list_blobs(self.bucket, max_results=count)
11131118
page_iter = iterator.pages
11141119

11151120
page1 = six.next(page_iter)
@@ -1133,7 +1138,8 @@ def test_paginate_files_with_offset(self):
11331138
exclusive_end_offset = self.FILENAMES[-1]
11341139
desired_files = self.FILENAMES[1:-1]
11351140
count = len(desired_files) - truncation_size
1136-
iterator = self.bucket.list_blobs(
1141+
iterator = Config.CLIENT.list_blobs(
1142+
self.bucket,
11371143
max_results=count,
11381144
start_offset=inclusive_start_offset,
11391145
end_offset=exclusive_end_offset,
@@ -1173,7 +1179,7 @@ class TestStoragePseudoHierarchy(TestStorageFiles):
11731179
def setUpClass(cls):
11741180
super(TestStoragePseudoHierarchy, cls).setUpClass()
11751181
# Make sure bucket empty before beginning.
1176-
_empty_bucket(cls.bucket)
1182+
_empty_bucket(Config.CLIENT, cls.bucket)
11771183

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

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

12071213
@RetryErrors(unittest.TestCase.failureException)
12081214
def test_first_level(self):
1209-
iterator = self.bucket.list_blobs(delimiter="/", prefix="parent/")
1215+
iterator = Config.CLIENT.list_blobs(
1216+
self.bucket, delimiter="/", prefix="parent/"
1217+
)
12101218
page = six.next(iterator.pages)
12111219
blobs = list(page)
12121220
self.assertEqual(
@@ -1219,7 +1227,9 @@ def test_first_level(self):
12191227
def test_second_level(self):
12201228
expected_names = ["parent/child/file21.txt", "parent/child/file22.txt"]
12211229

1222-
iterator = self.bucket.list_blobs(delimiter="/", prefix="parent/child/")
1230+
iterator = Config.CLIENT.list_blobs(
1231+
self.bucket, delimiter="/", prefix="parent/child/"
1232+
)
12231233
page = six.next(iterator.pages)
12241234
blobs = list(page)
12251235
self.assertEqual([blob.name for blob in blobs], expected_names)
@@ -1234,7 +1244,9 @@ def test_third_level(self):
12341244
# of 1024 characters in the UTF-8 encoded name:
12351245
# https://cloud.google.com/storage/docs/bucketnaming#objectnames
12361246
# Exercise a layer deeper to illustrate this.
1237-
iterator = self.bucket.list_blobs(delimiter="/", prefix="parent/child/grand/")
1247+
iterator = Config.CLIENT.list_blobs(
1248+
self.bucket, delimiter="/", prefix="parent/child/grand/"
1249+
)
12381250
page = six.next(iterator.pages)
12391251
blobs = list(page)
12401252
self.assertEqual(
@@ -1245,8 +1257,8 @@ def test_third_level(self):
12451257

12461258
@RetryErrors(unittest.TestCase.failureException)
12471259
def test_include_trailing_delimiter(self):
1248-
iterator = self.bucket.list_blobs(
1249-
delimiter="/", include_trailing_delimiter=True
1260+
iterator = Config.CLIENT.list_blobs(
1261+
self.bucket, delimiter="/", include_trailing_delimiter=True
12501262
)
12511263
page = six.next(iterator.pages)
12521264
blobs = list(page)
@@ -1273,7 +1285,7 @@ def setUpClass(cls):
12731285

12741286
@classmethod
12751287
def tearDownClass(cls):
1276-
_empty_bucket(cls.bucket)
1288+
_empty_bucket(Config.CLIENT, cls.bucket)
12771289
errors = (exceptions.Conflict, exceptions.TooManyRequests)
12781290
retry = RetryErrors(errors, max_tries=6)
12791291
retry(cls.bucket.delete)(force=True)
@@ -1961,7 +1973,7 @@ class TestAnonymousClient(unittest.TestCase):
19611973
def test_access_to_public_bucket(self):
19621974
anonymous = storage.Client.create_anonymous_client()
19631975
bucket = anonymous.bucket(self.PUBLIC_BUCKET)
1964-
(blob,) = retry_429_503(bucket.list_blobs)(max_results=1)
1976+
(blob,) = retry_429_503(anonymous.list_blobs)(bucket, max_results=1)
19651977
with tempfile.TemporaryFile() as stream:
19661978
retry_429_503(blob.download_to_file)(stream)
19671979

@@ -1988,7 +2000,7 @@ def _kms_key_name(self, key_name=None):
19882000
@classmethod
19892001
def setUpClass(cls):
19902002
super(TestKMSIntegration, cls).setUpClass()
1991-
_empty_bucket(cls.bucket)
2003+
_empty_bucket(Config.CLIENT, cls.bucket)
19922004

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

2051-
(listed,) = list(self.bucket.list_blobs())
2063+
(listed,) = list(Config.CLIENT.list_blobs(self.bucket))
20522064
self.assertTrue(listed.kms_key_name.startswith(kms_key_name))
20532065

20542066
@RetryErrors(unittest.TestCase.failureException)

packages/google-cloud-storage/tests/unit/test_blob.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1627,18 +1627,18 @@ def test_download_as_bytes_w_generation_match(self):
16271627
GENERATION_NUMBER = 6
16281628
MEDIA_LINK = "http://example.com/media/"
16291629

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

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

1639-
blob.download_to_file.assert_called_once_with(
1639+
client.download_blob_to_file.assert_called_once_with(
1640+
blob,
16401641
mock.ANY,
1641-
client=None,
16421642
start=None,
16431643
end=None,
16441644
raw_download=False,
@@ -1810,18 +1810,18 @@ def test_download_as_text_w_non_ascii_wo_explicit_encoding_w_charset(self):
18101810
def test_download_as_string(self, mock_warn):
18111811
MEDIA_LINK = "http://example.com/media/"
18121812

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

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

1822-
blob.download_to_file.assert_called_once_with(
1822+
client.download_blob_to_file.assert_called_once_with(
1823+
blob,
18231824
mock.ANY,
1824-
client=None,
18251825
start=None,
18261826
end=None,
18271827
raw_download=False,

0 commit comments

Comments
 (0)