How to delete files recursively from an S3 bucket

后端 未结 12 491
不知归路
不知归路 2021-01-29 23:54

I have the following folder structure in S3. Is there a way to recursively remove all files under a certain folder (say foo/bar1 or foo or foo/bar2/1 ..)

         


        
12条回答
  •  故里飘歌
    2021-01-30 00:27

    In case using AWS-SKD for ruby V2.

    s3.list_objects(bucket: bucket_name, prefix: "foo/").contents.each do |obj|
      next if obj.key == "foo/" 
      resp = s3.delete_object({
        bucket: bucket_name,
        key: obj.key,
      })
    end
    

    attention please, all "foo/*" under bucket will delete.

提交回复
热议问题