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:
- Enable billing export to BigQuery
- Create a BigQuery Client
- 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:
- Enable billing export to a file
- Write a code to download an object from a bucket
- Iterate through a downloaded file and perform the summation of "cost" property
Hope this helps :).