I can use this code to upload a single image to s3. When I try multiple, only the first item is uploaded and lambda stops executing. I'm new to javascript so I don't understand the problem here.
async function downloadImage(url) {
var options = {
uri: url,
encoding: null
};
await new Promise((resolve, reject) => {
request(options, function(error, response, body) {
if (error || response.statusCode !== 200) {
console.log("failed to get image");
console.log(error);
} else {
s3.putObject({
Body: body,
Key: 'template/'+url.split('/').pop(),
Bucket: bucketName
}, function(error, data) {
if (error) {
console.log("error downloading image to s3");
} else {
console.log("success uploading to s3");
}
});
}
});
})
.catch((error) => {
console.log("error");
});
}
exports.handler = async (event, _ctx, _cb) => {
var images = {
banner: "http://media.com/strip.png",
icon: "http://media.com/icon.png",
logo: "http://media.com/logo.png"
}
for (const [key, value] of Object.entries(images)) {
console.log(`${key}: ${value}`);
await downloadImage(value);
}
}
The output I get from lambda for this:
2020-10-03T19:11:32.293Z 3cd401a6-08c6-49a2-b01c-99d6430ffc1a INFO banner: http://media.com/circle/strip.png
2020-10-03T19:11:33.201Z 3cd401a6-08c6-49a2-b01c-99d6430ffc1a INFO success uploading to s3