我的s3存储桶“测试”中有一个目录,我想删除此目录。
这就是我在做的
s3 = boto3.resource('s3')
s3.Object(S3Bucket,'test').delete()
并得到这样的回应
但是我的目录没有被删除!
我尝试使用'/test','test/'和'/test/'等的所有组合,也尝试使用该目录中的文件和空目录,但都无法删除'test'。
最佳答案
delete_objects
使您可以使用单个HTTP请求从存储桶中删除多个对象。您最多可以指定1000个键。
https://boto3.readthedocs.io/en/latest/reference/services/s3.html#S3.Bucket.delete_objects
import boto3
s3 = boto3.resource('s3')
bucket = s3.Bucket('my-bucket')
objects_to_delete = []
for obj in bucket.objects.filter(Prefix='test/'):
objects_to_delete.append({'Key': obj.key})
bucket.delete_objects(
Delete={
'Objects': objects_to_delete
}
)
关于python - Boto3,s3文件夹未删除,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33104579/