My client has stored a large number of images on S3. We need to get these images back as response. Here is the code I've written:
var AWS = require('aws-sdk');
AWS.config.update({
accessKeyId: "AccessKey",
secretAccessKey: "SecretAccessKey"
});
var s3 = new AWS.S3();
s3.getObject({ Bucket: "BucketName", Key: "ImageURL" },
function(error, data) {
if (error != null) {
var _length = data.Body.length;
//Execution freezes here. Don't get any response.
res.writeHead(200, {
'Content-Type': 'image/jpeg',
'Content-Length': _length
});
res.end(new Buffer(data.Body, 'binary'));
}
}
);
How can we get the image in response?
//Execution freezes here.
You don't have anelse
block. And why exactly do you treatif (error != null)
as... success? Don't you have the logic backwards? – Michael - sqlbot Aug 22 '17 at 23:49