@@ -1538,6 +1538,21 @@ def _upload_fd(
15381538 The HttpResponse object from the finalize request.
15391539 """
15401540 offset = 0
1541+ http_options = http_options if http_options else self ._http_options
1542+ base_url = (
1543+ http_options .get ('base_url' )
1544+ if isinstance (http_options , dict )
1545+ else getattr (http_options , 'base_url' , None )
1546+ )
1547+ if base_url :
1548+ parsed_base = urlparse (base_url )
1549+ parsed_upload = urlparse (upload_url )
1550+ upload_url = urlunparse (
1551+ parsed_upload ._replace (
1552+ scheme = parsed_base .scheme , netloc = parsed_base .netloc
1553+ )
1554+ )
1555+
15411556 # Upload the file in chunks
15421557 while True :
15431558 file_chunk = file .read (CHUNK_SIZE )
@@ -1548,7 +1563,6 @@ def _upload_fd(
15481563 # If last chunk, finalize the upload.
15491564 if chunk_size + offset >= upload_size :
15501565 upload_command += ', finalize'
1551- http_options = http_options if http_options else self ._http_options
15521566 timeout = (
15531567 http_options .get ('timeout' )
15541568 if isinstance (http_options , dict )
@@ -1562,11 +1576,17 @@ def _upload_fd(
15621576 else self ._http_options .timeout
15631577 )
15641578 timeout_in_seconds = get_timeout_in_seconds (timeout )
1565- upload_headers = {
1579+ user_headers = (
1580+ http_options .get ('headers' , {})
1581+ if isinstance (http_options , dict )
1582+ else (getattr (http_options , 'headers' , {}) or {})
1583+ )
1584+ upload_headers = dict (user_headers ) if user_headers else {}
1585+ upload_headers .update ({
15661586 'X-Goog-Upload-Command' : upload_command ,
15671587 'X-Goog-Upload-Offset' : str (offset ),
15681588 'Content-Length' : str (chunk_size ),
1569- }
1589+ })
15701590 populate_server_timeout_header (upload_headers , timeout_in_seconds )
15711591 retry_count = 0
15721592 while retry_count < MAX_RETRY_COUNT :
@@ -1689,6 +1709,21 @@ async def _async_upload_fd(
16891709 The HttpResponse object from the finalized request.
16901710 """
16911711 offset = 0
1712+ http_options = http_options if http_options else self ._http_options
1713+ base_url = (
1714+ http_options .get ('base_url' )
1715+ if isinstance (http_options , dict )
1716+ else getattr (http_options , 'base_url' , None )
1717+ )
1718+ if base_url :
1719+ parsed_base = urlparse (base_url )
1720+ parsed_upload = urlparse (upload_url )
1721+ upload_url = urlunparse (
1722+ parsed_upload ._replace (
1723+ scheme = parsed_base .scheme , netloc = parsed_base .netloc
1724+ )
1725+ )
1726+
16921727 # Upload the file in chunks
16931728 if self ._use_aiohttp (): # pylint: disable=g-import-not-at-top
16941729 self ._aiohttp_session = await self ._get_aiohttp_session ()
@@ -1704,7 +1739,6 @@ async def _async_upload_fd(
17041739 # If last chunk, finalize the upload.
17051740 if chunk_size + offset >= upload_size :
17061741 upload_command += ', finalize'
1707- http_options = http_options if http_options else self ._http_options
17081742 timeout = (
17091743 http_options .get ('timeout' )
17101744 if isinstance (http_options , dict )
@@ -1718,11 +1752,17 @@ async def _async_upload_fd(
17181752 else self ._http_options .timeout
17191753 )
17201754 timeout_in_seconds = get_timeout_in_seconds (timeout )
1721- upload_headers = {
1755+ user_headers = (
1756+ http_options .get ('headers' , {})
1757+ if isinstance (http_options , dict )
1758+ else (getattr (http_options , 'headers' , {}) or {})
1759+ )
1760+ upload_headers = dict (user_headers ) if user_headers else {}
1761+ upload_headers .update ({
17221762 'X-Goog-Upload-Command' : upload_command ,
1723- 'X-Goog-Upload-Offset' : str (offset ),
1763+ 'X-Goog-Upload-Offset' : str (offset ),
17241764 'Content-Length' : str (chunk_size ),
1725- }
1765+ })
17261766 populate_server_timeout_header (upload_headers , timeout_in_seconds )
17271767
17281768 retry_count = 0
@@ -1780,7 +1820,6 @@ async def _async_upload_fd(
17801820 # If last chunk, finalize the upload.
17811821 if chunk_size + offset >= upload_size :
17821822 upload_command += ', finalize'
1783- http_options = http_options if http_options else self ._http_options
17841823 timeout = (
17851824 http_options .get ('timeout' )
17861825 if isinstance (http_options , dict )
@@ -1794,11 +1833,17 @@ async def _async_upload_fd(
17941833 else self ._http_options .timeout
17951834 )
17961835 timeout_in_seconds = get_timeout_in_seconds (timeout )
1797- upload_headers = {
1836+ user_headers = (
1837+ http_options .get ('headers' , {})
1838+ if isinstance (http_options , dict )
1839+ else (getattr (http_options , 'headers' , {}) or {})
1840+ )
1841+ upload_headers = dict (user_headers ) if user_headers else {}
1842+ upload_headers .update ({
17981843 'X-Goog-Upload-Command' : upload_command ,
17991844 'X-Goog-Upload-Offset' : str (offset ),
18001845 'Content-Length' : str (chunk_size ),
1801- }
1846+ })
18021847 populate_server_timeout_header (upload_headers , timeout_in_seconds )
18031848
18041849 retry_count = 0
0 commit comments