我想通过向我的Node/Express服务器发出请求,将音频文件从S3传输到我的React客户端。我设法实现了一些可行的方法,但我不确定我是否真的在这里流式传输这个文件,或者直接下载它。我怀疑我可能会下载文件,因为我对服务器的请求需要很长的时间回来:通过Express服务器从S3流式传输音频需要太长时间
Established database connection.
Server listening on port 9000!
::1 - - [18/Apr/2017:21:13:43 +0000] "GET/HTTP/1.1" 200 424 6.933 ms
::1 - - [18/Apr/2017:21:13:43 +0000] "GET /static/js/main.700ba5c4.js HTTP/1.1" 200 217574 1.730 ms
::1 - - [18/Apr/2017:21:13:43 +0000] "GET /index.css HTTP/1.1" 200 - 8.722 ms
Server received a request: GET /tracks
::1 - - [18/Apr/2017:21:13:43 +0000] "GET /tracks HTTP/1.1" 304 - 41.468 ms
Server received a request: GET /tracks/1/stream
::1 - - [18/Apr/2017:21:14:13 +0000] "GET /tracks/1/stream HTTP/1.1" 200 - 636.249 ms
Server received a request: GET /tracks/2/stream
Database query threw an error: ETIMEDOUT
注意636.249毫秒!
你们能否告诉我在这里做错了什么?我粘贴下面我目前的方法的代码;它具有以下功能:
- 客户端发出一个fetch调用到
/tracks/id/stream
- 服务器查询数据库来获取轨道
- Server使用
downloadStream
(从s3 package)来获取文件 - 服务器管道数据到客户端
- 客户端接收数据作为ArrayBuffer
- 客户端解码缓冲区并将其传递到AudioBufferSourceNode
- AudioBufferSourceNode播放音频
服务器端:
app.get('/tracks/:id/stream', (req, res) => {
const id = req.params.id
// Query the database for all tracks
database.query(`SELECT * FROM Tracks WHERE id = ${id}`, (error, results, fields) => {
// Upon failure...
if (error) {
res.sendStatus(500)
}
// Upon success...
const params = {
Bucket: bucketName, // use bucketName defined elsewhere
Key: results[0].key // use the key from the track object
};
// Download stream and pipe to client
const stream = client.downloadStream(params)
stream.pipe(res)
})
});
客户端获取拨打:
const URL = `/tracks/${id}/stream`
const options = { method: 'GET' }
fetch(URL, options)
.then(response => response.arrayBuffer())
.then(data => {
// do some stuff
AudioPlayer.play(data)
})
客户端AudioPlayer,负责处理实际AudioBufferSourceNode :
const AudioPlayer = {
play: function(data) {
// Decode the audio data
context.decodeAudioData(data, buffer => {
// Create a new buffer source
source = context.createBufferSource()
source.buffer = buffer
source.connect(context.destination)
// Start playing immediately
source.start(context.currentTime)
})
},
...
什么是'client'和'downloadStream()' – peteb
我提到'downloadStream'来自[S3包(https://www.npmjs.com/package/s3);客户也来自那里。 – Gundam194