From a7a83904045925f4dc65dd810ed0aafb537ecf39 Mon Sep 17 00:00:00 2001 From: Ignacio Tartavull Date: Sat, 6 May 2017 09:04:20 -0400 Subject: [PATCH] perf(storage): blob downloads gzipped content. GET requests now contain the header accept-encoding:gzip This improves performance of compressible strings which were uploaded as gzips. The caller is not required to do any decompression because decompressiong is handle by the library. Confusing `IOError: CRC check failed` exceptions will be risen in the case of mid-download connection breaks. --- storage/google/cloud/storage/blob.py | 1 + storage/tests/unit/test_blob.py | 8 ++++++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/storage/google/cloud/storage/blob.py b/storage/google/cloud/storage/blob.py index 7163b7d0d3b5..080ff0d4d3d3 100644 --- a/storage/google/cloud/storage/blob.py +++ b/storage/google/cloud/storage/blob.py @@ -462,6 +462,7 @@ def download_to_file(self, file_obj, client=None): """ download_url = self._get_download_url() headers = _get_encryption_headers(self._encryption_key) + headers['accept-encoding'] = 'gzip' transport = self._make_transport(client) try: diff --git a/storage/tests/unit/test_blob.py b/storage/tests/unit/test_blob.py index 21443480b32f..be6e72e69053 100644 --- a/storage/tests/unit/test_blob.py +++ b/storage/tests/unit/test_blob.py @@ -405,6 +405,7 @@ def _check_session_mocks(self, client, fake_session_factory, # **MUTABLE** headers and it was mutated before the # second request. headers['range'] = 'bytes=3-5' + headers['accept-encoding'] = 'gzip' call = mock.call( 'GET', expected_url, data=None, headers=headers) self.assertEqual(fake_transport.request.mock_calls, [call, call]) @@ -493,7 +494,8 @@ def test_download_to_file_with_failure(self, fake_session_factory): fake_session_factory.assert_called_once_with(client._credentials) # Check that the transport was called once. transport.request.assert_called_once_with( - 'GET', blob.media_link, data=None, headers={}) + 'GET', blob.media_link, data=None, + headers={'accept-encoding':'gzip'}) @mock.patch('google.auth.transport.requests.AuthorizedSession') def test_download_to_file_wo_media_link(self, fake_session_factory): @@ -553,7 +555,8 @@ def _download_to_file_helper(self, fake_session_factory, use_chunks=False): # Check that exactly one transport was created. fake_session_factory.assert_called_once_with(client._credentials) fake_transport.request.assert_called_once_with( - 'GET', media_link, data=None, headers={}) + 'GET', media_link, data=None, + headers={'accept-encoding': 'gzip'}) def test_download_to_file_default(self): self._download_to_file_helper() @@ -645,6 +648,7 @@ def test_download_to_filename_w_key(self, fake_session_factory): 'X-Goog-Encryption-Key-Sha256': header_key_hash_value, 'X-Goog-Encryption-Algorithm': 'AES256', 'X-Goog-Encryption-Key': header_key_value, + 'accept-encoding':'gzip' } self._check_session_mocks( client, fake_session_factory, media_link, headers=key_headers)