如何使用ruby aws s3一次删除许多对象

时间:2018-07-17 14:12:55

标签: ruby-on-rails ruby amazon-s3 aws-sdk

我找不到使用ruby aws sdk一次删除多个对象的方法,在文档中有一个有关如何删除包含文件的文件夹的代码示例:

# batch operations, delete objects in batches of 1k
bucket.objects(prefix: '/tmp-files/').delete

寻找的是提供文件密钥并立即将其删除,就像这样:

    bucket.objects(keys: [
    'avatars/0b722c42-4003-42cc-887c-b820cad3aed9/avatar1.png',
'avatars/be1275f1-664f-4d47-9890-b5b2a066a18f/avatar2.jpg', 
    'avatars/f5051ed2-b2e7-4844-bdc7-ca19c1900ce8/avatar3.gif'
    ]).delete 

有什么想法吗?

1 个答案:

答案 0 :(得分:4)

您可以使用delete_objects 作为文档示例:

bucket.delete_objects({
  delete: { # required
    objects: [ # required
      {
        key: "ObjectKey", # required
        version_id: "ObjectVersionId",
      },
    ],
    quiet: false,
  },
  mfa: "MFA",
  request_payer: "requester", # accepts requester
})

所以在您的示例中:

bucket.delete_objects({
 delete:{
  objects: [
   {key: 'avatars/0b722c42-4003-42cc-887c-b820cad3aed9/avatar1.png'},
   {key:'avatars/be1275f1-664f-4d47-9890-b5b2a066a18f/avatar2.jpg'}, 
   {key:'avatars/f5051ed2-b2e7-4844-bdc7-ca19c1900ce8/avatar3.gif}'
  ]
 }
})