Skip to content

Commit

Permalink
Only set CRC32C and MD5 hashes if they are present in the X-Goog-Hash
Browse files Browse the repository at this point in the history
header.
  • Loading branch information
remmykilonzo committed Sep 15, 2020
1 parent 8a560ef commit e03c44b
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions google/cloud/storage/blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -811,15 +811,16 @@ def _extract_headers_from_download(self, response):
# 'X-Goog-Hash': 'crc32c=4gcgLQ==,md5=CS9tHYTtyFntzj7B9nkkJQ==',
x_goog_hash = response.headers.get("X-Goog-Hash", "")

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

self.crc32c = digests.get("crc32c", self.crc32c)
self.md5_hash = digests.get("md5", self.md5_hash)
if x_goog_hash:
digests = {}
for encoded_digest in x_goog_hash.split(","):
match = re.match(r"(crc32c|md5)=([\w\d/\+/]+={0,3})", encoded_digest)
if match:
method, digest = match.groups()
digests[method] = digest

self.crc32c = digests.get("crc32c", None)
self.md5_hash = digests.get("md5", None)

def _do_download(
self,
Expand Down

0 comments on commit e03c44b

Please sign in to comment.