尝试将CSV转换为JSON,然后下载图片,然后将其上传到S3存储桶,然后更新当前JSON,最后生成新的CSV。但是,代码不会等待异步功能完成,因此const data = await handle_image_URLs(csv);
返回为 undefined 。
//parse CSV - extract images, then upload to S3 bucket
const handle_image_URLs = file => {
new Promise((resolve, reject) => {
fs.createReadStream(file)
.pipe(csv_to_json())
.on("data", async data => {
// console.log("data", data);
try {
if (is_image_url(data.image_link)) {
console.log("data.image_link", data.image_link);
image_buffer = await download_image(data.image_link);
name = uuid() + ".jpg";
const location = await upload_to_S3(image_buffer, name);
//Inject back updated image url
data.image_link = location.Location;
//New generated data goes into an array
results.push(data);
}
// console.log("RESULT", results);
resolve(results);
} catch (error) {
reject(error);
}
});
});
};
表达以处理csv上传
app.post("/", upload.single("file"), async (req, res) => {
const file = req.file;
const csv = `./uploads/${file.originalname}`;
const data = await handle_image_URLs(csv);
console.log("DATA", data);
// await json_to_csv_converter.json2csv(data, json2csvCallback);
res.status(200).json({ message: "working " });
});
答案 0 :(得分:1)
将return new promise((resolve, reject))
更新为return new promise( async (resolve, reject))
,它应该可以工作