Python GridFS(MongoDB)的自定义存储系统?

Python GridFS(MongoDB)的自定义存储系统?,python,django,mongodb,Python,Django,Mongodb,有人能告诉我提供可插拔自定义存储系统的任何项目/django应用程序,以便我可以使用GridFS和django存储上传的文件吗 我发现了django mongodb,但它似乎不支持GridFS,django存储也不支持 我计划运行mysql以满足正常的数据库需求,并且只使用mongodb进行文件存储,因此我不想使用mongodb作为我的主数据库。我使用的是PyMongo,mongodb Python驱动程序,还没有听说过任何使用GridFS为Django提供自定义存储的项目。这看起来在PyMon

有人能告诉我提供可插拔自定义存储系统的任何项目/django应用程序,以便我可以使用GridFS和django存储上传的文件吗

我发现了django mongodb,但它似乎不支持GridFS,django存储也不支持


我计划运行mysql以满足正常的数据库需求,并且只使用mongodb进行文件存储,因此我不想使用mongodb作为我的主数据库。

我使用的是PyMongo,mongodb Python驱动程序,还没有听说过任何使用GridFS为Django提供自定义存储的项目。这看起来在PyMongo上写起来并不难:可能是直接翻译的。也许你可以考虑在某个时候把一些东西放在一起,但这对于任何想参与其中的人来说都是一个很棒的开源项目。

可能值得一看,因为它使你不需要修改现有的Django代码就可以做到这一点。

我最近实现了一个你可能想签出的。这包括一个Django存储后端,您可以将其直接插入到项目中并与ImageField等一起使用。我正在生产中使用这些技术,到目前为止效果非常好。

我正需要它来实现可插入存储和数据库分离。使用MichaelDirolf最新的PyMongo库,启动一个基本类相当简单

要使用它:

from gridfsstorage import GridFSStorage
file = models.FileField(storage=GridFSStorage())
gridfsstorage.py文件:

import os

from django.core.files.storage import Storage
from django.utils.encoding import force_unicode
from django.conf import settings

from pymongo import Connection
from gridfs import GridFS

class GridFSStorage(Storage):
    def __init__(self, *args, **kwargs):
        self.db = Connection(host=settings.GRIDFS_HOST,
            port=settings.GRIDFS_PORT)[settings.DATABASE_NAME]
        self.fs = GridFS(self.db)


    def save(self, name, content):
        while True:
            try:
                # This file has a file path that we can move.
                if hasattr(content, 'temporary_file_path'):
                    self.move(content.temporary_file_path(), name)
                    content.close()
                # This is a normal uploadedfile that we can stream.
                else:
                    # This fun binary flag incantation makes os.open throw an
                    # OSError if the file already exists before we open it.
                    newfile = self.fs.new_file(filename=name)
                    try:
                        for chunk in content.chunks():
                            newfile.write(chunk)
                    finally:
                        newfile.close()
        except Exception, e:
            raise
        else:
            # OK, the file save worked. Break out of the loop.
            break

        return name


    def open(self, name, *args, **kwars):
        return self.fs.get_last_version(name)


    def delete(self, name):
        oid = self.fs.get_last_version(name)._id
        self.fs.delete(oid)


    def exists(self, name):
        return self.fs.exists(filename=name)        


    def path(self, name):
        return force_unicode(name)


    def size(self, name):
        return self.fs.get_last_version(name).length

    def move(self, old_file_name, name, chunk_size=1024*64):
        # first open the old file, so that it won't go away
        old_file = open(old_file_name, 'rb')
        try:
            newfile = self.fs.new_file(filename=name)

            try:
                current_chunk = None
                while current_chunk != '':
                    current_chunk = old_file.read(chunk_size)
                    newfile.write(current_chunk)
            finally:
                newfile.close()
        finally:
            old_file.close()

        try:
            os.remove(old_file_name)
        except OSError, e:
            # Certain operating systems (Cygwin and Windows) 
            # fail when deleting opened files, ignore it.  (For the 
            # systems where this happens, temporary files will be auto-deleted
            # on close anyway.)
            if getattr(e, 'winerror', 0) != 32 and getattr(e, 'errno', 0) != 13:
                raise

刚刚添加了一个包含gridfs存储后端的github repo