Google App Engine中的图片响应

时间:2012-02-11 21:41:04

标签: google-app-engine google-cloud-datastore python-imaging-library python-2.7

我通过执行以下操作将用户的图像保存为BlobProperty:

user.image = urlfetch.fetch(image_url).content

然后我使用如下的网址渲染该图像:

/image/user_id

图像必须保存,因为当我执行len(user.image)时,我会得到数千个数字。在本地实例上,图像呈现正常。在部署的应用程序上,我收到以下错误,当我转到图片网址时,浏览器中没有显示任何内容:

Traceback (most recent call last): File "/base/python27_runtime/python27_dist/lib/python2.7/wsgiref/handlers.py", line 86, in run self.finish_response() File "/base/python27_runtime/python27_dist/lib/python2.7/wsgiref/handlers.py", line 127, in finish_response self.write(data) File "/base/python27_runtime/python27_dist/lib/python2.7/wsgiref/handlers.py", line 202, in write assert type(data) is StringType,"write() argument must be string" AssertionError: write() argument must be string

此外,这是为图像提供服务的处理程序:

class ImageHandler(webapp2.RequestHandler):
""" Returns image based on id. """
def get(self, *args, **kwargs):
    user = db.get(
        db.Key.from_path('User', models.User.get_key_name(kwargs.get('id'))))
    if user.image:
        self.response.headers['Content-Type'] = "image/jpeg"
        self.response.out.write(user.image)
    else:
        self.response.out.write("No image")

为了澄清我尝试将内容类型设置为jpeg和png。事情在本地服务器上运行正常。任何帮助,将不胜感激。谢谢!

2 个答案:

答案 0 :(得分:2)

为什么不将图像写入blobstore然后使用send_blob()机制?

http://code.google.com/appengine/docs/python/blobstore/overview.html#Serving_a_Blob

答案 1 :(得分:0)

回答我的问题,这解决了一切:

self.response.out.write(str(user.image))

令人困惑,因为文档中的示例不会将BlobProperty强制转换为字符串。