Skip to content
Merged
17 changes: 17 additions & 0 deletions google/cloud/storage/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -581,3 +581,20 @@ def _api_core_retry_to_resumable_media_retry(retry, num_retries=None):
return resumable_media.RetryStrategy(max_retries=num_retries)
else:
return resumable_media.RetryStrategy(max_retries=0)


def _get_default_headers(user_agent):
"""Get the headers for a request.

Args:
user_agent (str): The user-agent for requests.
Returns:
Dict: The headers to be used for the request.
"""
return {
"Accept": "application/json",
"Accept-Encoding": "gzip, deflate",
"User-Agent": user_agent,
"X-Goog-Api-Client": user_agent,
"content-type": "application/json",
}
6 changes: 5 additions & 1 deletion google/cloud/storage/blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
from google.cloud.storage._helpers import _bucket_bound_hostname_url
from google.cloud.storage._helpers import _raise_if_more_than_one_set
from google.cloud.storage._helpers import _api_core_retry_to_resumable_media_retry
from google.cloud.storage._helpers import _get_default_headers
from google.cloud.storage._signing import generate_signed_url_v2
from google.cloud.storage._signing import generate_signed_url_v4
from google.cloud.storage._helpers import _NUM_RETRIES_MESSAGE
Expand Down Expand Up @@ -1739,7 +1740,10 @@ def _get_upload_arguments(self, content_type):
* An object metadata dictionary
* The ``content_type`` as a string (according to precedence)
"""
headers = _get_encryption_headers(self._encryption_key)
headers = {
**_get_encryption_headers(self._encryption_key),
**_get_default_headers(self.bucket.client._connection.user_agent)
}
object_metadata = self._get_writable_metadata()
content_type = self._get_content_type(content_type)
return headers, object_metadata, content_type
Expand Down
5 changes: 5 additions & 0 deletions google/cloud/storage/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from google.cloud._helpers import _LocalStack, _NOW
from google.cloud.client import ClientWithProject
from google.cloud.exceptions import NotFound
from google.cloud.storage._helpers import _get_default_headers
from google.cloud.storage._helpers import _get_environ_project
from google.cloud.storage._helpers import _get_storage_host
from google.cloud.storage._helpers import _BASE_STORAGE_URI
Expand Down Expand Up @@ -1131,6 +1132,10 @@ def download_blob_to_file(
_add_etag_match_headers(
headers, if_etag_match=if_etag_match, if_etag_not_match=if_etag_not_match,
)
headers = {
**headers,
**_get_default_headers(self._connection.user_agent)
}

transport = self._http
try:
Expand Down