Uploading Files in Django - How to avoid the increase of files in the upload directory

泪湿孤枕 提交于 2020-06-28 06:34:48

问题


I've been working on uploading a file on Django. I have the following code to run things.

def handle_uploaded_file(file, filename):
    if not os.path.exists('upload/'):
        os.mkdir('upload/')

    with open('upload/' + filename, 'wb+') as destination:
        for chunk in file.chunks():
            destination.write(chunk)

def upload_csv(request):
    # Get the context from the request.
    context = RequestContext(request)

    if request.is_ajax():
        if "POST" == request.method:
            csv_file = request.FILES['file']
            my_df = pd.read_csv(csv_file, header=0)
            handle_uploaded_file(csv_file, str(csv_file))
            .............................
            .............................              

As you can see above, I have been uploading files to the upload directory. However, my concern is that this type of method might not be that much efficient because each file is kept and stored in that folder. What if hundreds or thousands of files are uploaded each week? See this:

What would be the other way to efficiently and effectively upload files?


回答1:


It is considered best practice to store your uploaded files on a disk. There is no way around. You must provide enough disk space on your server. In addition, you could automatically block uploads if you are running out of disk space.

Storing files in a database should never happen. If it’s being considered as a solution for a problem, find a certified database expert and ask for a second opinion.

Source: Two Scopes of Django.


If you concerned about the file limit within a folder you could introduce subfolders to your upload directory. For example upload/category/year/filename. You can take advantage of the FileField.

def document_directory_path(instance, filename):
    path = 'upload/{}/%Y/{}'.format(instance.category.slug, filename)
    return datetime.datetime.now().strftime(path)


class Document(models.Model):
    category = models.ForeignKey(Category, on_delete=models.CASCADE)
    file = models.FileField(upload_to=document_directory_path)

    def filename(self):
        return os.path.basename(self.file.name)

Another similar example that groups your uploads by the user (uploader) id can be found here.


Last but not least, you have the option to move to a cloud provider. Which can be pricey if you expect thousands of files and a lot of traffic.



来源:https://stackoverflow.com/questions/48958888/uploading-files-in-django-how-to-avoid-the-increase-of-files-in-the-upload-dir

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