我的客户在S3上存储了大量图片。我们需要将这些图像作为响应返回。这是我写的代码:
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'));
}
}
);
我们如何才能得到回应的图像?
答案 0 :(得分:0)
var AWS = require('aws-sdk');
AWS.config.update({
accessKeyId: "AccessKey",
secretAccessKey: "SecretAccessKey"
});
var s3 = new AWS.S3();
s3.getObject({ Bucket: "BucketName", Key: "ETag.jpeg" },
function(error, data) {
if (error != null) {
alert("Failed to retrieve an object: " + error);
} else {
alert("Loaded " + data.ContentLength + " bytes");
}
}
);