8000 Merge pull request #914 from dhermes/fix-900 · googleapis/google-cloud-python@b0aef16 · GitHub
[go: up one dir, main page]

Skip to content

Commit b0aef16

Browse files
committed
Merge pull request #914 from dhermes/fix-900
Makes checking fstat for file optional.
2 parents b7eaf23 + 8358af2 commit b0aef16

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

gcloud/storage/blob.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,8 @@ def upload_from_file(self, file_obj, rewind=False, size=None,
370370
:type size: int
371371
:param size: The number of bytes to read from the file handle.
372372
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.)
374375
375376
:type content_type: string or ``NoneType``
376377
:param content_type: Optional type of content being uploaded.
@@ -382,6 +383,9 @@ def upload_from_file(self, file_obj, rewind=False, size=None,
382383
``NoneType``
383384
:param connection: Optional. The connection to use when sending
384385
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
385389
"""
386390
connection = _require_connection(connection)
387391
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,
392396
file_obj.seek(0, os.SEEK_SET)
393397

394398
# 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.')
396406
headers = {
397407
'Accept': 'application/json',
398408
'Accept-Encoding': 'gzip, deflate',

gcloud/storage/test_blob.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,16 @@ def test_download_as_string(self):
410410
fetched = blob.download_as_string(connection=connection)
411411
self.assertEqual(fetched, b'abcdef')
412412

413+
def test_upload_from_file_size_failure(self):
414+
BLOB_NAME = 'blob-name'
415+
bucket = _Bucket()
416+
blob = self._makeOne(BLOB_NAME, bucket=bucket)
417+
file_obj = object()
418+
connection = _Connection()
419+
with self.assertRaises(ValueError):
420+
blob.upload_from_file(file_obj, size=None,
421+
connection=connection)
422+
413423
def _upload_from_file_simple_test_helper(self, properties=None,
414424
content_type_arg=None,
415425
expected_content_type=None):

0 commit comments

Comments
 (0)
0