使用 boto3 清空 s3 存储桶的最快方法是什么? [英] What is the fastest way to empty s3 bucket using boto3?
问题描述
我正在考虑删除然后重新创建存储桶(我后来意识到这是一个糟糕的选择).
I was thinking about deleting and then re-creating bucket (bad option which I realised later).
那么如何删除bucket中的所有对象呢?
Then how can delete all objects from the bucket?
我试过这个:http://boto3.readthedocs.io/en/latest/reference/services/s3.html#S3.Bucket.delete_objects
但它不会删除多个对象.
But it deletes multiple objects not all.
您能建议清空桶的最佳方法是什么吗?
can you suggest what is the best way to empty bucket ?
推荐答案
只需使用 aws cli.
aws s3 rm s3://mybucket --recursive
好吧,如果您坚持使用 boto3,则需要更长的答案.这将向 s3 发送一个删除标记.无需文件夹处理.bucket.Object.all 将创建一个不限制为 1K 的迭代器.
Well, for longer answer if you insists to use boto3. This will send a delete marker to s3. No folder handling required. bucket.Object.all will create a iterator that not limit to 1K .
import boto3
s3 = boto3.resource('s3')
bucket = s3.Bucket('my-bucket')
# suggested by Jordon Philips
bucket.objects.all().delete()
这篇关于使用 boto3 清空 s3 存储桶的最快方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!