问题描述
Google App Engine Files API现在支持blobstore blob编程式创建。
我尝试通过http获取图像并将其存储到blobstore:
file_name = files.blobstore.create(mime_type ='image / jpeg')
image = urllib2 (file_name,'a')作为f:
f.write(image)#行142
files.finalize(file_name).urlopen(url)
with files.open
blob_key = files.blobstore.get_blob_key(file_name)
此代码引发错误:
文件/Users/willmerydith/repos/spam/admin.py,第142行,后
f .write(image)
文件/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/api/files/file.py,第364行,写入
self._make_rpc_call_with_retry('Append',request,response)
文件 /Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/api/files/file.py,行472,位于_make_rpc_call_with_retry
_make_call(方法,请求,响应)
在_make_call中的文件/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/api/files/file.py,第229行
rpc.check_success()
文件/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/api/apiproxy_stub_map.py,行558,在check_success
self .__ rpc.CheckSuccess()
文件/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/api/ apiproxy_rpc.py,第156行,在_WaitImpl
self.request,self.response)
文件/ Applications / Goo gleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/api/apiproxy_stub.py,第80行,在MakeSyncCall
if request.ByteSize()> self .__ max_request_size:
文件/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/api/files/file_service_pb.py,第1923行,在ByteSize
n + = self.lengthString(len(self.data_))
AttributeError:addinfourl实例没有属性'__len__'
我怀疑这是因为我超出了尺寸限制而破裂。这是由于我写图像到blobstore的方式吗? Blobstores的大小限制是2 GB,我测试的图像小于200-300 KB。
urllib2.urlopen
返回一个 urllib2.addinourl
对象,而不是一个字符串。你不能直接写这个对象到你的文件对象。
$ b
尝试 f.write(image.read())
在第142行。
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.
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.
这篇关于如何上传图片从网址到Blobstore?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!