What is a Django / Python solution for providing a one-time url for people to upload files?

I’m looking for a way to sell someone a card at an event that will have a unique code that they can use later to download a file (mp3, pdf, etc.) only once and mask the true location of the file, so an experienced user downloading file, will not be able to download the file more than once. It would be nice to host the file on Amazon S3 to save on bandwidth where our server is located.

My idea of ​​codes was to pre-generate unique codes that will be printed on cards and store them in a database, which can also have a field in which the number of downloaded files is stored. Thus, we could establish how many attempts we allow the user to download the file.

The part I need direction to is how to hide / hide the original location of the file so that people cannot steal this URL and then download the file as many times as you like. I did a Google search, and I'm either not looking for the right keywords, or there are not many libraries or fragments that already exist for this type of thing.

I assume that I can build something with the help django.views.static.servethat acts as a kind of proxy between the actual file and the user uploading the file. The only drawback of this method would be that I would need to use the actual web server and not be able to store the file on Amazon S3.

Any suggestions or thoughts are welcome.

+5
source share
2 answers

A sophisticated idea. However, I would caution against the one-time download method, because there is no guarantee that their first attempt to download will be successful. Perhaps the expiration method is used instead?

But this is possible with Django. Here is a outline of the main approach:

  • URL- django .
  • GET, , , .
  • , FileField . .
  • , :

(path - )

with open(path, 'rb') as f:
    response = HttpResponse(f.read())
response['Content-Type'] = 'application/octet-stream';
response['Content-Disposition'] = 'attachment; filename="%s"' % 'insert_filename_here'
return response

Django , .

+3

- , mod_xsendfile. -, lighttpd nginx.

: (, PHP script) , - .

, S3, , , AWS, . S3 /? , URL- .

+2

All Articles