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: extract hashes correctly during download #238

Merged
merged 8 commits into from Aug 14, 2020
2 changes: 1 addition & 1 deletion google/cloud/storage/blob.py
Expand Up @@ -804,7 +804,7 @@ def _extract_headers_from_download(self, response):

digests = {}
for encoded_digest in x_goog_hash.split(","):
match = re.match(r"(crc32c|md5)=([\w\d]+)==", encoded_digest)
match = re.match(r"(crc32c|md5)=([\w\d/]+={0,3})", encoded_digest)
if match:
method, digest = match.groups()
digests[method] = digest
Expand Down
12 changes: 12 additions & 0 deletions tests/system/test_system.py
Expand Up @@ -910,6 +910,18 @@ def test_upload_blob_owner(self):
owner = same_blob.owner
self.assertIn(user_email, owner["entity"])

def test_blob_crc32_md5_hash(self):
blob = self.bucket.blob("MyBuffer")
file_contents = b"Hello World"
blob.upload_from_string(file_contents)
self.case_blobs_to_delete.append(blob)

download_blob = self.bucket.blob("MyBuffer")

self.assertEqual(download_blob.download_as_string(), file_contents)
self.assertEqual(download_blob.crc32c, blob.crc32c)
self.assertEqual(download_blob.md5_hash, blob.md5_hash)


class TestUnicode(unittest.TestCase):
@vpcsc_config.skip_if_inside_vpcsc
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/test_blob.py
Expand Up @@ -1476,8 +1476,8 @@ def test_download_as_string_w_response_headers(self):
self.assertEqual(blob.content_encoding, "gzip")
self.assertEqual(blob.cache_control, "max-age=1337;public")
self.assertEqual(blob.storage_class, "STANDARD")
self.assertEqual(blob.md5_hash, "CS9tHYTtyFntzj7B9nkkJQ")
self.assertEqual(blob.crc32c, "4gcgLQ")
self.assertEqual(blob.md5_hash, "CS9tHYTtyFntzj7B9nkkJQ==")
self.assertEqual(blob.crc32c, "4gcgLQ==")
tseaver marked this conversation as resolved.
Show resolved Hide resolved

def test_download_as_string_w_hash_response_header_none(self):
blob_name = "blob-name"
Expand Down