gpt4 book ai didi

javascript - 通过服务器将多个文件上传到 S3 时如何从失败中恢复?

转载 作者:行者123 更新时间:2023-12-01 00:07:34 25 4
gpt4 key购买 nike

刚接触 NodeJS 和 S3,我编写了以下探索性代码,通过 NodeJS 服务器将文件上传到 S3,而不将文件保存到磁盘或内存:

var express = require('express');
var Busboy = require('busboy');
var S3 = require('../utils/s3Util');
var router = express.Router(); // mounted at /uploads

router.post("/", function (req, res, next) {
let bb = new Busboy({ headers: req.headers });
const uploads = [];
bb.on('file', (fieldname, stream, filename, encoding, mimeType) => {
console.log(`Uploaded fieldname: ${fieldname}; filename: ${filename}, mimeType: ${mimeType}`);
uploads.push(S3.svc.upload({ Bucket: 'my-test-bucket', Key: filename, Body: stream }).promise());
});
bb.on('finish', () => {
console.log("# of promises:", uploads.length);
Promise.all(uploads).then(retVals => {
for (let i = 0; retVals && i < retVals.length; i++) {
console.log(`File ${i + 1}::`, retVals[i]);
}
res.end();
}).catch(err => {
console.log("Error::", err);
res.status(500).send(`${err.name}: ${err.message}`);
});
});
req.pipe(bb);
});

module.exports = router;

一般失败的情况下,如果有1个或多个x个文件上传失败,该如何处理?有些上传会成功,有些会失败。但是,在 catch 子句中,我不知道哪些失败了......

如果能够使此上传过程有点事务性,那就太好了(即,要么所有上传成功,要么全部上传失败)。当错误发生时,理想情况下我能够“回滚”成功上传的子集。

最佳答案

你可以这样做:

将一个对象推送到上传中,并包含您需要重试的数据,因此:

uploads.push({ 
fieldname,
filename,
mimeType,
uploaded: S3.svc.upload({ Bucket: 'my-test-bucket', Key: filename, Body: stream })
.promise()
.then(() => true)
.catch(() => false)
});

...

const failed = await
(Promise.all(uploads.map(async upload => ({...upload, uploaded: await upload.uploaded})))).then(u => u.filter(upload => !upload.uploaded))

const failedFiles = failed.join(', ')

console.log(`The following files failed to upload: ${failedFiles}`);

您需要使事件处理程序异步以在其中使用await,因此,例如:

bb.on('file', async (fieldname, stream, filename, encoding, mimeType) => {

关于javascript - 通过服务器将多个文件上传到 S3 时如何从失败中恢复?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60274050/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com