其他分享
首页 > 其他分享> > 如何将图像从url上传到Blobstore?

如何将图像从url上传到Blobstore?

作者:互联网

如果blobstore blob,Google App Engine文件API现在支持编程创建.

我试图通过http获取图像并将其存储到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) 

此代码抛出错误:

  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__'

我怀疑它正在破裂,因为我超出了尺寸限制.这是由于我将图像写入blobstore的方式吗? Blobstores的大小限制为2 GB,我测试的图像小于200-300 KB.

解决方法:

urllib2.urlopen返回urllib2.addinourl对象,而不是字符串.您无法将此对象直接写入文件对象.

尝试第142行的f.write(image.read()).

标签:blobstore,python,file-upload,google-app-engine
来源: https://codeday.me/bug/20190903/1797876.html