我正在 bash 中运行以下命令,列出所有与特定模式匹配的谷歌云存储桶,并删除除最后创建的一个之外的所有存储桶。运行以下命令时,我收到错误: 错误:(gcloud.storage.rm) 以下 URL 未匹配任何对象或文件:gs://bucketname/
实际上这个存储桶确实存在,并且对于定义的循环,它正在删除循环中的最后一个条目。(我不应该使用gsutil,所以我使用gcloud storage命令)
#!/bin/bash
currentbucket_name=$(gcloud storage ls -b | sort -k2 -r | head -n 1);
echo $currentbucket_name
# List all the previously created buckets
all_buckets=$(gcloud storage ls -b gs://location-environmentname*);
# Iterate through the buckets and delete all except the current latest bucket
echo "$all_buckets" | while IFS= read -r older_bucket; do
if [ "$older_bucket" != "gs://${currentbucket_name}/" ]; then
gcloud storage rm -r "$older_bucket" --no-user-output-enabled && echo "Deleted the Bucket: $older_bucket" || echo "Failed to delete Bucket: $older_bucket.";
fi
done;
你可以尝试这样的事情并避免变量
gcloud storage ls -b gs://location-environmentname* | \
grep -v "gs://${currentbucket_name}/" | \
xargs -I '{}' -L1 gcloud storage rm -r '{}' --no-user-output-enabled