使用节点中的aws s3存储区的范围读取部分video文件

下面的代码工作正常,按范围读取文件

var path = 'assets/video/'+req.body.key; var stat = fs.statSync(path); var total = stat.size; var range = req.headers.range; var parts = range.replace(/bytes=/, "").split("-"); var partialstart = parts[0]; var partialend = parts[1]; var start = parseInt(partialstart, 10); var end = partialend ? parseInt(partialend, 10) : total-1; var chunksize = (end-start)+1; console.log('RANGE: ' + start + ' - ' + end + ' = ' + chunksize); var file = fs.createReadStream(path, {start: start, end: end}); res.writeHead(206, { 'Content-Range': 'bytes ' + start + '-' + end + '/' + total, 'Accept-Ranges': 'bytes', 'Content-Length': chunksize, 'Content-Type': 'video/mp4' }); file.pipe(res); 

但是我不知道用sws读取s3的部分文件

下面的代码正在从s3读取文件。

  var imgStream = s3.getObject(params).createReadStream(); imgStream.pipe(res); 

如何改变上面的代码,所以我可以从S3使用范围获取文件

看看在params选项中的范围:

 var params = { Bucket: 'STRING_VALUE', /* required */ Key: 'STRING_VALUE', /* required */ IfMatch: 'STRING_VALUE', IfModifiedSince: new Date || 'Wed Dec 31 1969 16:00:00 GMT-0800 (PST)' || 123456789, IfNoneMatch: 'STRING_VALUE', IfUnmodifiedSince: new Date || 'Wed Dec 31 1969 16:00:00 GMT-0800 (PST)' || 123456789, Range: 'STRING_VALUE', RequestPayer: 'requester', ResponseCacheControl: 'STRING_VALUE', ResponseContentDisposition: 'STRING_VALUE', ResponseContentEncoding: 'STRING_VALUE', ResponseContentLanguage: 'STRING_VALUE', ResponseContentType: 'STRING_VALUE', ResponseExpires: new Date || 'Wed Dec 31 1969 16:00:00 GMT-0800 (PST)' || 123456789, SSECustomerAlgorithm: 'STRING_VALUE', SSECustomerKey: new Buffer('...') || 'STRING_VALUE', SSECustomerKeyMD5: 'STRING_VALUE', VersionId: 'STRING_VALUE' }; s3.getObject(params, function(err, data) { if (err) console.log(err, err.stack); // an error occurred else console.log(data); // successful response }); 

http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#getObject-property

下面是一个演示AWS S3字节范围请求的node.js Express代码片段:

 s3.listObjectsV2({MaxKeys: 1, Prefix: file}, function(err, data) { if (err) { return res.sendStatus(404); } if (req != null && req.headers.range != null) { var range = req.headers.range; var bytes = range.replace(/bytes=/, '').split('-'); var start = parseInt(bytes[0], 10); var total = data.Contents[0].Size; var end = bytes[1] ? parseInt(bytes[1], 10) : total - 1; var chunksize = (end - start) + 1; res.writeHead(206, { 'Content-Range' : 'bytes ' + start + '-' + end + '/' + total, 'Accept-Ranges' : 'bytes', 'Content-Length' : chunksize, 'Last-Modified' : data.Contents[0].LastModified, 'Content-Type' : mimetype }); s3.getObject({Key: file, Range: range}).createReadStream().pipe(res); } else { res.writeHead(200, { 'Cache-Control' : 'max-age=' + cache + ', private', 'Content-Length': data.Contents[0].Size, 'Last-Modified' : data.Contents[0].LastModified, 'Content-Type' : mimetype }); s3.getObject({Key: file}).createReadStream().pipe(res); } }); 

S3预计“范围”参数的格式是w3c规格=>“bytes = nm”,其中“n”是起始字节,“m”是结束字节。 在Express应用程序中,范围从客户端请求的头部对象中填充。