利用七牛自带的 qshell 定期清理过期文件~
新项目需要上传临时图片在七牛中,七牛对存储空间有收费,于是乎,我们需要定期删除(仅保存半天内内)的文件,附上代码:
#!/bin/bash
#要删除的bucket
bucket="text"
currentTime=`date "+%Y-%m-%d %H:%M:%S"`
timestamp=`date -d "$currentTime" +%s`
# 保存多久的文件
let starttime=($timestamp-43200)
#配置您的账号信息,具体请参考:https://github.com/qiniu/qshell
qshell account xxx xxx
#取出bucket中的有文件
qshell listbucket $bucket ${bucket}_file.org
#取出key和创建时间
cat ${bucket}_file.org | awk '{print $1,$4}' > ${bucket}_file.list
let count=0
while read line
do
time=${line#* }
key=${line%% *}
let time=time/10000000
# 对比时间戳
if [ $time -le $starttime ]; then
res=`qshell delete ${bucket} ${key}`
let count+=1
echo $count, $key, $res
fi
done < ${bucket}_file.list
# 清除产生的临时
rm -rf ${bucket}_*
每个小时清理一次的crontab:
0 * * * * cd /home/www/ && sh clear_qiniu.sh > /home/www/clear_qiniu.log