Google云计费API,获取当月费用

lim*_*ack 4 google-api google-cloud-platform google-kubernetes-engine google-cloud-billing

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

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

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

我已经查看了这篇文章中的答案,但无法真正弄清楚如何使用它们。

mar*_*doi 6

官方文档:

\n\n

开始使用 Cloud Billing Budget API

\n\n

Cloud Billing 预算 API 设置

\n\n

使用 Cloud Billing Budget API

\n\n

云计费预算客户端库

\n\n
\n

使用 Cloud Billing Budget API 可以:

\n\n

为您的每个 Google Cloud 项目制定单独的预算,以便您\n 了解 Google Cloud 环境的哪些领域的支出\n 超出预期。

\n\n

在季度财务规划后批量更新您的所有预算。

\n\n

与您公司的部署管理器集成,将预算创建添加到您的云配置工作流程中。

\n
\n\n
    \n
  1. 创建一个项目
  2. \n
  3. 启用计费
  4. \n
  5. 启用 API(Cloud Budget API、Cloud Billing Budget API)
  6. \n
  7. 创建预算,在预算和警报上,创建预算、范围、金额(\n上个月\xe2\x80\x99s 支出)、操作
  8. \n
  9. 设置身份验证(创建服务帐户,授予服务帐户计费帐户管理员角色,下载 key.json,设置环境变量 export GOOGLE_APPLICATION_CREDENTIALS="[PATH]"
  10. \n
  11. 使用客户端库Client for Cloud Billing Budget API

    \n\n
    from google.cloud import billing_budgets_v1beta1\nclient = billing_budgets_v1beta1.BudgetServiceClient()\n\n#get all the budgets\nparent = client.billing_account_path([BILLING_ACCOUNT])\nfor element in client.list_budgets(parent):\n   print(element)\n   pass\n\n#for a specific budget\nname = client.budget_path(\'[BILLING_ACCOUNT]\', \'[BUDGET]\')\nresponse = client.get_budget(name)\n\n#The output should be in the form \n
    Run Code Online (Sandbox Code Playgroud)\n\n
    display_name: "billing-budget"\nbudget_filter {\nprojects: "projects/"\ncredit_types_treatment: \n}\namount {\nlast_period_amount {\n}\n}\nthreshold_rules {\nthreshold_percent: 0.5\nspend_basis: CURRENT_SPEND\n}\nthreshold_rules {\nthreshold_percent: 0.9\nspend_basis: CURRENT_SPEND\n}\nthreshold_rules {\nthreshold_percent: 1.0\nspend_basis: CURRENT_SPEND\n}\nall_updates_rule {\n}\netag: "" ```\n
    Run Code Online (Sandbox Code Playgroud)
  12. \n
\n\n

编辑:

\n\n

我检查了gcloud alpha billing命令,只能看到选项:

\n\n
    1. accounts(describe, get-iam-policy, list, projects(describe, link, list, unlink))\n\n    2. budgets(create, delete, describe, list, update) \n\n    3. projects(describe, link, list, unlink). These are the API that you can call. \n
Run Code Online (Sandbox Code Playgroud)\n\n

没有 API 可以让我了解当前的支出情况。您可以将账单导出到 BigQuery 或文件(已弃用)并进行分析。

\n

  • 虽然它确实帮助我更好地理解计费 API,但它并没有解决我的问题。因此,我认为接受答案会产生误导。 (4认同)