【问题标题】:How to upload multiple files parallelly to amazon S3?如何将多个文件并行上传到亚马逊 S3?
【发布时间】:2020-01-15 10:06:37
【问题描述】:

我使用的是纯 js 代码,并且我有一个多输入文件,我想将它的值上传到亚马逊 s3,上传到 s3 的代码可以正常工作,但是当我上传到其中一个输入文件时并且它还没有完成并在另一个输入文件中上传代码将被覆盖到第二次上传,所以我尝试使用这里的代码https://medium.com/@iamsohail/how-to-upload-multiple-files-parallelly-to-amazon-s3-3b9ac3630806

将多个文件并行上传到amazon S3没有冲突,但我遇到了很多问题,

var AWS = require('aws-sdk'),
    multer = require('multer');

var upload = multer({ dest: 'uploads/' });

var fileChooser = document.getElementsByClassName('s3-input');
if (fileChooser) {
  for (var i = 0; i < fileChooser.length; i++) {

    fileChooser[i].addEventListener('change', function () {
        var filechooserr = this;
        var inputFile = $(this);
        console.log(inputFile)
        var s3url = $(this).parent().next();
      var file = this.files[0];
            if (file) {

                // this code to rename input file
            var today = new Date();
            var date = today.getDate()  + '-' + (today.getMonth()+1) + '-' + today.getFullYear();
            var time = today.getHours() + "-" + today.getMinutes() + "-" + today.getSeconds();
            var userId = filechooserr.getAttribute("data-user-id");
            var uniqid = filechooserr.getAttribute("data-uniqid");

            console.log("file name: " + file.name + " file type: " + file.type + "file extention: " + file.name.split('.').pop() );
            if (this.classList.contains('personal-card-img')) {
              var myNewFile = new File([file.name], 'Teachers/personal-card/'+ userId +"-"+ date +"-"+ uniqid + "." + file.name.split('.').pop() , {type: file.type});
            } else if (this.classList.contains('profile-image-s3url')){
              var myNewFile = new File([file.name], 'Teachers/Avatar/'+ userId +"-"+ date +"-"+ uniqid + "." + file.name.split('.').pop() , {type: file.type});
            } else if (this.classList.contains('qualification-s3url')){
                var myNewFile = new File([file.name], 'Teachers/Qualification/'+ userId +"-"+ date +"-"+ uniqid + "." + file.name.split('.').pop() , {type: file.type});

            } else if (this.classList.contains('credit-card-img')){
                var myNewFile = new File([file.name], 'Teachers/Credit-Card/'+ userId +"-"+ date +"-"+ uniqid + "." + file.name.split('.').pop() , {type: file.type});
            } else {
                var myNewFile = new File([file.name], 'Teachers/anythingElse/'+ userId +"-"+ date +"-"+ uniqid + "." + file.name.split('.').pop() , {type: file.type});
            }

                AWS.config.update({
                    accessKeyId: "MYPRIVETKEY",
                    secretAccessKey: "MYACSESSKRY",
                    region: "ap-south-1"
                });

                var s3 = new AWS.S3({
                    httpOptions: {
                        timeout: 1000 * 2000//1sec = 33minutes
                    }
                });
                async function uploadFile(fileName, fileKey) {
                    return new Promise(async function(resolve, reject) {
                        var params = {
                            Bucket: "bucket-name",
                            Key: myNewFile.name,
                            ContentType: myNewFile.type,
                            Body: file,
                            ACL: 'public-read'
                    };
                        await s3.upload(params, function(s3Err, data) {
                            if (s3Err){
                                reject(s3Err);
                            }
                            console.log(`File uploaded successfully at ${data.Location}`);
                             $(s3url).val(data.Location);
                                resolve(data.Location);
                        });
                    });
                }
                var uploadFilePromises = [];
                var screenShot = request.files.screenShots;
                var apk = request.files.apk;
                var cpUpload = upload.fields([{
                        name: screenShot,
                        maxCount:5 
                    },
                    { name: apk, 
                        maxCount:1
                        }
                ]);
                router.post("/updateApp", cpUpload, async function (req, res, next) {
                    console.log("asmaa")
                });
                var apkFileKey = apk;
                uploadFilePromises.push(uploadFile(apk[0], apkFileKey));
                var screenShotFileKey = screenShot;
                uploadFilePromises.push(uploadFile(screenShot[0], screenShotFileKey));
                
                Promise.all(uploadFilePromises).then(async (values) => {
                    console.log(values);
                    }, reason => {
                    console.log(reason);
                });

            } else {
                    console.log("there is no file to upload")
            }
   
        }, false);
     
  }

}

第一个问题是关于multer插件的:我不知道怎么用。我是新来的, 当我执行require('multer'); 时,控制台出现错误:

Uncaught TypeError: required is not defined

我使用gulp.js,所以我通过添加此代码解决了这个问题

.pipe(rollup({ plugins: [json({compact: true}), babel(), resolve(), commonjs()] }, 'umd'))

但我认为它并没有解决问题,因为我遇到了另一个问题:

Uncaught TypeError: Cannot read property 'memoizedProperty' of undefined

那么,有没有人可以将多个文件并行上传到亚马逊?

【问题讨论】:

  • 欢迎来到 SO,感谢您为您的问题付出了这么多努力。你有一些排版问题,我更正了。

标签: javascript amazon-web-services amazon-s3 gulp


【解决方案1】:

您似乎在浏览器(客户端 js 运行器)上使用为 node.js(服务器端 js 运行器)设计的 API。如果是这种情况,那么只要您使用的 api 是为与 node.js 或类似的服务器端平台一起使用而设计的,那么这将不起作用。你需要下载 node.js 并在那里部署你的代码,也许还需要安装 express.js。网络上有一些关于如何部署 node.js 和应用程序或页面的教程。

【讨论】:

  • 实际上,我是新来使用这个的,请你分享一些链接来指导我解决问题!
  • 确定,您希望它与网络服务器+浏览器一起使用吗?或者控制台应用程序就足够了?对于网络服务器+浏览器,您可以检查例如 developer.mozilla.org/en-US/docs/Learn/Server-side/…
猜你喜欢
  • 2019-02-02
  • 2013-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多