问题
I have a server that communicates with S3 and I want to serve mp3 files to client's browser without downloading them to the server. The files required an authenticated user to access them, so they are all private. I'm also not sure if this sort of functionality specifically sits on the server code or can be done through some JS. My question is what is the right way to do this and how?
My flask code works like this essentially:
@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)
And my 'audio.html' file looks like this:
<!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>
回答1:
You should return a client app a direct link to the S3 file using S3 file sharing mechanisms. S3 file proxying through the Flask is very slow and overall bad approach.
来源:https://stackoverflow.com/questions/52322925/stream-files-stored-on-s3-to-client-without-downloading-it-to-flask-server