金字塔+ ZODB图像存储 [英] Pyramid + ZODB Image storing

查看:123
本文介绍了金字塔+ ZODB图像存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个上载表单,该表单接受一个zip文件,并具有一种将其解压缩并从中获取每个文件的方法.根据其md5哈希值创建唯一的ID,并将其存储在字典中;

I have an upload form that accepts a zip file and has a method that unzips it and get each file from it. Make a unique id from the md5 hash of it and stores them in a dictionary;

dict[uid] = imagebinary

并返回它,以便表单可以将它们存储到ZODB中.我无法像这样存储图像,因为此错误会逐渐消失;

and returns it so that the form can store them into ZODB. I can't store the image just like that, as this error spits out;

    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)

那么,我应该怎么做呢?我对此非常困惑.

So, how should I go about doing that? I'm pretty much stuck on this.

推荐答案

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

The error you see is unrelated to image storing in the ZODB.

要存储更大的数据,您确实要使用ZODB Blob,而不是将图像数据直接放在属性中. Blob分别存储在磁盘上,并且不会刷新ZODB缓存,并且可以再次流化回客户端.

To store larger pieces of data, you really want to use a ZODB Blob instead of putting the image data directly in an attribute. Blobs are stored separately on disk and do not flush the ZODB cache, and can be streamed back to the client on access again.

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

To create and store a Blob, use:

from ZODB.blob import Blob

uid = Blob(imagebinary.read())

这样创建后,您以后就可以将uid用作文件了;您需要先以读取或写入模式打开它.例如,要从视图返回Blob的内容,请使用:

Once created like that, you can later use uid as a file; you need to open it in read or write mode first. To return the contents of the blob from a view, for example, use:

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绑定到事务,如果回滚该事务,对它们的更改将被自动丢弃.

Blobs are bound to transactions and changes to them are automatically discarded if the transaction is rolled back.

这篇关于金字塔+ ZODB图像存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆