pythonflaskflask-uploads

What does UPLOADS_DEFAULT_URL configuration value in flask uploads mean?


From this page:

UPLOADS_DEFAULT_URL

If you have a server set up to serve from UPLOADS_DEFAULT_DEST, then set the server’s base URL here. Continuing the example above, if /var/uploads is accessible from http://localhost:5001, then you would set this to http://localhost:5001/ and URLs for the photos set would start with http://localhost:5001/photos. Include the trailing slash.

However, you don’t have to set any of the _URL settings - if you don’t, then they will be served internally by Flask. They are just there so if you have heavy upload traffic, you can have a faster production server like Nginx or Lighttpd serve the uploads.

I do not understand how Flask uses UPLOADS_DEFAULT_URL. The text says that if you don't specify it the uploads will be served internally by flask. Questions:

  1. On what url are they going to be served by flask if I don't specify the url?
  2. If I do specify URL what flask is going to do with it? How is it going to use it?

So it's easier to answer my question: I don't know how exactly python interacts with a web server such as apache or nginx. I do understand that in principle you want these web servers to front/proxy you python app for scalability/load but I don't know exact details on how this is done. May be if I knew that, the information above would be more obvious to me.

From practical perspective: I have someone else's python/flask app, and not a lot of experience with python. The parameter above needs to be specified in the config files. I got the app up and running, I did not specify this particular parameter, the uploads are working fine. I'm wondering what else could I have possibly broken by not specifying the URL.


Solution

  • On what url are they going to be served by flask if I don't specify the url?

    From the doc i understand that if you set like this

    UPLOADS_DEFAULT_DEST = '/var/uploads/'
    UPLOADS_DEFAULT_URL = 'http://localhost:5000/'
    

    Then when you upload a set named photos will store its uploads in /var/uploads/photos.Lets assume it as /var/uploads/photos/test.jpg.Then flask will serve the image as

    http://localhost:5000/photos/test.jpg.
    

    If I do specify URL what flask is going to do with it? How is it going to use it?

    Let if

    UPLOADED_PHOTOS_DEST = '/var/mypics/'
    UPLOADED_PHOTOS_URL =  'http://localhost:5000/'
    

    Then when you upload a set named photos will store its uploads in /var/mypics/.Lets assume it as /var/mypics/test.jpg.Then flask will serve the image as

    http://localhost:5000/test.jpg.
    

    But we do not use this in production.In production images, statics should be served by nginx or apache