在Django中上传文件-如何避免增加上传目录中的文件 [英] Uploading Files in Django - How to avoid the increase of files in the upload directory

查看:153
本文介绍了在Django中上传文件-如何避免增加上传目录中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力在Django上上传文件.我有以下代码来运行程序.

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?

推荐答案

将上传的文件存储在磁盘上是最佳实践. 没有办法.您必须在服务器上提供足够的磁盘空间.此外,如果磁盘空间不足,您可以自动阻止上传.

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.

来源:两个Django范围.

如果您担心文件限制文件夹,您可以在upload目录中引入子文件夹.例如upload/category/year/filename.您可以利用 FileField .

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)

可以找到另一个类似的示例,该示例按用户(上载者)ID对您的上载进行分组

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.

这篇关于在Django中上传文件-如何避免增加上传目录中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆