【发布时间】:2018-05-21 13:39:34
【问题描述】:
我在谷歌云存储中有一个存储桶。我在存储桶中有一个 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
【问题讨论】:
-
我通过使用 gcsfuse 工具安装我的存储桶解决了这个问题。现在我可以像管理本地磁盘一样管理我的存储桶了。但是在磁盘上做很多操作仍然很慢。不过,我现在可以快速清除它。有关 gcsfuse 的详细信息; cloud.google.com/storage/docs/gcs-fuse 但我认为谷歌需要自动解决这些需求。有类似问题的可以使用此方法。我对那些在这方面有更好方法的人的新想法持开放态度。
标签: google-cloud-storage bucket gsutil