我有一个与S3通信的服务器,我想将mp3文件提供给客户端的浏览器而不将其下载到服务器。这些文件需要经过身份验证的用户才能访问它们,因此它们都是私有的。我也不确定这种功能是否特别适用于服务器代码,或者可以通过某些JS完成。我的问题是这样做的正确方法是什么?
我的烧瓶代码基本上是这样的:
@app.route('/audio')
def audio():
s3 = boto_session.client("s3", region_name='us-west-2')
file_name = 'some_file_on_s3'
download_path = './static/' + file_name
bucket_name = 'some_bucket'
s3.download_file(bucket_name, file_name, download_path)
return render_template('audio.html', file_source=file_name)
我的'audio.html'文件看起来像这样:
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<audio id="t-rex-roar" controls src="{{'./static/'+file_source}}">
Your browser does not support the
<code>audio</code> element.
</audio>
</body>
</html>
您应该使用S3 file sharing mechanisms将客户端应用程序返回到S3文件的直接链接。通过Flask代理的S3文件是非常缓慢和整体糟糕的方法。