获取流的内容长度 [英] get a stream's content-length

查看:701
本文介绍了获取流的内容长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的node.js应用程序,我使用的是克(一实:graphicsmagick包装)和的aws2js(亚马逊CLI包装)。问题是,亚马逊需要知道内容长度,这样我可以把S3流。

我上传图片到我的应用程序,读取该文件通过创建一个流:

  VAR FILESTREAM = fs.createReadStream(file.path)
 

我通过这个文件来克,调整其大小,然后告诉它流的文件。然后,我希望把他们流AWS:

  GM(FILESTREAM,some.png)。
    识别({bufferStream:真},功能(ERR,资讯){
        this.stream(PNG功能(ERR,标准输出,标准错误){
            如果(ERR){执行console.log(ERR); CB(ERR);回报;}

            aws.S3.putStream(路径,标准输出,大众阅读,{内容长度:?????,内涵式:图像/ PNG'},功能(错了,结果){
            .....
            }
        });
    });
});
 

的问题是,亚马逊需要知道该内容 - 长度(其不是库)放流。 AWS不支持分块的数据流。

有谁知道我怎么能确定一个流的内容长度?或者将唯一的解决办法是tmp下写入到磁盘上,读取该文件作为流,然后把它放到亚马逊的临时文件的内容长度?

解决方案

 克(FILESTREAM).filesize({bufferStream:真},功能(错误,文件大小){
  this.stream(功能(错误,标准输出,标准错误){
    aws.S3.putStream(路径,标准输出,大众阅读,{内容长度:文件大小,内涵式:图像/ PNG'},功能(错误结果){
      // .....
    });
  });
});
 

In my node.js app, I'm using gm (a graphicsmagick wrapper) and aws2js (amazon cli wrapper). The problem is that Amazon needs to know the content-length so that I can put the stream on S3.

I'm uploading an image to my app, read that file by creating a stream:

var fileStream=fs.createReadStream(file.path)

I pass that file to gm, resize it and then tell it to stream that file. I then want to put that stream to aws:

gm( fileStream, "some.png" ).                               
    identify({bufferStream: true}, function(err, info) {
        this.stream("png", function (err, stdout, stderr) {
            if (err){console.log(err);cb(err);return;}

            aws.S3.putStream(path, stdout, 'public-read', {'content-length': ?????, 'content-type': 'image/png'}, function (err, result) {
            .....       
            }
        });
    });
});

The problem is that Amazon needs to know the content-length (its not the library) to put the stream. AWS doesn't support chunked streams.

Does anyone know how I could determine the content-length of a stream? Or would the only solution be to tmp write it to the disk, read the file as a stream and then put it to amazon with the content-length of the temp file?

解决方案

gm( fileStream ).filesize({bufferStream: true}, function (error, filesize) {
  this.stream(function (error, stdout, stderr) {
    aws.S3.putStream(path, stdout, 'public-read', {'content-length': filesize, 'content-type': 'image/png'}, function (error, result) {
      // .....       
    });
  });
});

这篇关于获取流的内容长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆