我试图将listObjectsV2作为数组返回,而我尝试过的所有解决方案都返回了promise。
似乎listObjectsV2在实际呈现页面之前未及时完成。
aws-sdk S3: best way to list all keys with listObjectsV2
const s3 = require('../controllers/aws');
*/
exports.index = (req, res) => {
var user_obj = [];
User.findById(res.locals.user._id, function (err, user) {
if (err) {} else {
User.find({
'_id': {
$in: user.baseball_team
}
}).limit(10).exec(function (err, docs) {
if (docs) {
for (var i = 0; i < docs.length; i++) {
var params = {
Bucket: 'handover-little-league18',
Prefix: 'uploads/profile_images/' + docs[i]._id // Can be your folder name
};
var list = [];
s3.listObjectsV2(params, function (err, data) {
if (err) {
logger.error('Profile : s3 failed listing', err.stack);
} else {
data.Contents.forEach(item => {
url = item.Key;
if (url.substring(url.lastIndexOf('/') + 1) != '') {
list.push(url.substring(url.lastIndexOf('/') + 1));
}
});
}
});
obj = {
'name': docs[i].profile.name,
'id': docs[i]._id,
'images' : list
}
user_obj.push(obj);
}
}
});
res.render('team', {
title: 'Team',
team: user_obj,
});
}
});
}