views:

52

answers:

5

I am prototyping a Django web-app that will provide a static media files (PDFs, movies and so on) to the users. One of the features I am going to implement is a dynamic URLs. The point is to generate different URLs for each access attempt. When user will request a list of files I will generate links and save them to the DB. When some of these links will be clicked I will return required content and clear links from the DB.

I suppose that serving media through the Django is far away from the best practices. Should it be avoided at all costs? Does it seems reasonable to access these files via non-Django app (Perl script or something)?

A: 

Use django for it just for some small development, forbidden ;) for production.
Even django team propose web server to serve static files.
You can embed cherrypy it would be better practice.

bua
+1  A: 

I suppose that serving media through the Django is far away from the best practices.

Correct. It is discouraged in the official documentation as well.

Should it be avoided at all costs?

ALL costs? Probably not. If the number of requests and the volume of data served is (quite) low enough then serving static files using Django will not affect the system's performance adversely.

Does it seems reasonable to access these files via non-Django app (Perl script or something)?

General answer: yes. It is difficult to give a better answer without knowing the details of what you are planning.

Manoj Govindan
A: 

Django iteself states that thisis unefficient an unsecure. As for me, django static file serve must only use in development environment. Rather smal or big, it must not used on production...

Documentatin about django static serve

FallenAngel
A: 

I think the question is wrongly stated.

Django is capable to serve static files, but by default it is disabled on production, and it is clearly stated it is just for development purposes.
So, everybody agrees that using django to serve static files is a bad idea :-)

However, as you put it, you do not want to use django for serving static files, you want to use django to implement some kind of "security" on file download.
As I understand it, the file is not static at all; on the contrary, it is definitely dynamic!

I think you have two possible options:

  • Use django to parse the URL, and then redirect to another server (e.g. nginx) for serving the file. This is for sure the fastest way, but it has the obvious disadvantage of giving away the "real" address of the file.
  • Use django to parse the URL, and then serving the PDF directly. It completely implements your original requirement (one-time URLs) but scaling up on performance could be quite difficult.

I think the second point needs some more details.
Serving a file requires some time; using django to serve it is not less efficient than any other method (most of the time is spent in waiting for the I/O), but it may consume a thread, or a process on your server.
If you plan to have many connections at the same time, this approach may have serious drawbacks; on the other hand, if you need to serve small files to a limited audience, then it may be completely OK.

Roberto Liffredo
A: 

Can't you use NGINX and it's secure link module ?
Streaming files with django ... is slow and stupid.
If you really must stream files with python over HTTP, at least use Whizzer or Twisted. It will have decent perfomance. They both can be easily hook in django`s ORM .

krikulis