gpt4 book ai didi

python - Pyramid + ZODB 图像存储

转载 作者:行者123 更新时间:2023-11-30 23:38:23 26 4
gpt4 key购买 nike

我有一个上传表单,它接受 zip 文件,并有一个方法可以解压缩它并从中获取每个文件。从它的 md5 哈希中创建一个唯一的 id 并将它们存储在字典中;

dict[uid] = imagebinary

并返回它,以便表单可以将它们存储到 ZODB 中。我无法像这样存储图像,因为会出现此错误;

    2013-01-31 08:59:59,061 ERROR [waitress][Dummy-5] Exception when serving /
Traceback (most recent call last):
File "/home/maverick/inigo/sources/devenv/lib/python2.7/site-packages/waitress-0.8.2-py2.7.egg/waitress/channel.py", line 329, in service
task.service()
File "/home/maverick/inigo/sources/devenv/lib/python2.7/site-packages/waitress-0.8.2-py2.7.egg/waitress/task.py", line 173, in service
self.execute()
File "/home/maverick/inigo/sources/devenv/lib/python2.7/site-packages/waitress-0.8.2-py2.7.egg/waitress/task.py", line 380, in execute
app_iter = self.channel.server.application(env, start_response)
File "/home/maverick/.buildout/eggs/pyramid-1.4-py2.7.egg/pyramid/router.py", line 251, in __call__
response = self.invoke_subrequest(request, use_tweens=True)
File "/home/maverick/.buildout/eggs/pyramid-1.4-py2.7.egg/pyramid/router.py", line 227, in invoke_subrequest
response = handle_request(request)
File "/home/maverick/inigo/sources/devenv/lib/python2.7/site-packages/pyramid_debugtoolbar-1.0.4-py2.7.egg/pyramid_debugtoolbar/toolbar.py", line 133, in toolbar_tween
body = tb.render_full(request).encode('utf-8', 'replace')
File "/home/maverick/inigo/sources/devenv/lib/python2.7/site-packages/pyramid_debugtoolbar-1.0.4-py2.7.egg/pyramid_debugtoolbar/tbtools.py", line 240, in render_full
summary = self.render_summary(include_title=False, request=request)
File "/home/maverick/inigo/sources/devenv/lib/python2.7/site-packages/pyramid_debugtoolbar-1.0.4-py2.7.egg/pyramid_debugtoolbar/tbtools.py", line 229, in render_summary
'description': description_wrapper % escape(self.exception),
UnicodeDecodeError: 'ascii' codec can't decode byte 0xef in position 114: ordinal not in range(128)

那么,我应该怎样做呢?我几乎陷入了困境。

最佳答案

您看到的错误与 ZODB 中的图像存储无关。

要存储较大的数据 block ,您确实需要使用 ZODB Blob 而不是将图像数据直接放入属性中。 Blob 单独存储在磁盘上,不会刷新 ZODB 缓存,并且可以在再次访问时流回客户端。

要创建并存储Blob,请使用:

from ZODB.blob import Blob

uid = Blob(imagebinary.read())

一旦这样创建,您以后就可以将uid用作文件;您需要先以读或写模式打开它。例如,要从 View 返回 blob 的内容,请使用:

from pyramid.response import Response

def serveimage(request):
# retrieve uid from somewhere
resp = Response(content_type='image/jpeg')
resp.app_iter = uid.open('r') # open for reading
return resp

Blob 与事务绑定(bind),如果事务回滚,对其所做的更改将自动丢弃。

关于python - Pyramid + ZODB 图像存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14613206/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com