How to get billing information programmatically from Google Cloud Platform?

拥有回忆 提交于 2019-12-25 18:37:12

问题


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?


回答1:


There are two ways to get the billing data from Google Cloud Platform:

  • Export Billing Data to BigQuery
  • Export Billing Data to a File

For BigQuery:

  1. Enable billing export to BigQuery
  2. Create a BigQuery Client
  3. Run a query to get the billing data

Query to get daily cost:

"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)"

For File:

  1. Enable billing export to a file
  2. Write a code to download an object from a bucket
  3. Iterate through a downloaded file and perform the summation of "cost" property

Hope this helps :).



来源:https://stackoverflow.com/questions/49919800/how-to-get-billing-information-programmatically-from-google-cloud-platform

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!