2017-07-19 37 views
0

我試圖下載從S3文件(我已經成功上傳的文件)S3的NodeJS流ReadStream拋出未指定的錯誤事件

const fs = require('fs'); 
const AWS = require('aws-sdk'); 
const S3 = AWS.S3; 
const S3S = require('s3-streams'); 

const awsSettings = { 
    ... // config stuff 
} 

var s3Instance = new S3(awsSettings); 
module.exports = { 
    download: (remoteReference, bucket, saveFile) => { 
     return new Promise((resolve, reject) => { 
      let download = new S3S.ReadStream(s3Instance, { 
       Bucket: bucket, 
       Key: remoteReference 
      }); 
      download.pipe(fs.createWriteStream(saveFile)) 
      .on('finish',() => { 
       resolve(); 
      }) 
      .on('error', (err) => { 
       reject(err); 
      }); 
     }); 
    }, 
} 

然而,節點崩潰時調用下載

崩潰日誌:

server/node_modules/aws-sdk/lib/request.js:31 
      throw err; 
      ^
Error: Uncaught, unspecified "error" event. ([object Object]) 
    at S3ReadStream.emit (events.js:163:17) 
    at Request.httpHeaders (server/node_modules/s3-streams/lib/read.js:52:9) 
    at Request.callListeners (server/node_modules/aws-sdk/lib/sequential_executor.js:105:20) 
    at Request.emit (server/node_modules/aws-sdk/lib/sequential_executor.js:77:10) 
    at Request.emit (server/node_modules/aws-sdk/lib/request.js:683:14) 
    at IncomingMessage.onHeaders (server/node_modules/aws-sdk/lib/event_listeners.js:245:24) 
    at emitThree (events.js:116:13) 
    at IncomingMessage.emit (events.js:194:7) 
    at ClientRequest.<anonymous> (server/node_modules/aws-sdk/lib/http/node.js:46:16) 
    at ClientRequest.g (events.js:292:16) 
    at emitOne (events.js:96:13) 
    at ClientRequest.emit (events.js:188:7) 
    at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:473:21) 
    at HTTPParser.parserOnHeadersComplete (_http_common.js:99:23) 
    at TLSSocket.socketOnData (_http_client.js:362:20) 
    at emitOne (events.js:96:13) 

我不太確定是什麼原因導致這個問題,但深挖S3流源,它似乎是失敗的請求的HTTP頭併發出一個錯誤。

+0

嘗試註釋掉'。對(「錯誤」 ...'位 然後結束處理 還是錯誤 –

+0

嘗試跳過實例化一個'新''S3S.ReadStream' –

回答

1

怎麼是這樣的:。。?

var s3Instance = new S3(awsSettings); 
module.exports = { 
    download: (remoteReference, bucket, saveFile) => { 
    return new Promise((resolve, reject) => { 
     let download = S3S.ReadStream(s3Instance, { 
     Bucket: bucket, 
     Key: remoteReference 
     }) 
     .on('finish',() => { 
     resolve(); 
     }) 
     .on('error', (err) => { 
     reject(err); 
     }) 
     .pipe(fs.createWriteStream(saveFile)); 
    }); 
    }, 
} 
+0

它的工作!我現在可以處理一個可讀的錯誤! –

相關問題