繁体   English   中英

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

[英]Read partial video file using range from aws s3 bucket in node

下面的代码可以很好地按范围读取文件

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 获取文件

看一下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

这是一个演示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应用程序中,该范围填充在客户端请求的标头对象中。

     var params = {
      Bucket: "examplebucket", 
      Key: "SampleFile.txt", 
      Range: "bytes=0-9"
     };
     s3.getObject(params, function(err, data) {
       if (err) console.log(err, err.stack); // an error occurred
       else     console.log(data);           // successful response
       /*
       data = {
        AcceptRanges: "bytes", 
        ContentLength: 10, 
        ContentRange: "bytes 0-9/43", 
        ContentType: "text/plain", 
        ETag: "\"0d94420ffd0bc68cd3d152506b97a9cc\"", 
        LastModified: <Date Representation>, 
        Metadata: {
        }, 
        VersionId: "null"
       }
       */
     });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM