简体   繁体   中英

AngularJS AWS S3 sdk putObject won't send multiple objects

Here is my controller code:

angular.module("MODULE").controller('CTRL', function($scope,
                                                                      $http,
                                                                      $location,
                                                                      $rootScope,
                                                                      $window,
                                                                      uuid) {
    $scope.uploading = [];
    $scope.uploaded = [];
    $scope.files = [];
    $scope.creds = {
      bucket: 'BUCKET',
      access_key: 'KEY',
      secret_key: 'SECRET'
    };

    $scope.abort = function(index) {
        $scope.upload[index].abort();
        $scope.upload[index] = null;
    };

    var unreg = $scope.$watch('files.length', function(){
        $scope.uploadImages();
    });


    $scope.server_upl = function(){
      console.log($scope.uploaded.length);
      if($scope.uploaded.length == $scope.files.length){
        $http({
          url: "/mupl",
          method: "POST",
          data: {ti: $scope.uploaded}
        }).success(function(data){
          if(data == "OK"){
            alert("All Done");
          }

        });
      }
    };

    $scope.uploadImages = function(){
      if($scope.files && $scope.files.length != 0){
        unreg();
        console.log($scope.files);
        AWS.config.update({ accessKeyId: $scope.creds.access_key,
                            secretAccessKey: $scope.creds.secret_key
                          });

        AWS.config.region = 'us-east-1';
        for(var i=0; i< $scope.files.length; i++){
          var bucket = new AWS.S3({ params: { Bucket: $scope.creds.bucket } });
          console.log($scope.files[i]);
          var file = $scope.files[i];
          var file_type = file.type.split("/")[1];
          var _uuid = uuid.new();
          var key = _uuid + "." + file_type;
          console.log("I: " + i);
          console.log("KEY: " + key);

          var params = { Key: key,
                         ContentType: file.type,
                         Body: file,
                         ServerSideEncryption: 'AES256' };

          bucket.putObject(params, function(err, data) {
            if(err) {
              // There Was An Error With Your S3 Config
              console.log(err);
              return false;
            }
            else {
              // Success!
              console.log("Temp Bucket Upload Done");
              $scope.uploaded.push(key);
              $scope.server_upl();
            }
          })
          .on('httpUploadProgress',function(progress) {
            // Log Progress Information
            //console.log(Math.round(progress.loaded / progress.total * 100) + '% done');
          });
        }
      } //if(files && files.length)
      else{
        console.log("No file selected");
      }
    }
});

In $scope.uploadImages for every file I'm creating a uuid. But if I browse for multiple files(n), somehow(?), for loops for n times and $scope.uploaded has n entities but all the entities has same uuid. What is the thing that I don't see here?

You set uuid here: var _uuid = uuid.new(); but then never change it. So every key for the file will have the same uuid appended with the file_type.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM