Node.js 使用节点中aws s3存储桶的范围读取部分视频文件
下面的代码工作正常,可以按范围读取文件Node.js 使用节点中aws s3存储桶的范围读取部分视频文件,node.js,aws-sdk,Node.js,Aws Sdk,下面的代码工作正常,可以按范围读取文件 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 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);
但我不知道如何使用aws sdk从s3读取部分文件
下面的代码正在从s3读取文件
var imgStream = s3.getObject(params).createReadStream();
imgStream.pipe(res);
如何更改上述代码,以便使用range从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
});
下面是一个node.js Express代码片段,演示了AWS S3字节范围请求:
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希望“Range”参数的格式符合w3c规范=>“bytes=n-m”,其中“n”是起始字节,“m”是结束字节。在Express应用程序中,范围从客户端请求填充到header对象中。使用
s3。headObject
而不是s3。listObjectsV2
提供mimetype,因此不必在本地缓存或查找它。