2017-02-17 66 views
0

我想批量刪除我的s3對象與我的數據庫中的一個特定的博客記錄相關聯,但我越來越掛斷如何將數組傳遞給我的PARAM對象使用在s3.deleteObjects方法中,但我被這個錯誤阻止:Check with error message InvalidParameterType: Expected params.Delete.Objects[0].Key to be a string。我覺得這可能與在這個過程的某一點沒有循環有關,也可能是值的格式被傳遞給我的s3File陣列。Nodejs S3刪除多個對象錯誤

這裏是我的路由:

.delete(function(req, res){ 

models.File.findAll({ 
    where: { 
     blogId: blog.blogId 
    } 
}).then(function(file){ 

var s3Files = []; 

      function s3Key(link){ 
       var parsedUrl = url.parse(link); 
       var fileName = parsedUrl.path.substring(1); 
       return fileName; 
      } 


      for(var k in file){ 
       console.log('Here are each files ' + file[k].fileName); 
       s3Files.push(s3Key(file[k].fileName)); 
      } 

      console.log('Here are the s3Files ' + s3Files); 

      //GOTTEN TO THIS POINT WITHOUT AN ERROR 
      aws.config.update({accessKeyId: process.env.AWS_ACCESS_KEY, secretAccessKey: process.env.AWS_SECRET_KEY, region: process.env.AWS_REGION}); 



      //var awsKeyPath = s3Key(file.fileName); 

      var s3 = new aws.S3(); 

      var options = { 
       Bucket: process.env.AWS_BUCKET, 
       Delete: { 
       Objects: [{ 
        Key: s3Files 
       }], 
       }, 
      }; 

      s3.deleteObjects(options, function(err, data){ 
       if(data){ 
        console.log("File successfully deleted"); 
       } else { 
        console.log("Check with error message " + err); 
       } 
      }); 
}); 

下面是console.log('Here are each files ' + file[k].fileName);輸出:

Here are each files https://local-bucket.s3.amazonaws.com/1/2017-02-12/screen_shot_2017-02-01_at_8_25_03_pm.png 
Here are each files https://local-bucket.s3.amazonaws.com/1/2017-02-13/test.xlsx 
Here are each files https://local-bucket.s3.amazonaws.com/1/2017-02-13/screen-shot-2017-02-08-at-8.23.37-pm.png 

下面是console.log('Here are the s3Files ' + s3Files);輸出:

Here are the s3Files 1/2017-02-12/screen_shot_2017-02-01_at_8_25_03_pm.png,1/2017-02-13/test.xlsx,1/2017-02-13/screen-shot-2017-02-08-at-8.23.37-pm.png 

以下是錯誤消息:

Check with error message InvalidParameterType: Expected params.Delete.Objects[0].Key to be a string 

回答

1

重點應該是一個字符串。您應該使用對象的數組作爲對象。
使用此代碼:

var objects = []; 
for(var k in file){ 
    objects.push({Key : file[k].fileName}); 
} 
var options = { 
    Bucket: process.env.AWS_BUCKET, 
    Delete: { 
    Objects: objects 
    } 
}; 
+0

這工作完美。謝謝! – cphill