Search code examples
pythondjangoweb-deploymentproduction-environmentfile-system-storage

Django - Alternative for FileSystemStorage in Production Environment


I am fairly new to django and I want to deploy my app. 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. 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?


Solution

  • 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