Google App Engine 中的图像响应

发布于 2025-01-04 16:12:51 字数 1226 浏览 4 评论 0原文

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

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

然后我使用 url 渲染该图像,例如:

/image/user_id

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

Traceback(最近一次调用最后一次): 文件“/base/python27_runtime/python27_dist/lib/python2.7/wsgiref/handlers.py”,第 86 行,运行中 self.finish_response() 文件“/base/python27_runtime/python27_dist/lib/python2.7/wsgiref/handlers.py”,第 127 行,在 finish_response 中 自写(数据) 文件“/base/python27_runtime/python27_dist/lib/python2.7/wsgiref/handlers.py”,第202行,写入 断言类型(数据)是 StringType,“write() 参数必须是字符串” 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。并且在本地服务器上一切正常。任何帮助将不胜感激。谢谢!

I am saving a user's image as a BlobProperty by doing:

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

Then I'm rendering that image using a url such as:

/image/user_id

The image must be saving because because when I do len(user.image) I get a number in the thousands. And on the local instance the image renders ok. On the deployed app, I get the following error, and when I go to the image url nothing shows in the browser:

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

Also, here's the handler that serves the image:

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")

Just to clarify I tried both setting content-type to jpeg and png. And things are working ok on the local server. Any help would be appreciated. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

゛时过境迁 2025-01-11 16:12:51

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

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

Why not write the image to the blobstore and then use the send_blob() mechanism?

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

箜明 2025-01-11 16:12:51

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

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

这很令人困惑,因为文档中的示例没有将 BlobProperty 转换为字符串。

Answering my question, that fixes everything:

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

It's confusing because the example in the docs does not cast the BlobProperty as string.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文