google-cloud-platform - 谷歌云计费API,抓取当月费用

标签 google-cloud-platform google-api google-kubernetes-engine google-cloud-billing

我目前正在使用 Flask 为在 GKE 上运行的在线服务构建管理仪表板。

我想展示自本月初以来该服务的账单有多大。我已经研究了 Google Cloud billing API,但它是一个血腥的丛林,我似乎找不到任何方法来检索我想要的数据。

我认为这应该是可能的,但我不知道如何。
任何对 Google Cloud API 更有经验的人都可以帮助我,最好是使用 python 代码片段吗?

我看过 this post 中的答案但无法真正弄清楚如何使用它们。

最佳答案

官方文档:

Get started with the Cloud Billing Budget API

Cloud Billing Budget API Setup

Using the Cloud Billing Budget API

Cloud Billing Budget Client Libraries

Use the Cloud Billing Budget API to:

Create a separate budget for each of your Google Cloud projects so you know which areas of your Google Cloud environment are spending more than expected.

Bulk update all of your budgets after quarterly financial planning.

Integrate with your company's deployment manager to add the creation of budgets to your cloud provisioning workflow.


  • 创建项目
  • 启用计费
  • 启用 API(Cloud Budget API、Cloud Billing Budget API)
  • 创建预算,在预算和警报上,创建预算、范围、金额(
    上个月的支出),操作
  • 设置身份验证(创建服务帐户,授予服务帐户计费帐户管理员角色,下载 key.json,设置环境变量 export GOOGLE_APPLICATION_CREDENTIALS="[PATH]"
  • 使用客户端库 Client for Cloud Billing Budget API
    from google.cloud import billing_budgets_v1beta1
    client = billing_budgets_v1beta1.BudgetServiceClient()
    
    #get all the budgets
    parent = client.billing_account_path([BILLING_ACCOUNT])
    for element in client.list_budgets(parent):
       print(element)
       pass
    
    #for a specific budget
    name = client.budget_path('[BILLING_ACCOUNT]', '[BUDGET]')
    response = client.get_budget(name)
    
    #The output should be in the form 
    
    display_name: "billing-budget"
    budget_filter {
    projects: "projects/"
    credit_types_treatment: 
    }
    amount {
    last_period_amount {
    }
    }
    threshold_rules {
    threshold_percent: 0.5
    spend_basis: CURRENT_SPEND
    }
    threshold_rules {
    threshold_percent: 0.9
    spend_basis: CURRENT_SPEND
    }
    threshold_rules {
    threshold_percent: 1.0
    spend_basis: CURRENT_SPEND
    }
    all_updates_rule {
    }
    etag: "" ```
    

  • 编辑:

    我查了 gcloud alpha billing命令,我只能看到选项:
        1. accounts(describe, get-iam-policy, list, projects(describe, link, list, unlink))
    
        2. budgets(create, delete, describe, list, update) 
    
        3. projects(describe, link, list, unlink). These are the API that you can call. 
    

    根据我的理解,没有用于获取当前支出的 API。您可以将帐单导出到 BigQuery 或文件(已弃用)并进行分析。

    关于google-cloud-platform - 谷歌云计费API,抓取当月费用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60148004/

    相关文章:

    javascript - GAPI 未定义

    android - Firebase 应用内消息传递 - Android 中的 PERMISSION_DENIED(请求被阻止的问题)

    c# - 使用 .Net 列出 Google Drive V2 中的所有文件和文件夹

    python - Google App Engine - YouTube Python 客户端库错误

    android - 使用 Google API 的应用程序 :10

    kubernetes - GKE 中与 kubernetes 的 TLS 握手超时

    jenkins - Kubernetes 上 Jenkins 从属设备的连接已断开

    nginx - 增加 proxy_send_timeout 和 proxy_read_timeout ingress nginx

    mysql - 安装 MySQL 后无法通过 SSH 连接到 Google Cloud VM

    python - google-cloud 使用 python api 获取实例 ID 和区域