How to delete files recursively from an S3 bucket

后端 未结 12 488
不知归路
不知归路 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:20

    I needed to do the following...

    def delete_bucket
      s3 = init_amazon_s3
      s3.buckets['BUCKET-NAME'].objects.each do |obj|
        obj.delete
      end
    end
    
    def init_amazon_s3
      config = YAML.load_file("#{Rails.root}/config/s3.yml")
      AWS.config(:access_key_id => config['access_key_id'],:secret_access_key => config['secret_access_key'])
      s3 = AWS::S3.new
    end
    

提交回复
热议问题