@@ -887,6 +887,29 @@ def _fix_up_method_description(method_desc, root_desc, schema):
887887 return path_url , http_method , method_id , accept , max_size , media_path_url
888888
889889
890+ def _fix_up_media_path_base_url (media_path_url , base_url ):
891+ """
892+ Update the media upload base url if its netloc doesn't match base url netloc.
893+
894+ This can happen in case the base url was overridden by
895+ client_options.api_endpoint.
896+
897+ Args:
898+ media_path_url: String; the absolute URI for media upload.
899+ base_url: string, base URL for the API. All requests are relative to this URI.
900+
901+ Returns:
902+ String; the absolute URI for media upload.
903+ """
904+ parsed_media_url = urllib .parse .urlparse (media_path_url )
905+ parsed_base_url = urllib .parse .urlparse (base_url )
906+ if parsed_media_url .netloc == parsed_base_url .netloc :
907+ return media_path_url
908+ return urllib .parse .urlunparse (
909+ parsed_media_url ._replace (netloc = parsed_base_url .netloc )
910+ )
911+
912+
890913def _urljoin (base , url ):
891914 """Custom urljoin replacement supporting : before / in url."""
892915 # In general, it's unsafe to simply join base and url. However, for
@@ -1133,6 +1156,7 @@ def method(self, **kwargs):
11331156 # Use the media path uri for media uploads
11341157 expanded_url = uritemplate .expand (mediaPathUrl , params )
11351158 url = _urljoin (self ._baseUrl , expanded_url + query )
1159+ url = _fix_up_media_path_base_url (url , self ._baseUrl )
11361160 if media_upload .resumable ():
11371161 url = _add_query_parameter (url , "uploadType" , "resumable" )
11381162
0 commit comments