我可以使用以下方法删除Amazon S3上“文件夹”中的单个文件:
s3 = AWS::S3.new(:access_key_id => ENV['AWS_ACCESS_KEY_ID'], :secret_access_key => ENV['AWS_ACCESS_KEY'])
folder_path = 'uploads/'+@image.s3_filename
s3.buckets[ENV['AWS_BUCKET']].objects.with_prefix(folder_path).delete_all
但是这会留下一个空文件夹。我怎样才能完全删除文件夹(folder_path)?
答案 0 :(得分:12)
这是一个老问题,但您可以为aws-sdk 2.0>
执行此操作<https://mvnrepository.com/artifact/com.sun.xml.messaging.saaj/saaj-impl>
<https://mvnrepository.com/artifact/mx4j/mx4j-tools/2.1.1>
<https://mvnrepository.com/artifact/com.sun.xml.messaging.saaj/saaj-impl/1.3.2>
<https://mvnrepository.com/artifact/commons-jxpath/commons-jxpath/1.3>
s3 = Aws::S3::Resource.new
folder = 'path/to/the/folder'
objects = s3.bucket(ENV['S3_BUCKET_NAME']).objects({prefix: folder})
objects.batch_delete!
已弃用
希望这有帮助!
答案 1 :(得分:6)
S3上的所有内容都是您可以通过其“键”操纵的对象。如果你只是抓住它,你可以打电话给delete
:
s3.buckets[ENV['AWS_BUCKET']].objects["name of the folder"].delete
答案 2 :(得分:1)
删除文件夹后,您必须删除文件夹中的所有文件。