允许用户播放 mp3 文件,但不要将其直接公开在网络上
我想将一些 mp3 存储在一个非公开的文件夹中,无法通过网络直接访问,并且仅允许用户在登录时使用浏览器收听/下载歌曲。
我该怎么做?
我使用 Django 进行 Web 开发,但如果我知道它是如何工作的就足够了。
I want to store some mp3s in a folder which is not public, can't be directly accessed through the web and allow users to hear/download the songs with a browser only if they are logged in.
How can I do that?
I do my web development with Django, but if I know how it works is enough.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您首先需要设置身份验证。 django 教程对此进行了深入探讨。
您不直接链接 mp3,而是链接到检查身份验证的 django 脚本,然后读取 mp3 并将其提供给具有 mp3 内容类型标头的客户端。
http://yourserver.com/listen?file=Fat+Boys+Greatest+Hits
You first need to setup authentication. The django tutorials thoroughly explore this.
You don't' link the mp3's directly, You link to a django script that checks the auth, then reads the mp3 and serves it to the client with a mp3 content type header.
http://yourserver.com/listen?file=Fat+Boys+Greatest+Hits
我假设你使用 django。然后你可以尝试这样的事情:
请注意,你的速度会急剧下降。使用生成器分块读取文件可能有助于节省内存。
Python 中读取大文件的惰性方法?
I assume you use django. Then you can try something like this:
Note that you will get dramatic speed decrease. Using generator to read file in blocks may help to save memory.
Lazy Method for Reading Big File in Python?
MEDIA_URL 文件夹)
每个用户的唯一链接
伪代码:
应该足以完成这项工作,但请记住 - 一旦访问了什么,您就无法控制它从现在开始的去向。并且每个文件下载都需要通过应用程序读取文件(不是静态给出的),这会影响应用程序的性能。
MEDIA_URL folders)
unique links for every user
Pseudocode:
Should do the job enough, but remember - what is once accessed, you have no control where it goes from now on. And that every file download needs reading the file through the app (it's not given statically), which will affect the performance of your app.