Arrays Nodejs S3删除多个对象错误

Arrays Nodejs S3删除多个对象错误,arrays,node.js,express,amazon-s3,Arrays,Node.js,Express,Amazon S3,我正试图批量删除与数据库中的一个特定博客记录关联的s3对象,但我正在考虑如何将数组传递给我的params对象,以便在s3.deleteObjects方法中使用,但我遇到了以下错误:检查错误消息InvalidParameterType:Expected params.delete.objects[0]。键必须是字符串。我觉得这可能与过程中的某个点没有循环有关,也可能与传递到我的s3File数组的值的格式有关 这是我的路线: .delete(function(req, res){ models.F

我正试图批量删除与数据库中的一个特定博客记录关联的s3对象,但我正在考虑如何将数组传递给我的params对象,以便在
s3.deleteObjects
方法中使用,但我遇到了以下错误:
检查错误消息InvalidParameterType:Expected params.delete.objects[0]。键必须是字符串。我觉得这可能与过程中的某个点没有循环有关,也可能与传递到我的
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);
                }
            });
});
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
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
这里是控制台.log的输出('这里是每个文件'+文件[k].fileName);

.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);
                }
            });
});
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
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
这里是控制台.log的输出('这里是S3文件'+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);
                }
            });
});
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
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
以下是错误消息:

.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);
                }
            });
});
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
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
  }
};

将数组更改为对象

const objects = [
  {Key: 'image1.jpg'},
  {Key: 'image2.jpg'}
]
for(var k in file){
  objects.push({Key : file[k].fileName});
}
将新项目添加到列表中

const objects = [
  {Key: 'image1.jpg'},
  {Key: 'image2.jpg'}
]
for(var k in file){
  objects.push({Key : file[k].fileName});
}
将数组设置为参数中的对象值

const options = {
    Bucket: process.env.BUCKET,
    Delete: {
        Objects: objects,
        Quiet: false
    }
};
 s3.deleteObjects(options, function(err, data) {
   if (err) console.log(err, err.stack); // an error occurred
   else     console.log(data);           // successful response
});
现在删除对象

const options = {
    Bucket: process.env.BUCKET,
    Delete: {
        Objects: objects,
        Quiet: false
    }
};
 s3.deleteObjects(options, function(err, data) {
   if (err) console.log(err, err.stack); // an error occurred
   else     console.log(data);           // successful response
});

这是文件