2016-12-27 53 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