Skip to content

Commit cf0774a

Browse files
fix(blob): base64 includes additional characters (#258)
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">
1 parent e190f1c commit cf0774a

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

google/cloud/storage/blob.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -813,7 +813,7 @@ def _extract_headers_from_download(self, response):
813813

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

tests/unit/test_blob.py

+18
Original file line numberDiff line numberDiff line change
@@ -1522,6 +1522,24 @@ def test_download_as_string_w_response_headers(self):
15221522
self.assertEqual(blob.md5_hash, "CS9tHYTtyFntzj7B9nkkJQ==")
15231523
self.assertEqual(blob.crc32c, "4gcgLQ==")
15241524

1525+
response = self._mock_requests_response(
1526+
http_client.OK,
1527+
headers={
1528+
"Content-Type": "application/octet-stream",
1529+
"Content-Language": "en-US",
1530+
"Cache-Control": "max-age=1337;public",
1531+
"Content-Encoding": "gzip",
1532+
"X-Goog-Storage-Class": "STANDARD",
1533+
"X-Goog-Hash": "crc32c=4/c+LQ==,md5=CS9tHYTt/+ntzj7B9nkkJQ==",
1534+
},
1535+
content=b"",
1536+
)
1537+
blob._extract_headers_from_download(response)
1538+
self.assertEqual(blob.content_type, "application/octet-stream")
1539+
self.assertEqual(blob.content_language, "en-US")
1540+
self.assertEqual(blob.md5_hash, "CS9tHYTt/+ntzj7B9nkkJQ==")
1541+
self.assertEqual(blob.crc32c, "4/c+LQ==")
1542+
15251543
def test_download_as_string_w_hash_response_header_none(self):
15261544
blob_name = "blob-name"
15271545
client = mock.Mock(spec=["_http"])

0 commit comments

Comments
 (0)