0

我一直在努力从这个问题的答案位于这里How to make a socket a stream? To connect https response to S3 after imagemagick。根据loganfsmyth的建议,我评论了req.end(image)行,但是当我尝试上传文件时,服务器只是超时。当我取消注释req.end(image)行时,我经历了类似的行为,但映像成功地上传了S3。有人能为我澄清哪种方式是正确的,如果是正确的取消注释req.end(图像)行什么是最好的方式发送一个响应浏览器,以防止它超时?奇怪的流行为node.js/knox

https.get(JSON.parse(queryResponse).data.url,function(res){ 

    graphicsmagick(res) 
    .resize('50','50') 
    .stream(function (err, stdout, stderr) { 

     ws. = fs.createWriteStream(output) 

     i = [] 

     stdout.on('data',function(data){ 
     i.push(data) 
     }) 

     stdout.on('close',function(){ 
     var image = Buffer.concat(i) 

     var req = S3Client.put("new-file-name",{ 
      'Content-Length' : image.length 
      ,'Content-Type' : res.headers['content-type'] 
     }) 

     req.on('response',function(res){ //prepare 'response' callback from S3 
      if (200 == res.statusCode) 
      console.log('it worked') 
     }) 
     //req.end(image) //send the content of the file and an end 
     }) 
    }) 
}) 

回答

0

在您链接到,用户使用putStream,因此调用req.end()的问题是不正确,但在你的情况,你正在使用put直接,所以你需要调用req.end()。否则,将它注释掉后,除了长度外,您绝对不会使用image值,因此您永远不会发送图像数据。

很难说没有看到实际运行此代码的服务器处理程序,但需要(可选)返回一些响应,然后再与浏览器进行实际连接,否则它将在那里等待。

所以,如果你有这样的事情

http.createServer(function(req, browserResponse){ 

    // Other code. 

    req.on('response',function(s3res){ //prepare 'response' callback from S3 
     if (200 == s3res.statusCode) console.log('it worked') 


     // Close the response. You also pass it data to send to the browser. 
     browserResponse.end(); 
    }) 

    // Other code. 
});