node.js - s3 streaming with etags (node) -


we streaming content s3 in node using createreadstream() in aws-sdk. add etag support. if add 'if-none-match' header client, s3 throws notmodified error can't seem handle.

retrievefile = function(req, res) {     var s3 = new aws.s3();      var params = {         bucket: bucket,         key: key     };     if (req.get('if-none-match')) {         params.ifnonematch = req.get('if-none-match');     }     return s3.getobject(params).on('httpheaders', function(statuscode, headers) {         if (headers.etag) {             res.set('etag', headers.etag);         }         if (headers['content-length']) {             return res.set('content-length', headers['content-length']);         }     }).createreadstream().pipe(res); }; 

i've tried listening events on stream , using callbacks on request returned getobject. can error's message way, else in aws-sdk appears killing process.

/projects/my-app/node_modules/aws-sdk/lib/request.js:31             throw err;             ^  notmodified: null   @ request.extracterror (/projects/my-app/node_modules/aws-sdk/lib/services/s3.js:519:35)   @ request.calllisteners (/projects/my-app/node_modules/aws-sdk/lib/sequential_executor.js:105:20)   @ request.emit (/projects/my-app/node_modules/aws-sdk/lib/sequential_executor.js:77:10)   @ request.emit (/projects/my-app/node_modules/aws-sdk/lib/request.js:673:14)   @ request.transition (/projects/my-app/node_modules/aws-sdk/lib/request.js:22:10)   @ acceptorstatemachine.runto (/projects/my-app/node_modules/aws-sdk/lib/state_machine.js:14:12)   @ /projects/my-app/node_modules/aws-sdk/lib/state_machine.js:26:10   @ request.<anonymous> (/projects/my-app/node_modules/aws-sdk/lib/request.js:38:9)   @ request.<anonymous> (/projects/my-app/node_modules/aws-sdk/lib/request.js:675:12)   @ request.calllisteners (/projects/my-app/node_modules/aws-sdk/lib/sequential_executor.js:115:18)   @ request.emit (/projects/my-app/node_modules/aws-sdk/lib/sequential_executor.js:77:10)   @ request.emit (/projects/my-app/node_modules/aws-sdk/lib/request.js:673:14)   @ request.transition (/projects/my-app/node_modules/aws-sdk/lib/request.js:22:10)   @ acceptorstatemachine.runto (/projects/my-app/node_modules/aws-sdk/lib/state_machine.js:14:12)   @ /projects/my-app/node_modules/aws-sdk/lib/state_machine.js:26:10   @ request.<anonymous> (/projects/my-app/node_modules/aws-sdk/lib/request.js:38:9)   @ request.<anonymous> (/projects/my-app/node_modules/aws-sdk/lib/request.js:675:12)   @ request.calllisteners (/projects/my-app/node_modules/aws-sdk/lib/sequential_executor.js:115:18)   @ callnextlistener (/projects/my-app/node_modules/aws-sdk/lib/sequential_executor.js:95:12)   @ incomingmessage.onend (/projects/my-app/node_modules/aws-sdk/lib/event_listeners.js:244:11)   @ emitnone (events.js:91:20)   @ incomingmessage.emit (events.js:185:7)   @ endreadablent (_stream_readable.js:974:12)   @ _combinedtickcallback (internal/process/next_tick.js:74:11)   @ process._tickdomaincallback (internal/process/next_tick.js:122:9) 

it seems error thrown on stream , if you're listening on aws.request you'll receive error, stream error still not catched.

consider this:

s3.getobject({     bucket: 'foo',     key: 'bar' }).on('error', function (err) {     console.log('error event!'); }).createreadstream(); 

this show 'error event!' throw error , exit process. because stream created createreadstream receiving error.

consider this:

s3.getobject({     bucket: 'lalaland',     key: 'blabaliets' }).on('error', function (err) {     console.log('error event!'); }).createreadstream().on('error', function (err) {     console.log('error event on stream!'); }); 

it'll listen on aws.request object errors, also on stream object. first print 'error event!', 'error event on stream!' , process not exit.

note this:

s3.getobject({     bucket: 'lalaland',     key: 'blabaliets' }).createreadstream().on('error', function (err) {     console.log('error event on stream!'); }); 

will only print 'error event on stream!' , not exit. last option want: listen errors on stream , not on request object.

to original intentions; @ point (in on error on stream) want check if it's indeed notmodified error , res.status(304).end().


Comments