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

是否有Python API可获取GCP项目中各类活跃资源及元数据?

获取GCP各类资源元数据的Python API方案

GCP每个服务都有对应的官方Python客户端库,专门用于获取该服务下的资源元数据,下面分服务说明:

计算引擎(VM实例)

使用google-cloud-compute库,可直接获取instance_id、操作系统、内存配置、机器系列等完整实例元数据。

示例代码:

from google.cloud import compute_v1

def list_vm_metadata(project_id):
    instances_client = compute_v1.InstancesClient()
    # 替换为你的项目覆盖的可用区
    zones = ["us-central1-a", "us-central1-b"]
    for zone in zones:
        instances = instances_client.list(project=project_id, zone=zone)
        for instance in instances:
            print(f"Instance ID: {instance.id}")
            # 从镜像路径提取操作系统信息
            print(f"操作系统: {instance.disks[0].initialize_params.source_image.split('/')[-1]}")
            print(f"内存配置: {instance.memory_mb} MB")
            # 从机器类型路径提取机器系列
            print(f"机器系列: {instance.machine_type.split('/')[-1].split('-')[0]}")
            print("---")

list_vm_metadata("your-project-id")

Cloud Storage(存储桶)

使用google-cloud-storage库,可获取存储桶的创建时间、存储类别、位置等元数据。

示例代码:

from google.cloud import storage

def list_bucket_metadata(project_id):
    storage_client = storage.Client(project=project_id)
    buckets = storage_client.list_buckets()
    for bucket in buckets:
        print(f"存储桶名称: {bucket.name}")
        print(f"创建时间: {bucket.time_created}")
        print(f"存储类别: {bucket.storage_class}")
        print(f"位置: {bucket.location}")
        print("---")

list_bucket_metadata("your-project-id")

BigQuery(数据集)

使用google-cloud-bigquery库,可获取数据集的位置、描述、权限以及下属表的元数据。

示例代码:

from google.cloud import bigquery

def list_dataset_metadata(project_id):
    client = bigquery.Client(project=project_id)
    datasets = client.list_datasets()
    for dataset in datasets:
        dataset_ref = client.get_dataset(dataset.dataset_id)
        print(f"数据集ID: {dataset_ref.dataset_id}")
        print(f"位置: {dataset_ref.location}")
        print(f"描述: {dataset_ref.description}")
        # 列出数据集内的表
        tables = client.list_tables(dataset_ref)
        print("包含表:")
        for table in tables:
            print(f"- {table.table_id}")
        print("---")

list_dataset_metadata("your-project-id")

Dataproc(集群)

使用google-cloud-dataproc库,可获取集群的机器配置、软件版本、状态等元数据。

示例代码:

from google.cloud import dataproc_v1

def list_dataproc_metadata(project_id, region):
    client = dataproc_v1.ClusterControllerClient(
        client_options={"api_endpoint": f"{region}-dataproc.googleapis.com:443"}
    )
    clusters = client.list_clusters(project_id=project_id, region=region)
    for cluster in clusters.clusters:
        print(f"集群名称: {cluster.cluster_name}")
        print(f"状态: {cluster.status.state.name}")
        print(f"主节点机器类型: {cluster.config.master_config.machine_type_uri.split('/')[-1]}")
        print(f"软件版本: {cluster.config.software_config.image_version}")
        print("---")

list_dataproc_metadata("your-project-id", "us-central1")

注意事项

  • 安装对应依赖库:pip install google-cloud-compute google-cloud-storage google-cloud-bigquery google-cloud-dataproc
  • 确保已完成GCP身份认证:本地开发可使用gcloud auth application-default login,生产环境建议使用服务账号密钥文件

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.28 15:17:23