In a Node.js12.x based Lambda function, I am attempting to
- Download object from S3
- Download file from web if object not exists in S3
- PassThrough the downloaded data regardless of how its arrived to multiple write streams
I tried following
var request = require('request');
const readStream = ({ Bucket, Key }) => {
s3.getObjectMetadata({ Bucket, Key })
.promise().then(() => {
return s3.getObject().createReadStream();
})
.catch(error => {
if (error.statusCode === 404) {
return request.get('http://example.com/' + Key);
}
});
};
readStream({ ... })
.pipe(sharp().resize(w, h).toFormat('png'))
.pipe(writeStream);
Above works if the object available in s3, but the catch block doesn't work.
Do I need await or a promise on request.get?
I also tried following with no luck
http.get('http://example.com/' + Key, function(response) {
return response;
});