从AWS删除多张图片

时间:2018-09-17 06:35:08

标签: javascript node.js amazon-web-services amazon-s3

这是我用来从aws删除多张图片的代码

AWS.config.update({
  accessKeyId: process.env.ACCESS_KEY,
  secretAccessKey: process.env.SECRET_ACCESS_KEY
})

const s3 = new AWS.S3({
  params: {
    Bucket: process.env.BUCKET
  }
})

这是我从前端传递来删除图像的错误

const array = [
  'https://s3.amazonaws.com/lgfit/picture/5b55a7a6c316686bbbbc8120.jpg',
  'https://s3.amazonaws.com/lgfit/picture/5b4c3ca0097c915e38d0d21515-09-2018_12_13_48.jpg'
]

这里我正在deleteObject的aws中传递图像

var options = {
  Bucket: process.env.BUCKET,
  Delete: {
    Objects: [{
      Key: array
    }],
  }
}
return new Bluebird((resolve) => {
  s3.deleteObject(options, function (err,data){
    console.log(err)
    console.log(data)
  })
})

我收到以下错误消息

{ MultipleValidationErrors: There were 2 validation errors:
* MissingRequiredParameter: Missing required key 'Key' in params
* UnexpectedParameter: Unexpected key 'Delete' found in params
    at ParamValidator.validate (/home/user/ashish/FitnessApp/Fitness-New-one/fitnessapp-backend/node_modules/aws-sdk/lib/param_validator.js:40:28)
    at Request.VALIDATE_PARAMETERS (/home/user/ashish/FitnessApp/Fitness-New-one/fitnessapp-backend/node_modules/aws-sdk/lib/event_listeners.js:108:42

请在这里帮助我做错了!!

3 个答案:

答案 0 :(得分:2)

您的代码中有两个错误。

  1. 您使用了错误的SDK方法。
  2. 您传递的参数格式错误。

deleteObjects是删除多个对象的方法 和 Delete.Objects期望key:string,但是您要提供key:array,因此您需要从数组中生成对象。我假设lgfit是您的水桶。

const params = {
    Bucket: process.env.BUCKET,
    Delete: {
        Objects: [{
                Key: "picture/5b55a7a6c316686bbbbc8120.jpg"
            },
            {
                Key: "picture/5b4c3ca0097c915e38d0d21515-09-2018_12_13_48.jpg"
            }
        ],
        Quiet: false
    }
};
return new Bluebird((resolve) => {
    s3.deleteObjects(params, function (err, data) {
        if (err) console.log(err, err.stack); // an error occurred
        else console.log(data); // successful response
        /*
        data = {
        Deleted: [
            {
        DeleteMarker: true, 
        DeleteMarkerVersionId: "A._w1z6EFiCF5uhtQMDal9JDkID9tQ7F", 
        Key: "objectkey1"
        }, 
            {
        DeleteMarker: true, 
        DeleteMarkerVersionId: "iOd_ORxhkKe_e8G8_oSGxt2PjsCZKlkt", 
        Key: "objectkey2"
        }
        ]
        }
        */
    });
});

答案 1 :(得分:1)

由于尝试使用单个HTTP请求删除多个对象,因此应使用s3.deleteObjects()而不是s3.deleteObject()。您可以尝试以下操作:

var options = {
  Bucket: process.env.BUCKET,
  Delete: {
    Objects: [{
      Key: array[0]
    },
    {
      Key: array[1]
    }],
  }
}
return new Bluebird((resolve) => {
  s3.deleteObjects(options, function (err,data){
    console.log(err)
    console.log(data)
  })
})

答案 2 :(得分:0)

将数组更改为对象

const imgList = [
  {Key: 'picture/5b55a7a6c316686bbbbc8120.jpg'},
  {Key: 'picture/5b4c3ca0097c915e38d0d21515-09-2018_12_13_48.jpg'}
]

将数组设置为参数中的对象值

const params = {
    Bucket: process.env.BUCKET,
    Delete: {
        Objects: imgList,
        Quiet: false
    }
};

现在删除对象

return new Bluebird((resolve) => {
    s3.deleteObjects(params, function (err, data) {
        if (err) console.log(err, err.stack); // an error occurred
        else console.log(data); // successful response
    });
});

Learn more from official docs