我在代码中执行两个循环。下面是伪代码。
get the subfolder list in the bucket
Loop through every subfolders
loop through every objects in a subfolder
read the objects
delete the objects (So at the end the subfolder will be empty)
get the subfolder list again (asuming the empty subfolder also will be deleted and new subfolders can be created by someone)但是结果是,由于子文件夹仍然在桶中,所以我得到了一个无限循环。因此,在删除子文件夹中的每个对象之后,我找到了一个删除子文件夹的解决方案。但我找不到解决办法。请提出你的想法
下面是s3文件夹结构
├── bucket
└── folder-1
└── folder-2
└── folder-3发布于 2021-06-12 05:49:12
文件夹实际上并不存在于亚马逊的S3中。相反,每个对象的Key (文件名)由对象的完整路径组成。
因此,您只需要循环遍历给定前缀的每个对象并删除对象。
下面是Python中的一个示例:
import boto3
s3_resource = boto3.resource('s3')
for object in s3_resource.Bucket('bucket-name').objects.filter(Prefix='folder-name/'):
print('Deleting:', object.key)
object.delete()这也将删除子文件夹中的对象,因为它们具有相同的前缀(并且文件夹实际上并不存在)。
https://stackoverflow.com/questions/67945783
复制相似问题