当指定的键不存在时,为什么 S3.deleteObject 不会失败?

Why does S3.deleteObject not fail when the specified key doesn't exist?

使用 AWS SDK for Node,为什么我在尝试删除不存在的对象(即 S3 密钥错误)时没有收到错误消息?

另一方面,如果我指定了一个不存在的桶,则会产生错误。

如果您考虑以下 Node 程序,Key 参数列出了存储桶中不存在的键,但回调的 error 参数是 null

var aws = require('aws-sdk')

function getSetting(name) {
  var value = process.env[name]
  if (value == null) {
    throw new Error('You must set the environment variable ' + name)
  }
  return value
}

var s3Client = new aws.S3({
  accessKeyId: getSetting('AWSACCESSKEYID'),
  secretAccessKey: getSetting('AWSSECRETACCESSKEY'),
  region: getSetting('AWSREGION'),
  params: {
    Bucket: getSetting('S3BUCKET'),
  },
})
picturePath = 'nothing/here'
s3Client.deleteObject({
  Key: picturePath,
}, function (err, data) {
  console.log('Delete object callback:', err)
})

因为那是 specs 说它应该做的。

deleteObject(params = {}, callback) ⇒ AWS.Request

Removes the null version (if there is one) of an object and inserts a delete marker, which becomes the latest version of the object. If there isn't a null version, Amazon S3 does not remove any objects.

因此,如果对象不存在,调用时仍然不会出错deleteObject,如果启用了版本控制,即使之前没有要删除的内容,它也会添加一个删除标记。