【问题标题】:Django - Alternative for FileSystemStorage in Production EnvironmentDjango - 生产环境中 FileSystemStorage 的替代品
【发布时间】:2021-04-10 23:46:52
【问题描述】:

我是 django 的新手,我想部署我的应用程序。我已经使用 FileSystemStorage 将我的文件临时存储在 Media 文件夹中,然后在我的 django 应用程序中删除它们,但是我也读过它不应该在生产环境中使用。我的代码与这里显示的代码基本相同 article.

为什么我不能在生产环境中使用 FileSystemStorage,我可以使用什么来代替我的目的?

【问题讨论】:

  • 你在哪里读到的?
  • 不建议使用静态设置来提供文件,因为如果它在生产中,他们可以从 url 路径中看到您的文件代码。要像文章所说的那样向用户提供文件,请使用 nginx 而不是提供文件As mentioned earlier, this method should only be used to serve media in development and not in production. You might want to use something like nginx in production.
  • @Kroustou 在我提到的网站上
  • @LinhNguyen 实际上,我不会长时间存储文件。我只是暂时保存它们;处理完文件后,它们会被删除。
  • 哦,那么 FileSystemStorage 对于这种事情是完全正常的,您只需将变量设置为文件路径并随后将其删除

标签: python django web-deployment production-environment file-system-storage


【解决方案1】:

如果你想处理用户上传的文件,然后删除它,这是我在一个简单的视图中实现它的方式:

def my_image(request, image_name):
    # user upload file
    folder_path = 'path_to_your_file_folder/'
    fs = FileSystemStorage(location=folder_path, base_url=folder_path)    
    
    image_data = open(folder_path + image_name, "rb").read() #set file as variable
    fs.delete(folder_path + image_name)# delete the file from folder
    return HttpResponse(image_data, content_type="image")

此视图将返回图像 1 次

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-14
    • 2012-05-30
    • 2019-11-27
    • 1970-01-01
    • 2011-03-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多