Skip to content

Commit 50bf536

Browse files
authored
Remove deprecated method call (blob.download_as_string) (#20091)
1 parent 3a0f554 commit 50bf536

File tree

6 files changed

+14
-14
lines changed

6 files changed

+14
-14
lines changed

airflow/providers/google/cloud/hooks/gcs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ def download(
311311
self.log.info('File downloaded to %s', filename)
312312
return filename
313313
else:
314-
return blob.download_as_string()
314+
return blob.download_as_bytes()
315315

316316
except GoogleCloudError:
317317
if num_file_attempts == num_max_attempts:

airflow/providers/google/cloud/operators/bigquery.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -948,7 +948,7 @@ def execute(self, context) -> None:
948948
delegate_to=self.delegate_to,
949949
impersonation_chain=self.impersonation_chain,
950950
)
951-
schema_fields = json.loads(gcs_hook.download(gcs_bucket, gcs_object))
951+
schema_fields = json.loads(gcs_hook.download(gcs_bucket, gcs_object).decode("utf-8"))
952952
else:
953953
schema_fields = self.schema_fields
954954

@@ -1203,7 +1203,7 @@ def execute(self, context) -> None:
12031203
delegate_to=self.delegate_to,
12041204
impersonation_chain=self.impersonation_chain,
12051205
)
1206-
schema_fields = json.loads(gcs_hook.download(self.bucket, self.schema_object))
1206+
schema_fields = json.loads(gcs_hook.download(self.bucket, self.schema_object).decode("utf-8"))
12071207
else:
12081208
schema_fields = self.schema_fields
12091209

airflow/providers/google/cloud/utils/mlengine_operator_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ def apply_validate_fn(*args, templates_dict, **kwargs):
264264
raise ValueError(f"Wrong format prediction_path: {prediction_path}")
265265
summary = os.path.join(obj.strip("/"), "prediction.summary.json")
266266
gcs_hook = GCSHook()
267-
summary = json.loads(gcs_hook.download(bucket, summary))
267+
summary = json.loads(gcs_hook.download(bucket, summary).decode("utf-8"))
268268
return validate_fn(summary)
269269

270270
evaluate_validation = PythonOperator(

tests/providers/google/cloud/hooks/test_gcs.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -641,12 +641,12 @@ def test_compose_without_destination_object(self, mock_service):
641641
assert str(ctx.value) == 'bucket_name and destination_object cannot be empty.'
642642

643643
@mock.patch(GCS_STRING.format('GCSHook.get_conn'))
644-
def test_download_as_string(self, mock_service):
644+
def test_download_as_bytes(self, mock_service):
645645
test_bucket = 'test_bucket'
646646
test_object = 'test_object'
647647
test_object_bytes = io.BytesIO(b"input")
648648

649-
download_method = mock_service.return_value.bucket.return_value.blob.return_value.download_as_string
649+
download_method = mock_service.return_value.bucket.return_value.blob.return_value.download_as_bytes
650650
download_method.return_value = test_object_bytes
651651

652652
response = self.gcs_hook.download(bucket_name=test_bucket, object_name=test_object, filename=None)
@@ -666,10 +666,10 @@ def test_download_to_file(self, mock_service):
666666
)
667667
download_filename_method.return_value = None
668668

669-
download_as_a_string_method = (
670-
mock_service.return_value.bucket.return_value.blob.return_value.download_as_string
669+
download_as_a_bytes_method = (
670+
mock_service.return_value.bucket.return_value.blob.return_value.download_as_bytes
671671
)
672-
download_as_a_string_method.return_value = test_object_bytes
672+
download_as_a_bytes_method.return_value = test_object_bytes
673673
response = self.gcs_hook.download(
674674
bucket_name=test_bucket, object_name=test_object, filename=test_file
675675
)
@@ -690,10 +690,10 @@ def test_provide_file(self, mock_service, mock_temp_file):
690690
)
691691
download_filename_method.return_value = None
692692

693-
download_as_a_string_method = (
694-
mock_service.return_value.bucket.return_value.blob.return_value.download_as_string
693+
download_as_a_bytes_method = (
694+
mock_service.return_value.bucket.return_value.blob.return_value.download_as_bytes
695695
)
696-
download_as_a_string_method.return_value = test_object_bytes
696+
download_as_a_bytes_method.return_value = test_object_bytes
697697
mock_temp_file.return_value.__enter__.return_value = mock.MagicMock()
698698
mock_temp_file.return_value.__enter__.return_value.name = test_file
699699

tests/providers/google/cloud/operators/test_mlengine_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ def test_successful_run(self):
127127

128128
with patch('airflow.providers.google.cloud.utils.mlengine_operator_utils.GCSHook') as mock_gcs_hook:
129129
hook_instance = mock_gcs_hook.return_value
130-
hook_instance.download.return_value = '{"err": 0.9, "count": 9}'
130+
hook_instance.download.return_value = b'{"err": 0.9, "count": 9}'
131131
result = validate.execute({})
132132
hook_instance.download.assert_called_once_with(
133133
'legal-bucket', 'fake-output-path/prediction.summary.json'

tests/providers/google/cloud/utils/test_mlengine_operator_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ def test_apply_validate_fn(self, mock_beam_pipeline, mock_python, mock_download)
232232

233233
_, _, evaluate_validation = result
234234

235-
mock_download.return_value = json.dumps({"err": 0.3, "mse": 0.04, "count": 1100})
235+
mock_download.return_value = json.dumps({"err": 0.3, "mse": 0.04, "count": 1100}).encode("utf-8")
236236
templates_dict = {"prediction_path": PREDICTION_PATH}
237237
with pytest.raises(ValueError) as ctx:
238238
evaluate_validation.python_callable(templates_dict=templates_dict)

0 commit comments

Comments
 (0)