如何以编程方式从Google Cloud Platform获取账单信息? [英] How to get billing information programmatically from Google Cloud Platform?
问题描述
我想要写实用程序,该实用程序可以动态获取项目的账单信息,但是我无法从GCP中找到任何特定的API来执行此操作. 我尝试了一些API,例如getBillingInfo,但这些API仅提供有关帐单帐户的信息,而不是价格. 他们具有将计费数据导出到文件的机制,但我想以编程方式进行.有没有做同样的API?
I want write utility which fetches billing information for my project dynamically but I am not able to find any specific API from GCP to do the same. I tried a couple of APIs like getBillingInfo but these APIs just give information about billing account, not the pricing. They have the mechanism to export billing data to file but I want to do it programmatically. Is there any API to do the same?
推荐答案
有两种方法可从Google Cloud Platform获取计费数据:
There are two ways to get the billing data from Google Cloud Platform:
- 将帐单数据导出到BigQuery
- 将帐单数据导出到文件
对于BigQuery:
- 启用将帐单导出到BigQuery
- 创建BigQuery客户端
- 运行查询以获取帐单数据
查询以获取每日费用:
"SELECT SUM(cost) AS cost FROM `"+ PROJECT_ID + "." + DATA_SET + "." + TABLE_NAME + "`" + "WHERE CAST(DATE(_PARTITIONTIME) AS DATE) = DATE_SUB(CURRENT_DATE(), INTERVAL 1 DAY)"
对于文件:
- 启用计费导出到文件
- 编写代码以从存储桶中下载对象
- 遍历下载的文件并执行"cost"属性的总和
希望这会有所帮助:).
Hope this helps :).
这篇关于如何以编程方式从Google Cloud Platform获取账单信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!