From 2926d551d28ed94dbb2d9686cafe76fc680707e9 Mon Sep 17 00:00:00 2001 From: remmy Date: Tue, 15 Sep 2020 19:49:09 +0000 Subject: [PATCH] Only set CRC32C and MD5 hashes if they are present in the X-Goog-Hash header. --- google/cloud/storage/blob.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/google/cloud/storage/blob.py b/google/cloud/storage/blob.py index d61081c44..a7e6952bf 100644 --- a/google/cloud/storage/blob.py +++ b/google/cloud/storage/blob.py @@ -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,