You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何查询GCS中50天以上对象数量以估算存储类迁移成本?

查询GCS存储桶中50天以上对象数量的gcloud命令

你可以通过gcloud storage工具结合文本处理工具,统计存储桶中创建时间超过50天的对象数量,以下是具体实现方式:

单存储桶查询命令

gcloud storage ls gs://[你的存储桶名称] --recursive --format="value(timeCreated)" | awk -v cutoff=$(date -d "50 days ago" +%s) '{split($0,a,"T"); split(a[1],b,"-"); split(a[2],c,":"); epoch=mktime(b[1]" "b[2]" "b[3]" "c[1]" "c[2]" "c[3]); if(epoch < cutoff) count++} END {print count}'

命令拆解

  • gcloud storage ls gs://[你的存储桶名称] --recursive:递归列出目标存储桶内的所有对象
  • --format="value(timeCreated)":仅输出对象的创建时间字段,减少冗余数据
  • awk逻辑:将当前时间往前推50天转换为Unix时间戳,逐一对比每个对象的创建时间戳,统计符合条件的对象总数

多存储桶批量查询脚本

如果需要同时统计多个存储桶,可使用以下Shell脚本循环处理:

#!/bin/bash
# 替换为你的存储桶列表
BUCKETS=("bucket-name-1" "bucket-name-2" "bucket-name-3")
# 计算50天前的Unix时间戳
CUTOFF=$(date -d "50 days ago" +%s)

for BUCKET in "${BUCKETS[@]}"
do
  echo "存储桶 gs://$BUCKET 中50天以上的对象数量:"
  gcloud storage ls gs://$BUCKET --recursive --format="value(timeCreated)" | awk -v cutoff=$CUTOFF '{split($0,a,"T"); split(a[1],b,"-"); split(a[2],c,":"); epoch=mktime(b[1]" "b[2]" "b[3]" "c[1]" "c[2]" "c[3]); if(epoch < cutoff) count++} END {print count}'
done

替代方案(使用gsutil)

若习惯使用gsutil工具,也可执行以下命令:

gsutil ls -l gs://[你的存储桶名称] | awk -v cutoff=$(date -d "50 days ago" +%Y-%m-%d) '{if($1 ~ /^[0-9]/ && $2 < cutoff) count++} END {print count}'

注意事项

  • 若存储桶内对象数量极大(千万级及以上),遍历统计会耗时较长,建议先在测试桶验证逻辑,或分目录分批统计
  • 确保本地环境已安装并配置好gcloud/gsutil工具,且拥有目标存储桶的读取权限

内容的提问来源于stack exchange,提问作者Krish

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 19:42:35