arrays - Nodejs S3删除多个对象错误

标签 arrays node.js express amazon-s3

我正在尝试批量删除与数据库中的一个特定博客记录关联的 s3 对象,但我对如何将数组传递到要在 s3.deleteObjects 方法中使用的 params 对象感到困惑,但我遇到了以下错误:检查错误消息 InvalidParameterType: Expected params.Delete.Objects[0].Key to be a string。我觉得这可能与过程中某个时刻没有循环有关,或者可能与传递给我的 s3File 数组的值的格式有关。

这是我的路由:

.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 every 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

最佳答案

键应该是一个字符串。您应该使用对象到对象的数组。
使用此代码:

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

关于arrays - Nodejs S3删除多个对象错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42287560/

相关文章:

node.js - 如何设计一条了解一天中时间的快速路线

arrays - Mongoose 查询 : find an object by id in an array

Java - 如何访问在 void 函数中创建的数组

java - 使用给定的字符串参数搜索数组元素

javascript - Stripe/node.js : how retrieve stripe subscription safely + increment 1

mysql - 如何将各种 MySQL 查询结果合并到单个 JSON 对象中

java - 任意维度的数组作为方法参数

node.js - Openshift 应用程序进入 "idle"且没有唤醒

javascript - Jade 选择字段填充数据

node.js - 如何在express-validator的checkSchema下的isLength类型验证中为min、max提供不同的错误消息