简体   繁体   English

Django - 生产环境中 FileSystemStorage 的替代方案

[英]Django - Alternative for FileSystemStorage in Production Environment

I am fairly new to django and I want to deploy my app.我对 django 相当陌生,我想部署我的应用程序。 I have used FileSystemStorage to store my files temporarily in a Media folder and deleting them afterwards, in my django app however I've also read that it should not be used in a production environment.我已经使用 FileSystemStorage 将我的文件临时存储在 Media 文件夹中,然后在我的 django 应用程序中删除它们,但是我还读到它不应该在生产环境中使用。 My code is essentially the same as the one shown in this article .我的代码与本文中显示的代码基本相同。

Why can't I use FileSystemStorage in a production environment and what can I use instead in order to serve my purpose?为什么我不能在生产环境中使用 FileSystemStorage,我可以使用什么来代替我的目的?

If you want to handle user uploaded file then delete it afterward here is how i implemented it in a simple view:如果你想处理用户上传的文件,然后删除它,这是我在一个简单的视图中实现它的方式:

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")

This view will return the image 1 time此视图将返回图像 1 次

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM