Pyramid + ZODB Image storing

流过昼夜 提交于 2019-12-08 13:46:29

The error you see is unrelated to image storing in the 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.

To create and store a Blob, use:

from ZODB.blob import Blob

uid = Blob(imagebinary.read())

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!