使用来自节点中 aws s3 存储桶的范围读取部分视频文件

use*_*877 3 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 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);
Run Code Online (Sandbox Code Playgroud)

但我不知道如何使用 aws-sdk 从 s3 读取部分文件

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

  var imgStream = s3.getObject(params).createReadStream();                  
  imgStream.pipe(res);
Run Code Online (Sandbox Code Playgroud)

如何更改上面的代码,以便我可以使用范围从 s3 获取文件

小智 5

这是一个 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);
    }
});
Run Code Online (Sandbox Code Playgroud)

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


小智 2

看一下 params 选项中的 Range:

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
});
Run Code Online (Sandbox Code Playgroud)

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