使用 Django 将文件存储在多个硬盘驱动器上
Store file on several hard drives with Django
我想在 django 中同时使用多个硬盘来存储我的文件。我的目标是将大文件存储在服务器上并在本地网络上共享。
我想知道如何将我的文件存储在多个磁盘中,以及当第一个磁盘已满时 django 将如何反应。这个想法是填充第一个然后切换到第二个。显然,如果我们删除第一个文件中的文件,我们会重新使用它。
我不认为 django 可以满足这样的需求 "out-of-the-box" 但你可以通过自己制作来实现这样的功能 "UploadHandler"
您可以从 FileUploadHandler
继承 class
from django.core.files.uploadhandler import FileUploadHandler
class MyUploadHandler(FileUploadHandler):
def receive_data_chunk(self, raw_data, start):
""" doc says:
Receives a “chunk” of data from the file upload.
raw_data is a byte string containing the uploaded data.
start is the position in the file where this raw_data chunk begins.
"""
# Here get a temporary copy of file content
def file_complete(self, file_size):
"""Called when a file has finished uploading."""
# Here manage the copy on one of your hard disks
在settings.py中定义:
FILE_UPLOAD_HANDLERS = [
'path.to.MyUploadHandler
]
见https://docs.djangoproject.com/en/2.0/ref/files/uploads/#writing-custom-upload-handlers
我想在 django 中同时使用多个硬盘来存储我的文件。我的目标是将大文件存储在服务器上并在本地网络上共享。
我想知道如何将我的文件存储在多个磁盘中,以及当第一个磁盘已满时 django 将如何反应。这个想法是填充第一个然后切换到第二个。显然,如果我们删除第一个文件中的文件,我们会重新使用它。
我不认为 django 可以满足这样的需求 "out-of-the-box" 但你可以通过自己制作来实现这样的功能 "UploadHandler"
您可以从 FileUploadHandler
from django.core.files.uploadhandler import FileUploadHandler
class MyUploadHandler(FileUploadHandler):
def receive_data_chunk(self, raw_data, start):
""" doc says:
Receives a “chunk” of data from the file upload.
raw_data is a byte string containing the uploaded data.
start is the position in the file where this raw_data chunk begins.
"""
# Here get a temporary copy of file content
def file_complete(self, file_size):
"""Called when a file has finished uploading."""
# Here manage the copy on one of your hard disks
在settings.py中定义:
FILE_UPLOAD_HANDLERS = [
'path.to.MyUploadHandler
]
见https://docs.djangoproject.com/en/2.0/ref/files/uploads/#writing-custom-upload-handlers