How to get billing information programmatically fr

2019-09-19 10:43发布

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条回答
2楼-- · 2019-09-19 11:31

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 :).

查看更多
登录 后发表回答