如何使用Python SDK从Azure获取月度成本数据?

jc3wubiy  于 2023-06-24  发布在  Python
关注(0)|答案(1)|浏览(159)

我试图使用Azure SDK for Python从Azure获取每月的成本数据,但Microsoft文档似乎非常混乱和过时,没有示例。我需要在Azure Portal之外创建月度发展图表。
从Azure中检索每月成本信息的正确方法是什么?
我已经尝试使用BillingManagementClientclass,ConsumptionManagementClient.balances中的get_for_billing_period_by_billing_account方法,现在尝试使用ConsumptionManagementClient中的usage_details.list方法,但收到奇怪的重复数据:

consumption_client = ConsumptionManagementClient(self.credential, self.subscription_id)
start_date = "2022-11-19T00:00:00.0000000Z"
end_date = "2022-11-20T00:00:00.0000000Z"
filters = f"properties/usageStart eq '{start_date}' and properties/usageEnd eq '{end_date}'"
consumption_list = consumption_client.usage_details.list(f"/subscriptions/{subscription_id}", None, filters)

for consumption_data in consumption_list:
    print(f"date: {consumption_data.date} \nstart_date: {consumption_data.billing_period_start_date} \nend_date: {consumption_data.billing_period_end_date}\ncost: {consumption_data.cost} \n")

脚本输出:
日期:2022-11-20 00:00:00+00:00 start_date:2022-11-11 00:00:00+00:00 end_date:2022-12-10 00:00:00+00:00费用:0.658392
日期:2022-11-19 00:00:00+00:00 start_date:2022-11-11 00:00:00+00:00 end_date:2022-12-10 00:00:00+00:00费用:0.658392
日期:2022-11-19 00:00:00+00:00 start_date:2022-11-11 00:00:00+00:00 end_date:2022-12-10 00:00:00+00:00费用:0.67425593616
日期:2022-11-20 00:00:00+00:00 start_date:2022-11-11 00:00:00+00:00 end_date:2022-12-10 00:00:00+00:00费用:0.67425593616

8fq7wneg

8fq7wneg1#

一团乱
我建议您使用connect Power BI to Azure Cost Management,因为这将为您提供开箱即用的解决方案。

相关问题