echo -e "\033[34;1;4m---------批量删除COS桶及文件脚本---------\033[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为:$Secretid\n您的secret_key为:$Secretkey\n"
echo -e "\033[43;31;7;1;5m温馨提示:为了您的账户和数据安全,请勿将secret_id和secret_key提供给他人\033[0m\n"
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
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。