问题描述
如标题所示,我目前正在使用sailjs
+ skipper-better-s3
进行s3上传。首先上传一个效果很好的文件,然后因为更改要求立即上传多个文件,所以我添加了一个for
循环,但是这样做,所有键都是相同的,最后只有一个文件已上传,这是最后一个上传的文件,但具有第一个上传文件名。
我确实读过一些文章,有人说The problem is because for loop is synchronous and file upload is asynchronous
之类的东西,有人说这样做的结果是使用了我尝试过的递归,但运气不好,同样的事情也会发生。
我的递归代码在下面...
s3_upload_multi: async (req,res) => {
const generatePath = (rootPath,fieldName) => {
let path;
// this is just a switch statement here to check which fieldName is provided then value of path will depend on it
// as for the other two variable is just checking if upload content type is correct
return { path };
};
const processUpload = async ({
fieldName,awsOp,fileExtension,rootPath,fileName,}) => {
return new Promise(function (resolve,reject) {
req.file(fieldName).upload(awsOp,async (err,filesUploaded) => {
if (err) reject(err);
const filesUploadedF = filesUploaded[0]; // F = first file
const response = {
status: true,errCode: 200,msg: 'OK',response: {
url: filesUploadedF.extra.Location,size: filesUploadedF.size,type: fileExtension,filename: filesUploadedF.filename,key: filesUploadedF.extra.Key,field: fieldName,}
};
resolve(response);
});
});
}
const process_recur = async (files,fieldName) => {
if (files.length <= 0) return;
const fileUpload = files[0].stream;
const rootPath = `${sails.config.aws.upload.path.root}`;
const fileCType = fileUpload.headers['content-type'];
// console.log(fileCType,'fileCType');
const { path } = generatePath(rootPath,fieldName);
const fileName = fileUpload.filename;
const fileExtension = fileUpload.filename.split('.').pop();
const genRan = await UtilsService.genRan(8);
const fullPath = `${path}${genRan}-${fileName}`;
const awsOp = {
adapter: require('skipper-better-s3'),key: sails.config.aws.access_key,secret: sails.config.aws.secret_key,saveAs: fullPath,bucket: sails.config.aws.bucket,s3params: {
ACL: 'public-read'
},};
const config = {
fieldName,}
const procceed = await processUpload(config);
files.shift();
await process_recur(files,fieldName);
};
try {
const fieldName = req._fileparser.upstreams[0].fieldName;
const files = req.file(fieldName)._files;
await process_recur(files,fieldName);
} catch (e) {
console.log(e,'inside UploadService');
return false;
}
}
下面是我使用for循环的代码,虽然从上面看是很相似的
s3_upload_multi: async (req,}
};
resolve(response);
});
});
}
try {
const fieldName = req._fileparser.upstreams[0].fieldName;
const files = req.file(fieldName)._files;
for (const file of files) {
const fileUpload = file.stream;
const rootPath = `${sails.config.aws.upload.path.root}`;
const fileCType = fileUpload.headers['content-type'];
// console.log(fileCType,'fileCType');
const fileName = fileUpload.filename;
const { path } = generatePath(rootPath,fieldName);
const fileExtension = fileUpload.filename.split('.').pop();
// using a variable here because if this is an image,a thumbnail will be created with the same name as the original one
const genRan = await UtilsService.genRan(8);
const fullPath = await `${path}${genRan}-${fileName}`;
const awsOp = {
adapter: require('skipper-better-s3'),s3params: {
ACL: 'public-read'
},};
const config = {
fieldName,}
const procceed = await processUpload(config);
console.log(procceed,'procceed');
}
} catch (e) {
console.log(e,'inside UploadService');
return false;
}
}
我在哪个部分犯了导致此类行为的错误?我console.log
在此先感谢您的建议和帮助。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)