@@ -636,33 +636,31 @@ def upload(
636636            'Unknown mime type: Could not determine the mimetype for your' 
637637            ' file\n     please set the `mime_type` argument' 
638638        )
639+     request_specific_headers  =  (
640+         (config_model .http_options .headers  or  {})
641+         if  config_model .http_options 
642+         else  {}
643+     )
644+ 
645+     # Define and merge headers, with upload headers taking priority 
646+     create_final_headers  =  {
647+         ** request_specific_headers ,
648+         'Content-Type' : 'application/json' ,
649+         'X-Goog-Upload-Protocol' : 'resumable' ,
650+         'X-Goog-Upload-Command' : 'start' ,
651+         'X-Goog-Upload-Header-Content-Length' : f'{ file_obj .size_bytes }  ,
652+         'X-Goog-Upload-Header-Content-Type' : f'{ file_obj .mime_type }  ,
653+     }
654+ 
655+     create_http_options  =  types .HttpOptions (
656+         api_version = '' ,
657+         headers = create_final_headers ,
658+     )
639659
640-     http_options : types .HttpOptions 
641-     if  config_model  and  config_model .http_options :
642-       http_options  =  config_model .http_options 
643-       http_options .api_version  =  '' 
644-       http_options .headers  =  {
645-           'Content-Type' : 'application/json' ,
646-           'X-Goog-Upload-Protocol' : 'resumable' ,
647-           'X-Goog-Upload-Command' : 'start' ,
648-           'X-Goog-Upload-Header-Content-Length' : f'{ file_obj .size_bytes }  ,
649-           'X-Goog-Upload-Header-Content-Type' : f'{ file_obj .mime_type }  ,
650-       }
651-     else :
652-       http_options  =  types .HttpOptions (
653-           api_version = '' ,
654-           headers = {
655-               'Content-Type' : 'application/json' ,
656-               'X-Goog-Upload-Protocol' : 'resumable' ,
657-               'X-Goog-Upload-Command' : 'start' ,
658-               'X-Goog-Upload-Header-Content-Length' : f'{ file_obj .size_bytes }  ,
659-               'X-Goog-Upload-Header-Content-Type' : f'{ file_obj .mime_type }  ,
660-           },
661-       )
662660    response  =  self ._create (
663661        file = file_obj ,
664662        config = types .CreateFileConfig (
665-             http_options = http_options , should_return_http_response = True 
663+             http_options = create_http_options , should_return_http_response = True 
666664        ),
667665    )
668666
@@ -679,11 +677,17 @@ def upload(
679677
680678    if  isinstance (file , io .IOBase ):
681679      return_file  =  self ._api_client .upload_file (
682-           file , upload_url , file_obj .size_bytes , http_options = http_options 
680+           file ,
681+           upload_url ,
682+           file_obj .size_bytes ,
683+           http_options = config_model .http_options ,
683684      )
684685    else :
685686      return_file  =  self ._api_client .upload_file (
686-           fs_path , upload_url , file_obj .size_bytes , http_options = http_options 
687+           fs_path ,
688+           upload_url ,
689+           file_obj .size_bytes ,
690+           http_options = config_model .http_options ,
687691      )
688692
689693    return  types .File ._from_response (
@@ -1122,58 +1126,67 @@ async def upload(
11221126            ' file\n     please set the `mime_type` argument' 
11231127        )
11241128
1125-     http_options : types .HttpOptions 
1126-     if  config_model  and  config_model .http_options :
1127-       http_options  =  config_model .http_options 
1128-       http_options .api_version  =  '' 
1129-       http_options .headers  =  {
1130-           'Content-Type' : 'application/json' ,
1131-           'X-Goog-Upload-Protocol' : 'resumable' ,
1132-           'X-Goog-Upload-Command' : 'start' ,
1133-           'X-Goog-Upload-Header-Content-Length' : f'{ file_obj .size_bytes }  ,
1134-           'X-Goog-Upload-Header-Content-Type' : f'{ file_obj .mime_type }  ,
1135-       }
1136-     else :
1137-       http_options  =  types .HttpOptions (
1138-           api_version = '' ,
1139-           headers = {
1140-               'Content-Type' : 'application/json' ,
1141-               'X-Goog-Upload-Protocol' : 'resumable' ,
1142-               'X-Goog-Upload-Command' : 'start' ,
1143-               'X-Goog-Upload-Header-Content-Length' : f'{ file_obj .size_bytes }  ,
1144-               'X-Goog-Upload-Header-Content-Type' : f'{ file_obj .mime_type }  ,
1145-           },
1146-       )
1129+     request_specific_headers  =  (
1130+         (config_model .http_options .headers  or  {})
1131+         if  config_model .http_options 
1132+         else  {}
1133+     )
1134+ 
1135+     # Define and merge headers, with upload headers taking priority 
1136+     create_final_headers  =  {
1137+         ** request_specific_headers ,
1138+         'Content-Type' : 'application/json' ,
1139+         'X-Goog-Upload-Protocol' : 'resumable' ,
1140+         'X-Goog-Upload-Command' : 'start' ,
1141+         'X-Goog-Upload-Header-Content-Length' : f'{ file_obj .size_bytes }  ,
1142+         'X-Goog-Upload-Header-Content-Type' : f'{ file_obj .mime_type }  ,
1143+     }
1144+ 
1145+     create_http_options  =  types .HttpOptions (
1146+         api_version = '' ,
1147+         headers = create_final_headers ,
1148+     )
1149+ 
11471150    response  =  await  self ._create (
11481151        file = file_obj ,
11491152        config = types .CreateFileConfig (
1150-             http_options = http_options , should_return_http_response = True 
1153+             http_options = create_http_options , should_return_http_response = True 
11511154        ),
11521155    )
11531156    if  (
11541157        response .sdk_http_response  is  None 
11551158        or  response .sdk_http_response .headers  is  None 
1156-         or  (
1157-             'x-goog-upload-url'  not  in response .sdk_http_response .headers 
1158-             and  'X-Goog-Upload-URL'  not  in response .sdk_http_response .headers 
1159-         )
11601159    ):
11611160      raise  KeyError (
1162-           'Failed to create file. Upload URL did not returned from the create' 
1163-           ' file request.' 
1161+           'Failed to create file. The SDK HTTP response or its headers were' 
1162+           ' missing.' 
1163+       )
1164+ 
1165+     upload_url  =  None 
1166+     for  key , value  in  response .sdk_http_response .headers .items ():
1167+       if  key .lower () ==  'x-goog-upload-url' :
1168+         upload_url  =  value 
1169+         break   # Stop as soon as we find the first match 
1170+ 
1171+     if  upload_url  is  None :
1172+       raise  KeyError (
1173+           'Failed to create file. Upload URL was not returned in the response' 
1174+           ' headers.' 
11641175      )
1165-     elif  'x-goog-upload-url'  in  response .sdk_http_response .headers :
1166-       upload_url  =  response .sdk_http_response .headers ['x-goog-upload-url' ]
1167-     else :
1168-       upload_url  =  response .sdk_http_response .headers ['X-Goog-Upload-URL' ]
11691176
11701177    if  isinstance (file , io .IOBase ):
11711178      return_file  =  await  self ._api_client .async_upload_file (
1172-           file , upload_url , file_obj .size_bytes , http_options = http_options 
1179+           file ,
1180+           upload_url ,
1181+           file_obj .size_bytes ,
1182+           http_options = config_model .http_options ,
11731183      )
11741184    else :
11751185      return_file  =  await  self ._api_client .async_upload_file (
1176-           fs_path , upload_url , file_obj .size_bytes , http_options = http_options 
1186+           fs_path ,
1187+           upload_url ,
1188+           file_obj .size_bytes ,
1189+           http_options = config_model .http_options ,
11771190      )
11781191
11791192    return  types .File ._from_response (
0 commit comments