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. 
});