How to upload image from url to Blobstore?

2019-05-14 15:51发布

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.

2条回答
太酷不给撩
2楼-- · 2019-05-14 16:39

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.

查看更多
一纸荒年 Trace。
3楼-- · 2019-05-14 16:54

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

查看更多
登录 后发表回答