对node.js使用aws-sdk(2.0.18),我尝试使用deleteObjects
方法删除多个对象。
delete: function (objects, callback) {
var s3delete = new AWS.S3();
var params = {
Bucket: config.s3.bucket,
Delete: {
Objects: objects
}
};
s3delete.deleteObjects(params, function (err, deleted) {
if (err) {
console.log(err, err.stack);
return callback(err);
}
return callback(null, deleted);
});
}
我的params对象看起来像这样:
{ Bucket: 'bucketname',
Delete: {
Objects:[
{"Key":"folder/file.extension"},
{"Key":"folder/file.extension"},
{"Key":"folder/file.extension"},
{"Key":"folder/file.extension"},
{"Key":"folder/file.extension"},
{"Key":"folder/file.extension"},
{"Key":"folder/file.extension"}
]
}
}
这会返回以下错误:
{
message: "The XML you provided was not well-formed or did not validate against our published schema",
code: "MalformedXML",
time: "2014-10-11T10:35:52.525Z",
statusCode: 400,
retryable: false
}
根据API spec,这应该有效。仅供参考:我从Mongoose模型中传递objects
参数,它在Mongoose中存储为混合。
答案 0 :(得分:1)
这是由Mongoose在原始objects
数组中返回格式错误的JSON对象引起的。
为了解决这个问题,我必须创建一个新的Object并将它们推送到一个新的数组,在这种特殊情况下:
var s3Objects = [];
objects.forEach(function(ele, index, arr) {
console.log(index + ': ' + ele.Key);
var thisKey = {
Key: ele.Key
};
s3Objects.push(thisKey);
});
然后将s3Objects
传递给参数:
var params = {
Bucket: config.s3.bucket,
Delete: {
Objects: properObjects
}
};
它现在有效。