Pra*_*u R 1 file amazon-s3 amazon-web-services delete-file
需要删除超过 7 天的 S3 中的文件,需要一个 shell 脚本来做到这一点,谷歌搜索不走运,我找到了下面的网址
http://shout.setfive.com/2011/12/05/deleting-files-older-than-specified-time-with-s3cmd-and-bash/
这对我们没有帮助,有人有删除超过 7 天的所有文件的脚本吗?
最简单的方法是在 Amazon S3 存储桶上定义对象生命周期管理。
您可以指定应过期(删除)超过特定天数的对象。最好的部分是这会定期自动发生,您无需运行自己的脚本。
如果你想自己做,最好的办法是编写一个脚本(例如在 Python 中)来检索文件列表并删除早于某个日期的文件。
示例:GitHub - jordansissel/s3cleaner:Amazon S3 文件清理器 - 删除超过特定年龄的内容,匹配模式等。
作为shell脚本来做有点麻烦。
谢谢,约翰
我们稍微修改了代码,它运行良好。
aws s3 ls BUCKETNAME/ | while read -r line;
do
createDate=`echo $line|awk {'print $1" "$2'}`
createDate=`date -d"$createDate" +%s`
olderThan=`date --date "7 days ago" +%s`
if [[ $createDate -lt $olderThan ]]
then
fileName=`echo $line|awk {'print $4'}`
if [[ $fileName != "" ]]
then
aws s3 rm BUCKETNAME/$fileName
fi
fi
done;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16046 次 |
| 最近记录: |