如何在Google Cloud Platform中获取项目下存储桶的总容量(TB)?(适配Google Cloud SDK 204.0.0旧版本)
Got it, since you're stuck on Google Cloud SDK 204.0.0 and can't upgrade, here's how you can get the storage bucket sizes you need using tools available in that older version:
1. 查询单个存储桶的总容量
The gsutil du command is fully supported in SDK 204.0.0 and works perfectly for this task:
- First, run this command to get the total size of your bucket in bytes:
You'll get output likegsutil du -s gs://your-bucket-name1234567890123 gs://your-bucket-name, where the first number is the total bytes used. - To convert this directly to TB (binary-based, 1TB = 1024⁴ bytes), use this one-liner:
If you prefer decimal-based TB (1TB = 1000⁴ bytes), replace the denominator withgsutil du -s gs://your-bucket-name | awk '{print $1 / 1099511627776 " TB"}'1000000000000.
2. 获取所有存储桶的总容量(以TB为单位)
Older SDK versions don't have a built-in command to sum bucket sizes, but a simple bash script will do the trick. Here's how:
- Create a script file (e.g.,
total_bucket_size.sh) with this content:#!/bin/bash total_bytes=0 # Loop through all your buckets for bucket in $(gsutil ls); do # Extract the byte count for each bucket bucket_size=$(gsutil du -s "$bucket" | awk '{print $1}') # Add to total total_bytes=$((total_bytes + bucket_size)) done # Convert total bytes to binary TB (with 2 decimal places) total_tb=$(echo "scale=2; $total_bytes / 1099511627776" | bc) echo "Total storage across all buckets: $total_tb TB" - Make the script executable:
chmod +x total_bucket_size.sh - Run it to get your total capacity:
./total_bucket_size.sh
Note: If you have buckets with a huge number of objects, gsutil du -s might take a minute to run, but it's reliable for older SDK versions. Just ensure you have the storage.objects.list and storage.objects.get permissions on your project.
内容的提问来源于stack exchange,提问作者user15110035
相关产品推荐
相关产品推荐

