2016-12-27 62 views
0

我一直在使用http.get和s3.putObject。基本上,只需要从http位置获取文件并将其保存到S3中的存储桶中即可。看起来很简单。原始文件大小是47kb。S3 PutObject在AWS Lambda(通过节点)保存到存储桶时将文件大小加倍

问题是,检索到的文件(47kb)被保存到S3存储桶(使用s3.putObject),大小为92.4kb。某处文件的大小增加了一倍,使其不可用。

如何防止文件在保存到S3存储桶时的大小加倍?

这里的使用的整个代码:

exports.handler = function(event, context) { 
    var imgSourceURL = "http://www.asite.com/an-image.jpg"; 
    var body; 
    var stagingparams; 
    http.get(imgSourceURL, function(res) { 
     res.on('data', function(chunk) { body += chunk; }); 
     res.on('end', function() { 
      var tmp_contentType = res.headers['content-type']; // Reported as image/jpeg 
      var tmp_contentLength = res.headers['content-length']; // The reported filesize is 50kb (the actual filesize on disk is 47kb) 
      stagingparams = { 
       Bucket: "myspecialbucket", 
       Key: "mytestimage.jpg", 
       Body: body 
      }; 
      // When putObject saves the file to S3, it doubles the size of the file to 92.4kb, thus making file non-readable. 
      s3.putObject(stagingparams, function(err, data) { 
       if (err) { 
        console.error(err, err.stack); 
       } 
       else { 
        console.log(data); 
       } 
      }); 
     }); 
    }); 
}; 

回答

1

使用一个阵列存储可读流的字节,然后串联阵列中的所有缓冲器的实例调用s3.putObject之前一起:

exports.handler = function(event, context) { 
    var imgSourceURL = "http://www.asite.com/an-image.jpg"; 
    var body = []; 
    var stagingparams; 
    http.get(imgSourceURL, function(res) { 
     res.on('data', function(chunk) { body.push(chunk); }); 
     res.on('end', function() { 
      var tmp_contentType = res.headers['content-type']; // Reported as image/jpeg 
      var tmp_contentLength = res.headers['content-length']; // The reported filesize is 50kb (the actual filesize on disk is 47kb) 
      stagingparams = { 
       Bucket: "myspecialbucket", 
       Key: "mytestimage.jpg", 
       Body: Buffer.concat(body) 
      }; 
      // When putObject saves the file to S3, it doubles the size of the file to 92.4kb, thus making file non-readable. 
      s3.putObject(stagingparams, function(err, data) { 
       if (err) { 
        console.error(err, err.stack); 
       } 
       else { 
        console.log(data); 
       } 
      }); 
     }); 
    }); 
}; 
+0

你,先生,真棒!数组和连接是修复。 – bepuzzled