Stream files stored on S3 to client without downloading it to flask server

非 Y 不嫁゛ 提交于 2019-12-11 04:07:44

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!