将存储在S3上的文件流传输到客户端,而无需将其下载到flask服务器 [英] Stream files stored on S3 to client without downloading it to flask server

查看:77
本文介绍了将存储在S3上的文件流传输到客户端,而无需将其下载到flask服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个与S3通信的服务器,我想将mp3文件提供给客户端的浏览器,而无需将其下载到服务器.这些文件需要经过身份验证的用户才能访问,因此它们都是私有的. 我也不确定这种功能是专门位于服务器代码上还是可以通过某些JS完成. 我的问题是什么是正确的方法以及如何做?

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) 

我的"audio.html"文件如下所示:

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>

推荐答案

您应该使用

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.

这篇关于将存储在S3上的文件流传输到客户端,而无需将其下载到flask服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆