我正在尝试批量删除与我的数据库中的一个特定博客记录相关联的s3对象,但是我还想知道如何将数组传递给我的params对象以便在{{ 1}}方法,但我对这个错误持谨慎态度:s3.deleteObjects
。我觉得这可能与在流程中的某个时刻没有循环或者传递给我的Check with error message 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 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
答案 0 :(得分:4)
键应该是一个字符串。您应该使用Object to Objects数组 使用此代码:
var objects = [];
for(var k in file){
objects.push({Key : file[k].fileName});
}
var options = {
Bucket: process.env.AWS_BUCKET,
Delete: {
Objects: objects
}
};
答案 1 :(得分:0)
将数组更改为对象
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
});