2009-09-19 88 views
4

任何人都可以指向任何提供可插入自定義存儲系統的項目/ django應用程序,這樣我就可以在Django中使用GridFS來存儲文件上傳了嗎?GridFS(MongoDB)的自定義存儲系統?

我找到了django-mongodb,但它似乎不支持GridFS,也沒有django-storages。

我打算爲正常的數據庫要求運行mysql,並只使用mongodb進行文件存儲,以便清楚我不想將mongodb用作我的主數據庫。

回答

3

我使用的是PyMongo,MongoDB Python驅動程序,並沒有聽說任何使用GridFS爲Django提供自定義存儲的項目。這看起來像在PyMongo之上寫起來不是很難:可能是將GridFS API直接翻譯成Django storage API。也許可以看看在某些時候將某些東西扔在一起,但對於任何想要參與的人來說,這將是一個很好的開源項目。

+0

剛剛加入含有GridFS的存儲後端的一個GitHub庫http://goo.gl/tCFvf – 2011-03-05 07:27:58

1

django-mongodb-engine可能值得一看,因爲它可以讓你做到這一點,而無需對現有的Django代碼進行更改。

1

我最近實施了GridFS support in Mongoengine,您可能希望結賬。這包括一個Django存儲後端,您可以直接插入到您的項目中並與ImageField一起使用等等。我在生產中使用這些技術,並且迄今爲止它表現非常出色。

1

我需要的是Mayan EDMS,可插拔存儲和數據庫分離。使用邁克爾·迪爾夫最新的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