1+ import mimetypes
12from typing import Any , Dict , Optional , Union
23
34import httpx
@@ -154,7 +155,7 @@ def upload(
154155 key : Optional [str ] = None ,
155156 name : Optional [str ] = None ,
156157 blob_file_path : str ,
157- content_type : str ,
158+ content_type : Optional [ str ] = None ,
158159 source_path : Optional [str ] = None ,
159160 content : Optional [Union [str , bytes ]] = None ,
160161 folder_key : Optional [str ] = None ,
@@ -166,7 +167,7 @@ def upload(
166167 key (Optional[str]): The key of the bucket.
167168 name (Optional[str]): The name of the bucket.
168169 blob_file_path (str): The path where the file will be stored in the bucket.
169- content_type (str): The MIME type of the file.
170+ content_type (Optional[ str] ): The MIME type of the file. For file inputs this is computed dynamically. Default is "application/octet-stream" .
170171 source_path (Optional[str]): The local path of the file to upload.
171172 content (Optional[Union[str, bytes]]): The content to upload (string or bytes).
172173 folder_key (Optional[str]): The key of the folder where the bucket resides.
@@ -185,9 +186,17 @@ def upload(
185186 name = name , key = key , folder_key = folder_key , folder_path = folder_path
186187 )
187188
189+ # if source_path, dynamically detect the mime type
190+ # default to application/octet-stream
191+ if source_path :
192+ _content_type , _ = mimetypes .guess_type (source_path )
193+ else :
194+ _content_type = content_type
195+ _content_type = _content_type or "application/octet-stream"
196+
188197 spec = self ._retrieve_writeri_spec (
189198 bucket .id ,
190- content_type ,
199+ _content_type ,
191200 blob_file_path ,
192201 folder_key = folder_key ,
193202 folder_path = folder_path ,
@@ -209,6 +218,8 @@ def upload(
209218 )
210219 }
211220
221+ headers ["Content-Type" ] = _content_type
222+
212223 if content is not None :
213224 if isinstance (content , str ):
214225 content = content .encode ("utf-8" )
@@ -237,7 +248,7 @@ async def upload_async(
237248 key : Optional [str ] = None ,
238249 name : Optional [str ] = None ,
239250 blob_file_path : str ,
240- content_type : str ,
251+ content_type : Optional [ str ] = None ,
241252 source_path : Optional [str ] = None ,
242253 content : Optional [Union [str , bytes ]] = None ,
243254 folder_key : Optional [str ] = None ,
@@ -249,8 +260,9 @@ async def upload_async(
249260 key (Optional[str]): The key of the bucket.
250261 name (Optional[str]): The name of the bucket.
251262 blob_file_path (str): The path where the file will be stored in the bucket.
252- content_type (str): The MIME type of the file.
263+ content_type (Optional[ str] ): The MIME type of the file. For file inputs this is computed dynamically. Default is "application/octet-stream" .
253264 source_path (str): The local path of the file to upload.
265+ content (Optional[Union[str, bytes]]): The content to upload (string or bytes).
254266 folder_key (Optional[str]): The key of the folder where the bucket resides.
255267 folder_path (Optional[str]): The path of the folder where the bucket resides.
256268
@@ -267,9 +279,17 @@ async def upload_async(
267279 name = name , key = key , folder_key = folder_key , folder_path = folder_path
268280 )
269281
282+ # if source_path, dynamically detect the mime type
283+ # default to application/octet-stream
284+ if source_path :
285+ _content_type , _ = mimetypes .guess_type (source_path )
286+ else :
287+ _content_type = content_type
288+ _content_type = _content_type or "application/octet-stream"
289+
270290 spec = self ._retrieve_writeri_spec (
271291 bucket .id ,
272- content_type ,
292+ _content_type ,
273293 blob_file_path ,
274294 folder_key = folder_key ,
275295 folder_path = folder_path ,
@@ -293,6 +313,8 @@ async def upload_async(
293313 )
294314 }
295315
316+ headers ["Content-Type" ] = _content_type
317+
296318 if content is not None :
297319 if isinstance (content , str ):
298320 content = content .encode ("utf-8" )
0 commit comments