使用节点中aws s3 bucket的范围读取部分视频文件

时间:2015-09-15 07:54:35

标签: 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);

但我不知道使用aws-sdk从s3读取部分文件很热。

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

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

如何更改上面的代码,以便我可以使用范围

从s3获取文件

2 个答案:

答案 0 :(得分: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
});

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

答案 1 :(得分:1)

这是一个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期待"范围" param为每w3c规范的格式=> "字节= N-M"在哪里" n"是起始字节," m"是结束字节。在Express应用程序中,范围将填充在客户端请求的头对象中。