GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — feat(blob.py): auto-populate standard headers for non-chunked downloads by william-silversmith · Pull Request #204 · googleapis/python-storage

This repository was archived by the owner on Mar 31, 2026. It is now read-only.

feat(blob.py): auto-populate standard headers for non-chunked downloads #204

Merged
gcf-merge-on-green merged 13 commits into googleapis:master from seung-lab:master
Jul 23, 2020

Uh oh!

There was an error while loading. Please reload this page.

Merged

feat(blob.py): auto-populate standard headers for non-chunked downloads #204

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
13 commits
Select commit Hold shift + click to select a range
ccc169f
fix: for non-chunked downloads, automatically populate standard fields
william-silversmith Jul 14, 2020
46e2028
fix: don't assume GRMP has a stable interface
william-silversmith Jul 14, 2020
4d37401
refactor: move commented out code to a more sensible location
william-silversmith Jul 14, 2020
d6c9d28
feat: extract crc32c and md5 from X-Goog-Hash
william-silversmith Jul 14, 2020
dc7a207
fix: crashing when method is not defined
william-silversmith Jul 14, 2020
cd2763d
refactor(blob.py): change style to comport with black
william-silversmith Jul 14, 2020
42b62c8
refactor: remove unnecessary additional code path
william-silversmith Jul 14, 2020
55601ce
docs: added docstring to new method
william-silversmith Jul 14, 2020
175f4d8
fix: ensure x_goog_hash is set if an error is encountered
william-silversmith Jul 14, 2020
c8f9226
Merge branch 'master' into master
william-silversmith Jul 21, 2020
0deef45
refactor: no need to use 'raw' with a requests.Response
william-silversmith Jul 21, 2020
7b54722
test: extract headers from response
william-silversmith Jul 22, 2020
6a733d6
chore: reformat whitespace with black
william-silversmith Jul 22, 2020
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

33 changes: 31 additions & 2 deletions 33 google/cloud/storage/blob.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from io import BytesIO
import mimetypes
import os
import re
import warnings
import six

Expand Down Expand Up @@ -783,6 +784,34 @@ def _get_download_url(
)
return _add_query_parameters(base_url, name_value_pairs)

def _extract_headers_from_download(self, response):
"""Extract headers from a non-chunked request's http object.

This avoids the need to make a second request for commonly used
headers.

:type response:
:class requests.models.Response
:param response: The server response from downloading a non-chunked file
"""
self.content_encoding = response.headers.get("Content-Encoding", None)
self.content_type = response.headers.get("Content-Type", None)
self.cache_control = response.headers.get("Cache-Control", None)
self.storage_class = response.headers.get("X-Goog-Storage-Class", None)
self.content_language = response.headers.get("Content-Language", None)
# '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]+)==", 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,
transport,
Expand Down Expand Up @@ -840,8 +869,8 @@ def _do_download(
download = klass(
download_url, stream=file_obj, headers=headers, start=start, end=end
)
download.consume(transport, timeout=timeout)

response = download.consume(transport, timeout=timeout)
self._extract_headers_from_download(response)
else:

if raw_download:
Expand Down
31 changes: 31 additions & 0 deletions 31 tests/unit/test_blob.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -1451,6 +1451,37 @@ def _download_as_string_helper(self, raw_download, timeout=None):
stream = blob._do_download.mock_calls[0].args[1]
self.assertIsInstance(stream, io.BytesIO)

def test_download_as_string_w_response_headers(self):
blob_name = "blob-name"
client = mock.Mock(spec=["_http"])
bucket = _Bucket(client)
media_link = "http://example.com/media/"
properties = {"mediaLink": media_link}
blob = self._make_one(blob_name, bucket=bucket, properties=properties)

response = self._mock_requests_response(
http_client.OK,
headers={
"Content-Type": "application/json",
"Content-Language": "ko-kr",
"Cache-Control": "max-age=1337;public",
"Content-Encoding": "gzip",
"X-Goog-Storage-Class": "STANDARD",
"X-Goog-Hash": "crc32c=4gcgLQ==,md5=CS9tHYTtyFntzj7B9nkkJQ==",
},
# { "x": 5 } gzipped
content=b"\x1f\x8b\x08\x00\xcfo\x17_\x02\xff\xabVP\xaaP\xb2R0U\xa8\x05\x00\xa1\xcaQ\x93\n\x00\x00\x00",
)
blob._extract_headers_from_download(response)

self.assertEqual(blob.content_type, "application/json")
self.assertEqual(blob.content_language, "ko-kr")
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")

def test_download_as_string_w_generation_match(self):
GENERATION_NUMBER = 6
MEDIA_LINK = "http://example.com/media/"
Expand Down
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.