How to upload image from url to Blobstore?

不羁岁月 提交于 2019-12-07 13:29:06

问题


The Google App Engine Files API now supports programmatic creation if blobstore blobs.

I'm trying this out by attempting to fetch an image over http and store it to the blobstore:

    file_name = files.blobstore.create(mime_type='image/jpeg')
    image = urllib2.urlopen(url)
    with files.open(file_name, 'a') as f:
        f.write(image)  # LINE 142

    files.finalize(file_name)   
    blob_key = files.blobstore.get_blob_key(file_name) 

This code is throwing the error:

  File "/Users/willmerydith/repos/spam/admin.py", line 142, in post
    f.write(image)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/api/files/file.py", line 364, in write
    self._make_rpc_call_with_retry('Append', request, response)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/api/files/file.py", line 472, in _make_rpc_call_with_retry
    _make_call(method, request, response)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/api/files/file.py", line 229, in _make_call
    rpc.check_success()
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/api/apiproxy_stub_map.py", line 558, in check_success
    self.__rpc.CheckSuccess()
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/api/apiproxy_rpc.py", line 156, in _WaitImpl
    self.request, self.response)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/api/apiproxy_stub.py", line 80, in MakeSyncCall
    if request.ByteSize() > self.__max_request_size:
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/api/files/file_service_pb.py", line 1923, in ByteSize
    n += self.lengthString(len(self.data_))
AttributeError: addinfourl instance has no attribute '__len__'

I suspect it is breaking because I am exceeding a size limit. Is that due to the way I am writing the image to the blobstore? The size limit for Blobstores is 2 GB, and the images I am testing are less than 200-300 KB.


回答1:


urllib2.urlopen returns a urllib2.addinourl object, rather than a string. You can't write this object directly to your file object.

Try f.write(image.read()) on line 142.




回答2:


This is not working anymore as the files API has been disabled in September 2015.



来源:https://stackoverflow.com/questions/5665966/how-to-upload-image-from-url-to-blobstore

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!