@@ -370,7 +370,8 @@ def upload_from_file(self, file_obj, rewind=False, size=None,
370
370
:type size: int
371
371
:param size: The number of bytes to read from the file handle.
372
372
If not provided, we'll try to guess the size using
373
- :func:`os.fstat`
373
+ :func:`os.fstat`. (If the file handle is not from the
374
+ filesystem this won't be possible.)
374
375
375
376
:type content_type: string or ``NoneType``
376
377
:param content_type: Optional type of content being uploaded.
@@ -382,6 +383,9 @@ def upload_from_file(self, file_obj, rewind=False, size=None,
382
383
``NoneType``
383
384
:param connection: Optional. The connection to use when sending
384
385
requests. If not provided, falls back to default.
386
+
387
+ :raises: :class:`ValueError` if size is not passed in and can not be
388
+ determined
385
389
"""
386
390
connection = _require_connection (connection )
387
391
content_type = (content_type or self ._properties .get ('contentType' ) or
@@ -392,7 +396,13 @@ def upload_from_file(self, file_obj, rewind=False, size=None,
392
396
file_obj .seek (0 , os .SEEK_SET )
393
397
394
398
# Get the basic stats about the file.
395
- total_bytes = size or os .fstat (file_obj .fileno ()).st_size
399
+ total_bytes = size
400
+ if total_bytes is None :
401
+ if hasattr (file_obj , 'fileno' ):
402
+ total_bytes = os .fstat (file_obj .fileno ()).st_size
403
+ else :
404
+ raise ValueError ('total bytes could not be determined. Please '
405
+ 'pass an explicit size.' )
396
406
headers = {
397
407
'Accept' : 'application/json' ,
398
408
'Accept-Encoding' : 'gzip, deflate' ,
0 commit comments