问题
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 :).
来源:https://stackoverflow.com/questions/49919800/how-to-get-billing-information-programmatically-from-google-cloud-platform