99from ..models import Bucket
1010from ..tracing ._traced import traced
1111from ._base_service import BaseService
12+ import os
13+ import mimetypes
1214
1315
1416def _upload_from_memory_input_processor (inputs : Dict [str , Any ]) -> Dict [str , Any ]:
@@ -154,7 +156,7 @@ def upload(
154156 key : Optional [str ] = None ,
155157 name : Optional [str ] = None ,
156158 blob_file_path : str ,
157- content_type : str ,
159+ content_type : Optional [ str ] = None ,
158160 source_path : Optional [str ] = None ,
159161 content : Optional [Union [str , bytes ]] = None ,
160162 folder_key : Optional [str ] = None ,
@@ -166,7 +168,7 @@ def upload(
166168 key (Optional[str]): The key of the bucket.
167169 name (Optional[str]): The name of the bucket.
168170 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.
171+ content_type (Optional[ str] ): The MIME type of the file. For file inputs this is computed dynamically. Default is "application/octet-stream" .
170172 source_path (Optional[str]): The local path of the file to upload.
171173 content (Optional[Union[str, bytes]]): The content to upload (string or bytes).
172174 folder_key (Optional[str]): The key of the folder where the bucket resides.
@@ -185,9 +187,17 @@ def upload(
185187 name = name , key = key , folder_key = folder_key , folder_path = folder_path
186188 )
187189
190+ # if source_path, dynamically detect the mime type
191+ # default to application/octet-stream
192+ if source_path :
193+ _content_type , _ = mimetypes .guess_type (source_path )
194+ else :
195+ _content_type = content_type
196+ _content_type = _content_type or "application/octet-stream"
197+
188198 spec = self ._retrieve_writeri_spec (
189199 bucket .id ,
190- content_type ,
200+ _content_type ,
191201 blob_file_path ,
192202 folder_key = folder_key ,
193203 folder_path = folder_path ,
@@ -209,6 +219,8 @@ def upload(
209219 )
210220 }
211221
222+ headers ["Content-Type" ] = _content_type
223+
212224 if content is not None :
213225 if isinstance (content , str ):
214226 content = content .encode ("utf-8" )
@@ -237,7 +249,7 @@ async def upload_async(
237249 key : Optional [str ] = None ,
238250 name : Optional [str ] = None ,
239251 blob_file_path : str ,
240- content_type : str ,
252+ content_type : Optional [ str ] = None ,
241253 source_path : Optional [str ] = None ,
242254 content : Optional [Union [str , bytes ]] = None ,
243255 folder_key : Optional [str ] = None ,
@@ -249,8 +261,9 @@ async def upload_async(
249261 key (Optional[str]): The key of the bucket.
250262 name (Optional[str]): The name of the bucket.
251263 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.
264+ content_type (Optional[ str] ): The MIME type of the file. For file inputs this is computed dynamically. Default is "application/octet-stream" .
253265 source_path (str): The local path of the file to upload.
266+ content (Optional[Union[str, bytes]]): The content to upload (string or bytes).
254267 folder_key (Optional[str]): The key of the folder where the bucket resides.
255268 folder_path (Optional[str]): The path of the folder where the bucket resides.
256269
@@ -267,9 +280,17 @@ async def upload_async(
267280 name = name , key = key , folder_key = folder_key , folder_path = folder_path
268281 )
269282
283+ # if source_path, dynamically detect the mime type
284+ # default to application/octet-stream
285+ if source_path :
286+ _content_type , _ = mimetypes .guess_type (source_path )
287+ else :
288+ _content_type = content_type
289+ _content_type = _content_type or "application/octet-stream"
290+
270291 spec = self ._retrieve_writeri_spec (
271292 bucket .id ,
272- content_type ,
293+ _content_type ,
273294 blob_file_path ,
274295 folder_key = folder_key ,
275296 folder_path = folder_path ,
@@ -293,6 +314,8 @@ async def upload_async(
293314 )
294315 }
295316
317+ headers ["Content-Type" ] = _content_type
318+
296319 if content is not None :
297320 if isinstance (content , str ):
298321 content = content .encode ("utf-8" )
0 commit comments