10000 Makes checking fstat for file optional. by dhermes · Pull Request #914 · googleapis/google-cloud-python · GitHub
[go: up one dir, main page]

Skip to content

Makes checking fstat for file optional. #914

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 18, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions gcloud/storage/blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,8 @@ def upload_from_file(self, file_obj, rewind=False, size=None,
:type size: int
:param size: The number of bytes to read from the file handle.
If not provided, we'll try to guess the size using
:func:`os.fstat`
:func:`os.fstat`. (If the file handle is not from the
filesystem this won't be possible.)

:type content_type: string or ``NoneType``
:param content_type: Optional type of content being uploaded.
Expand All @@ -382,6 +383,9 @@ def upload_from_file(self, file_obj, rewind=False, size=None,
``NoneType``
:param connection: Optional. The connection to use when sending
requests. If not provided, falls back to default.

:raises: :class:`ValueError` if size is not passed in and can not be
determined
"""
connection = _require_connection(connection)
content_type = (content_type or self._properties.get('contentType') or
Expand All @@ -392,7 +396,13 @@ def upload_from_file(self, file_obj, rewind=False, size=None,
file_obj.seek(0, os.SEEK_SET)

# Get the basic stats about the file.
total_bytes = size or os.fstat(file_obj.fileno()).st_size
total_bytes = size
if total_bytes is None:
if hasattr(file_obj, 'fileno'):
total_bytes = os.fstat(file_obj.fileno()).st_size
else:
raise ValueError('total bytes could not be determined. Please '
'pass an explicit size.')
headers = {
'Accept': 'application/json',
'Accept-Encoding': 'gzip, deflate',
Expand Down
10 changes: 10 additions & 0 deletions gcloud/storage/test_blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,16 @@ def test_download_as_string(self):
fetched = blob.download_as_string(connection=connection)
self.assertEqual(fetched, b'abcdef')

def test_upload_from_file_size_failure(self):
BLOB_NAME = 'blob-name'
bucket = _Bucket()
blob = self._makeOne(BLOB_NAME, bucket=bucket)
file_obj = object()
connection = _Connection()
with self.assertRaises(ValueError):
blob.upload_from_file(file_obj, size=None,
connection=connection)

def _upload_from_file_simple_test_helper(self, properties=None,
content_type_arg=None,
expected_content_type=None):
Expand Down
0