gsutil 命令删除前一天的旧文件

spa*_*ala 5 bucket google-cloud-storage gsutil

我在谷歌云存储中有一个存储桶。我在存储桶中有一个 tmp 文件夹。每天在此目录中创建数以千计的文件。我想每天晚上删除超过 1 天的文件。我在 gsutil 上找不到这个工作的论据。我不得不使用一个经典而简单的 shell 脚本来做到这一点。但是文件删除速度非常慢。

我在文件夹中积累了 650K 个文件。必须删除其中的 540K。但是我自己的 shell 脚本工作了 1 天,只能删除 34K 文件。

gsutil 生命周期功能无法完全满足我的要求。他正在清理整个桶。我只想定期删除某个文件夹底部的文件..同时我想更快地进行删除。

我愿意接受您的建议和帮助。我可以使用单个 gsutil 命令执行此操作吗?或不同的方法?

我为测试创建的简单脚本(我准备临时删除批量文件。)

    ## step 1 - I pull the files together with the date format and save them to the file list1.txt.
gsutil -m ls -la gs://mygooglecloudstorage/tmp/ | awk '{print $2,$3}' > /tmp/gsutil-tmp-files/list1.txt


## step 2 - I filter the information saved in the file list1.txt. Based on the current date, I save the old dated files to file list2.txt.
cat /tmp/gsutil-tmp-files/list1.txt | awk -F "T" '{print $1,$2,$3}' | awk '{print $1,$3}' | awk -F "#" '{print $1}' |grep -v `date +%F` |sort -bnr > /tmp/gsutil-tmp-files/list2.txt


## step 3 - After the above process, I add the gsutil delete command to the first line and convert it into a shell script.
cat /tmp/gsutil-tmp-files/list2.txt | awk '{$1 = "/root/google-cloud-sdk/bin/gsutil -m rm -r "; print}' > /tmp/gsutil-tmp-files/remove-old-files.sh


## step 4 - I'm set the script permissions and delete old lists.
chmod 755 /tmp/gsutil-tmp-files/remove-old-files.sh
rm -rf /tmp/gsutil-tmp-files/list1.txt /tmp/gsutil-tmp-files/list2.txt


## step 5 - I run the shell script and I destroy it after it is done.
/bin/sh /tmp/gsutil-tmp-files/remove-old-files.sh
rm -rf /tmp/gsutil-tmp-files/remove-old-files.sh
Run Code Online (Sandbox Code Playgroud)

r1t*_*l1n 10

有一种非常简单的方法可以做到这一点,例如:

gsutil -m ls -l gs://bucket-name/ | grep 2017-06-23 | grep .jpg  | awk '{print $3}' | gsutil -m rm -I
Run Code Online (Sandbox Code Playgroud)