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

时间:2011-04-14 15:51:04

标签: python google-app-engine file-upload 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。

2 个答案:

答案 0 :(得分:5)

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

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

答案 1 :(得分:0)

由于文件API已于2015年9月停用,因此无法使用。