问题描述
我正在尝试使用批量文件上传到s3。
以某种方式,如果我使用回调上传,它将可以正常工作,但是我想将所有返回数据推入数组,然后再执行一些操作。但这是行不通的。
我在网上看时,看到诸如使用async await
或recuRSSive
之类的答案有效,但仍然无法正常工作。我什至尝试使用reduce
,但也没有运气
我的reduce
return files.reduce((accumulator,current) => {
const {path,buffer} = current;
const s3 = new AWS.S3();
// https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#putobject-property
s3.putObject(awsS3sdkParams(path,buffer),function ( err,data ) {
const { protocol,host } = this.request.httpRequest.endpoint;
data.params = this.request.params;
data.params.url = `${protocol}//${host}/${data.params.Key}`;
return [...accumulator,data];
});
},[]);
使用递归的示例
const result = [];
const helper = (files) => {
const {path,buffer} = files[0];
const s3 = new AWS.S3();
// https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#putobject-property
s3.putObject(UploadService.awsS3sdkParams(path,function (err,data){
const { protocol,host } = this.request.httpRequest.endpoint;
data.params = this.request.params;
data.params.url = `${protocol}//${host}/${data.params.Key}`;
UtilsService.clDebug(data,'data');
result.push(data);
files.shift();
if(files.length > 0) return helper(files);
});
};
helper(files);
return results;
使用承诺的示例
const result = [];
for(let {path,buffer} of files){
const s3 = new AWS.S3();
s3.putObject(awsS3sdkParams(path,buffer)).promise()
.then(file => {
result.push(file);
})
.catch(err => {
console.log(err,'errs');
});
}
我几乎可以理解为什么结果总是为[]
,但是如何使结果起作用?
我无法使用异步等待的原因是因为我尝试过,但是以某种方式将文件上载了不良数据后,我什至无法打开文件,或者键是相同的...
有人有其他建议吗?
提前感谢
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)