Skip to content

Commit

Permalink
fix: extract hashes correctly during download (googleapis#238)
Browse files Browse the repository at this point in the history
Co-authored-by: Tres Seaver <tseaver@palladion.com>
  • Loading branch information
2 people authored and cojenco committed Oct 13, 2021
1 parent 179ffbd commit 3c90a87
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
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==")

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

0 comments on commit 3c90a87

Please sign in to comment.