GeezerVison is showing github.com — Blob.upload_from_file doesn't work for file-like objects without size parameter · Issue #900 · googleapis/google-cloud-python
Blob.upload_from_file doesn't work for file-like objects without size parameter #900DescriptionWhen using StringIO, io.BytesIO or other file-like objects (such as Flask/werkzeug's file upload object), trying to upload via Blob.upload_from_file doesn't work because it attempts to find the size of the object using os.stat throwing a AttributeError: StringIO instance has no attribute 'fileno'. Passing in the size manually fixes this, but imo this inconsistency shouldn't burden the user as there are other ways than fileno to determine the size of a StringIO or io.BytesIO instance (seek, tell).
At any rate, the error message could be more helpful. Activitydhermes commented on May 29, 2015 Can you give a stacktrace of the error message?
The offending line is
# Get the basic stats about the file.
total_bytes = size or os.fstat(file_obj.fileno()).st_size
which as @jonparrott points out, assumes the file-like object is a file (from the stdlib). theacodes commented on May 29, 2015 ContributorAuthor More actions Traceback (most recent call last):
File "/Users/jonwayne/workspace/getting-started-python/env/lib/python2.7/site-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "/Users/jonwayne/workspace/getting-started-python/env/lib/python2.7/site-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/Users/jonwayne/workspace/getting-started-python/env/lib/python2.7/site-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "/Users/jonwayne/workspace/getting-started-python/env/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/Users/jonwayne/workspace/getting-started-python/env/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Users/jonwayne/workspace/getting-started-python/env/lib/python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/Users/jonwayne/workspace/getting-started-python/env/lib/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/Users/jonwayne/workspace/getting-started-python/env/lib/python2.7/site-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/jonwayne/workspace/getting-started-python/oauth2/__init__.py", line 33, in decr
return f(*args, **kwargs)
File "/Users/jonwayne/workspace/getting-started-python/bookshelf/crud.py", line 57, in add
image_url = upload_image_file(request.files.get('image'))
File "/Users/jonwayne/workspace/getting-started-python/bookshelf/crud.py", line 25, in upload_image_file
current_app.config['ALLOWED_EXTENSIONS']
File "/Users/jonwayne/workspace/getting-started-python/storage/__init__.py", line 52, in upload_file
blob.upload_from_file(file)
File "/Users/jonwayne/workspace/getting-started-python/env/src/gcloud/gcloud/storage/blob.py", line 395, in upload_from_file
total_bytes = size or os.fstat(file_obj.fileno()).st_size
UnsupportedOperation: fileno
dhermes commented on May 30, 2015 Ha, ok. Doesn't provide anything we didn't know. theacodes commented on May 30, 2015 ContributorAuthor More actions Heh, let me know if there's anything else I can provide to help here. The file-like object is a flask file upload (werkzeug.FileStorage). It fails likewise when using blob.upload_from_file(file) and blob.upload_from_file(file.stream). added a commit that references this issue on Jun 6, 2015This issue will close once commit 8358af2 is merged into the 'master' branch.8358af2
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|