簡介
一個非空bucket是無法直接刪除的薪者,最好的辦法是將bucket里面的objects全部刪除后再刪除bucket竿裂。
代碼
#!/bin/python
import sys
import os
import boto
import boto.s3.connection
from boto.s3.key import Key
from multiprocessing import Process
from multiprocessing import Queue as bigqueue
acckey = 'IIIIIIIIIIIIIIIII'
seckey = 'fGGGGGGGGGGGGGGGGGGGGGGGGG'
mhost = '192.168.1.1'
def get_buckets(cmd, all_buckets):
'''
Get buckets from radosgw
'''
buckets = os.popen(cmd).readlines()
buckets = buckets[1:-2]
for x in buckets:
tt = x.replace('"','')
yy = tt.replace(' ','')
uu = yy[:-2]
ii = uu.strip()
all_buckets.put(ii)
return all_buckets.qsize()
class Delete_buckets(Process):
'''
Delete specified bucket
'''
def __init__(self, bucketname):
Process.__init__(self)
self.bucketname = bucketname
def run(self):
while True:
self.nowbucket = ''
try:
self.nowbucket = self.bucketname.get(timeout=2)
print 'get bucket '+ self.nowbucket
except:
print 'all finish!'
return
self.bucket_file = 'sudo radosgw-admin bi list --bucket='+self.nowbucket+'|grep \'"name"\' |cut -d ":" -f2 |cut -d \'"\' -f 2'
self.conn = boto.connect_s3(aws_access_key_id=acckey,
aws_secret_access_key=seckey,
host=mhost,
port=7480,
is_secure=False,
calling_format = boto.s3.connection.OrdinaryCallingFormat())
self.dbucket = self.conn.get_bucket(self.nowbucket)
self.all_files = os.popen(self.bucket_file).readlines()
for x in self.all_files:
self.dbucket.delete_key(x.strip())
try:
self.conn.delete_bucket(self.nowbucket)
print self.nowbucket + 'have deleted.'
except Exception,e:
print str(e)
pass
if __name__ == '__main__':
procs = 25
all_proc = []
del_buckets = bigqueue()
if sys.argv[1] == 'all':
cmd = 'sudo radosgw-admin bucket list'
bcount = get_buckets(cmd=cmd, all_buckets=del_buckets)
print 'begin to delete {bcount} bucket'.format(bcount=str(bcount))
for x in xrange(0,procs):
tempp = Delete_buckets(bucketname=del_buckets)
tempp.daemon = True
all_proc.append(tempp)
tempp.start()
for y in all_proc:
y.join()
else:
print 'spec'
參考
http://www.strugglesquirrel.com/2018/04/24/記錄一個快速刪除bucket的python腳本/