Skip to content

Commit 23cfb65

Browse files
fix: extract hashes correctly during download (#238)
Co-authored-by: Tres Seaver <tseaver@palladion.com>
1 parent acea15b commit 23cfb65

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

google/cloud/storage/blob.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -804,7 +804,7 @@ def _extract_headers_from_download(self, response):
804804

805805
digests = {}
806806
for encoded_digest in x_goog_hash.split(","):
807-
match = re.match(r"(crc32c|md5)=([\w\d]+)==", encoded_digest)
807+
match = re.match(r"(crc32c|md5)=([\w\d/]+={0,3})", encoded_digest)
808808
if match:
809809
method, digest = match.groups()
810810
digests[method] = digest

tests/system/test_system.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -910,6 +910,18 @@ def test_upload_blob_owner(self):
910910
owner = same_blob.owner
911911
self.assertIn(user_email, owner["entity"])
912912

913+
def test_blob_crc32_md5_hash(self):
914+
blob = self.bucket.blob("MyBuffer")
915+
file_contents = b"Hello World"
916+
blob.upload_from_string(file_contents)
917+
self.case_blobs_to_delete.append(blob)
918+
919+
download_blob = self.bucket.blob("MyBuffer")
920+
921+
self.assertEqual(download_blob.download_as_string(), file_contents)
922+
self.assertEqual(download_blob.crc32c, blob.crc32c)
923+
self.assertEqual(download_blob.md5_hash, blob.md5_hash)
924+
913925

914926
class TestUnicode(unittest.TestCase):
915927
@vpcsc_config.skip_if_inside_vpcsc

tests/unit/test_blob.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1476,8 +1476,8 @@ def test_download_as_string_w_response_headers(self):
14761476
self.assertEqual(blob.content_encoding, "gzip")
14771477
self.assertEqual(blob.cache_control, "max-age=1337;public")
14781478
self.assertEqual(blob.storage_class, "STANDARD")
1479-
self.assertEqual(blob.md5_hash, "CS9tHYTtyFntzj7B9nkkJQ")
1480-
self.assertEqual(blob.crc32c, "4gcgLQ")
1479+
self.assertEqual(blob.md5_hash, "CS9tHYTtyFntzj7B9nkkJQ==")
1480+
self.assertEqual(blob.crc32c, "4gcgLQ==")
14811481

14821482
def test_download_as_string_w_hash_response_header_none(self):
14831483
blob_name = "blob-name"

0 commit comments

Comments
 (0)