2017-04-10 119 views
2

我在考慮刪除然後重新創建存儲桶(後來我意識到這是壞選項)。什麼是使用boto3清空s3桶的最快方法?

那麼如何從桶中刪除所有對象?

我嘗試這樣做:http://boto3.readthedocs.io/en/latest/reference/services/s3.html#S3.Bucket.delete_objects

但不是所有的刪除多個對象。

你能提出什麼是清空桶的最佳方法嗎?

+0

這是S3 API的限制 - [刪除多個對象](http://docs.aws.amazon.com/AmazonS3/latest/API/multiobjectdeleteapi.html)。您將不得不實施分頁並一次刪除1000個。 – AChampion

回答

3

只需使用aws cli

aws s3 rm s3://mybucket --recursive 

那麼,如果你堅持使用boto3,長時間回答。這會向s3發送一個刪除標記。不需要文件夾處理。 bucket.Object.all將創建一個不限於1K的迭代器。

import boto3  
s3 = boto3.resource('s3') 
bucket = s3.Bucket('my-bucket') 
# suggested by Jordon Philips 
bucket.objects.all().delete() 
# for obj in bucket.objects.all(): 
# obj.delete() 
bucket.delete() 
+0

雖然你的答案可能是正確的,但downvote是因爲問題明確地說「使用boto3」。 –

+0

@mootmoot你的步驟將工作,但我需要它與boto3工作:/ –

+0

@TusharNiras:你試過我的boto3代碼嗎? – mootmoot