COS存储批量删除buckets脚本

时间:2022-07-22
本文章向大家介绍COS存储批量删除buckets脚本,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

一个简单的批量删除cos桶的shell脚本

echo -e "33[34;1;4m---------批量删除COS桶及文件脚本---------33[0m"
yum install python-pip -y
pip install -U cos-python-sdk-v5
pip install coscmd

echo -e "n"
read -p "请输入您的secret_id:" Secretid
read -p "请输入您的secret_key:" Secretkey
echo -e "n您的secret_id为:$Secretidn您的secret_key为:$Secretkeyn"
echo -e "33[43;31;7;1;5m温馨提示:为了您的账户和数据安全,请勿将secret_id和secret_key提供给他人33[0mn"
while true
do
read -p "请输入要删除的COS桶所在区域( 例:ap-guangzhou ap-beijing ):" Zone
cat >>test.py<<EOF
# -*- coding=utf-8
from qcloud_cos import CosConfig
from qcloud_cos import CosS3Client
import sys
import logging
import json
logging.basicConfig(level=logging.INFO, stream=sys.stdout)
secret_id = '$Secretid'      # 替换为用户的 secretId
secret_key = '$Secretkey'    # 替换为用户的 secretKey
region = '$Zone'     		 # 替换为用户的 Region
token = None                 # 使用临时密钥需要传入 Token,默认为空,可不填
scheme = 'https'             # 指定使用 http/https 协议来访问 COS,默认为 https,可不填
config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token, Scheme=scheme)
# 2. 获取客户端对象
client = CosS3Client(config)
response = client.list_buckets( )
print(json.dumps(response, ensure_ascii=False, indent=4))
EOF

buckets=`python test.py|grep -B1 "$Zone"|grep "Name"|awk -F '"' '{print $4}'`
default_bucket=`echo $buckets|awk '{print $1}'`
rm -f ~/.cos.conf
coscmd config -a $Secretid -s $Secretkey -b $default_bucket -r $Zone &>/dev/null

echo -e "n当前区域拥有的COS桶如下:n$buckets"
read -p "您确定要删除这些桶及桶中的文件吗 [ y | n ]: " Action
if [ $Action == "y" ];then
	for i in $buckets
	do
		coscmd -b $i -r $Zone deletebucket -f
	done
else
	break
fi

echo -e "n当前所有区域的COS桶分布情况如下:"
python test.py |grep "Location"|awk -F '"' '{print $4}'|sort|uniq -c|sort -r
read -p "是否继续删除其他区域的桶及文件 [ y | n ]:" Action1
if [ $Action1 == "n" ];then
	rm -f ./test.py
	echo "ByeBye"
	exit
fi

rm -f ./test.py
done