Skip to content

Commit

Permalink
fix(blob): base64 includes additional characters (googleapis#258)
Browse files Browse the repository at this point in the history
Hashes were not being parsed correctly. I forgot that base64 includes the "+" and "/" characters. 

https://en.wikipedia.org/wiki/Base64

<img width="656" alt="image" src="https://user-images.githubusercontent.com/2517065/91491542-3329ee80-e882-11ea-9665-4eaa564b406b.png">
  • Loading branch information
william-silversmith authored and remmykilonzo committed Sep 15, 2020
1 parent 4d3faad commit 818dc5a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion google/cloud/storage/blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -813,7 +813,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/]+={0,3})", 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
18 changes: 18 additions & 0 deletions tests/unit/test_blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -1522,6 +1522,24 @@ def test_download_as_string_w_response_headers(self):
self.assertEqual(blob.md5_hash, "CS9tHYTtyFntzj7B9nkkJQ==")
self.assertEqual(blob.crc32c, "4gcgLQ==")

response = self._mock_requests_response(
http_client.OK,
headers={
"Content-Type": "application/octet-stream",
"Content-Language": "en-US",
"Cache-Control": "max-age=1337;public",
"Content-Encoding": "gzip",
"X-Goog-Storage-Class": "STANDARD",
"X-Goog-Hash": "crc32c=4/c+LQ==,md5=CS9tHYTt/+ntzj7B9nkkJQ==",
},
content=b"",
)
blob._extract_headers_from_download(response)
self.assertEqual(blob.content_type, "application/octet-stream")
self.assertEqual(blob.content_language, "en-US")
self.assertEqual(blob.md5_hash, "CS9tHYTt/+ntzj7B9nkkJQ==")
self.assertEqual(blob.crc32c, "4/c+LQ==")

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

0 comments on commit 818dc5a

Please sign in to comment.