流和S3上传问题

Issues with streams and S3 upload

本文关键字:问题 S3 流和      更新时间:2023-09-26

我的零字节流有问题。我正在调整图像的大小,并将其作为流上传到S3。如果我通过管道输出以响应,它将正确显示。

// Fetch remote file
var request = http.get('mybigfile.jpg', function(response) {
  // Setup IM convert
  var convert = spawn('convert', ['-', '-strip', '-thumbnail', 600, '-']);
  // Pipe file stream to it
  response.pipe(convert.stdin);
  // Pipe result to browser - works fine
  //convert.stdout.pipe(res);

  // S3 requires headers
  var headers = {
    'content-type': response.headers['content-type'],
    'x-amz-acl': 'public-read'
   };
  // Upload to S3
  var aws = aws2js.load('s3', aws.key, aws.secret);
  aws.setBucket(aws.bucket);
  aws.putStream('thumb.jpg', convert.stdout, false, headers, function(err) {
    if (err) {
      return console.error('Error storing:', err.toString());
    } else {
      // No errors - this shows - but file is 0kb
      console.log(path + ' uploaded to S3');
    }
  }

我看到一些关于流由于内容长度而无法使用S3的说明。我正在尝试缓冲,但到目前为止没有成功。

好吧,没有继续流-我想我可以使用暂停流或多部分来从技术上实现这一点,但除此之外,我认为这是不可能的。我最后用了一个缓冲区。

...
// Pipe file stream to it
response.pipe(convert.stdin);
// Save to buffer
var bufs = [] ;
convert.stdout.on('data', function(chunk) {
  bufs.push(chunk);
});
convert.stdout.on('end', function() {
  var buffer = Buffer.concat(bufs);
// S3 requires headers
...
aws.putBuffer(path, buffer, false, headers, function(err) {
...