gcp存储桶使用情况
读取gcp存储桶使用情况 需要调用到基础接口脚本里的数据库读写和google cloud库
from google.cloud import storage
from google.oauth2 import service_account
#替换为服务账号的密钥文件路径
SERVICE_ACCOUNT_FILE = "<xxx.json>"
#初始化客户端时使用服务账号凭据
credentials = service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE)
storage_client = storage.Client(credentials= credentials)
def list_buckets_and_objects():
"""列出所有存储桶及其对象"""
buckets = storage_client.list_buckets()
print(f"{'Bucket Name':<30}{'Total Size (GB)':>20}")
print("-" * 50)
for bucket in buckets:
total_size = 0
blobs = storage_client.list_blobs(bucket.name)
for blob in blobs:
total_size += blob.size
total_size_gb = total_size / (1024 ** 3)
print(f"{bucket.name:<30}{total_size_gb:>20.2f}")
#执行函数
list_buckets_and_objects()