admin管理员组文章数量:1400285
The code below will retrieve at least one object (possibly two) from AWS S3.
I am using the AWS JS SDK and retrieve the objects from within a loop as there is noway to retrieve multiple objects with one request at this moment in time.
After the objects have been retrieved I want to do some image position (the objects are images).
My problem is that the rest of my code executes before I have successfully retrieved the objects. I know this because objects
remains unchanged when logged to the console.
How do I ensure I receive the objects from S3 before I attempt to carry out my other function to manipulate the images?
var app = require('../application');
exports.generate = function (req, res) {
objects = {
logo: req.body.logo,
}
if (!req.body.background.startsWith('#')) {
objects.background = req.body.background;
}
for (type in objects) {
var params = {
Bucket: "my-bucket",
Key: objects[type]
};
app.s3.getObject(params, function(err, data) {
if (err) {
console.log(err, err.stack);
}
else {
objects[type] = data;
}
});
}
if (objects.background) {
gm(objects.logo).append(objects.background).write('temp.jpg', function() {
console.log('Logo and background have been appended');
});
}
console.log(objects);
console.log('Finished');
}
The console logs the following
{ logo: 'Original 106fm Logo #268390.jpg', background: 'test.jpg' }
Finished
When the images are retrieved the log should be showing the data body for each image.
The code below will retrieve at least one object (possibly two) from AWS S3.
I am using the AWS JS SDK and retrieve the objects from within a loop as there is noway to retrieve multiple objects with one request at this moment in time.
After the objects have been retrieved I want to do some image position (the objects are images).
My problem is that the rest of my code executes before I have successfully retrieved the objects. I know this because objects
remains unchanged when logged to the console.
How do I ensure I receive the objects from S3 before I attempt to carry out my other function to manipulate the images?
var app = require('../application');
exports.generate = function (req, res) {
objects = {
logo: req.body.logo,
}
if (!req.body.background.startsWith('#')) {
objects.background = req.body.background;
}
for (type in objects) {
var params = {
Bucket: "my-bucket",
Key: objects[type]
};
app.s3.getObject(params, function(err, data) {
if (err) {
console.log(err, err.stack);
}
else {
objects[type] = data;
}
});
}
if (objects.background) {
gm(objects.logo).append(objects.background).write('temp.jpg', function() {
console.log('Logo and background have been appended');
});
}
console.log(objects);
console.log('Finished');
}
The console logs the following
{ logo: 'Original 106fm Logo #268390.jpg', background: 'test.jpg' }
Finished
When the images are retrieved the log should be showing the data body for each image.
Share Improve this question edited Jul 11, 2017 at 9:06 Joe Ainsworth asked Jul 11, 2017 at 8:56 Joe AinsworthJoe Ainsworth 5712 gold badges7 silver badges20 bronze badges2 Answers
Reset to default 4Key is to use Promise Objects
app.get('/api/template', (req, res) => {
let promises = [getS3Object(`templates/x.tpl`),
getS3Object(`templates/y.tpl`),
getS3Object(`templates/z.tpl`),
getS3Object('templates/b.tpl')];
return Promise.all(promises)
.then((pres) => {
const iterable = [0, 1, 2, 3];
for (let value of iterable) {
if (!pres[value].statusCode) {
res.send(pres[value]);
}
}
})
.catch((res) => {
console.log(`Error Getting Templates: ${res}`);
});
});
const getS3Object = key => {
return new Promise((resolve, reject) => {
s3.getObject({
Key: key
}, (err, data) => {
if (err){
resolve(err)
} else {
resolve(data.Body)
}
})
})
}
This happens because your console.log
calls are executing prior to download files. What you can do is process files after receiving them.
app.s3.getObject(params, function(err, data) {
if (err) {
console.log(err, err.stack);
}
else {
objects[type] = data;
//Here you have the downloaded files. Do your processing here
console.log(objects);
}
});
Asynchronous behavior is what you should read more about.
[Update] you can use following wrapper module to download multiple files
Download multiple files using aws s3
本文标签:
版权声明:本文标题:amazon web services - How to retrieve multiple objects from AWS S3 using Javascript and then perform a function when they have b 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744226926a2596136.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论