我试图使用Azure Monitoring API获取大小细节或已用容量,但它返回了空白的json。
import requests
from datetime import datetime, timedelta
from azure.identity import ClientSecretCredential
#start_time = (datetime.utcnow() - timedelta(hours=1)).replace(microsecond=0).isoformat() + "Z"
#end_time = datetime.utcnow().replace(microsecond=0).isoformat() + "Z"
start_time = (datetime.utcnow() - timedelta(hours=23)).replace(microsecond=0).isoformat() + "Z"
end_time = (datetime.utcnow() - timedelta(hours=22)).replace(microsecond=0).isoformat() + "Z"
#print("Start_Time:",start_time)
#print("End_Time:",end_time)
subscription_id = ""
resource_group = ""
storage_account_name = ""
metric_name = "UsedCapacity"
time_range = f"{start_time}/{end_time}"
api_version = "2021-05-01"
tenant_id = dbutils.secrets.get(scope="sn_scope",key="tenant-id")
client_id = dbutils.secrets.get(scope="sn_scope", key="clientid")
client_secret = dbutils.secrets.get(scope="sn_scope", key="secret")
token_endpoint = f"https://login.microsoftonline.com/{tenant_id}/oauth2/token"
data = {
"grant_type": "client_credentials",
"client_id": f"{client_id}",
"client_secret": f"{client_secret}",
"resource": "https://management.azure.com/"
}
response = requests.post(token_endpoint, data=data)
if response.status_code == 200:
post_response = response.text
print("POST_Response:",post_response)
access_token = response.json()["access_token"]
#print("Access Token:", access_token)
else:
print("Error generating access token:", response.status_code)
headers = {
"Authorization": f"Bearer {access_token}",
"Content-Type" : "application/json"
}
api_url = f"https://management.azure.com/subscriptions/{subscription_id}/resourceGroups/{resource_group}/providers/Microsoft.Storage/storageAccounts/{storage_account_name}/providers/microsoft.insights/metrics?timespan={time_range}&metricnames={metric_name}&api-version={api_version}"
print("API:",api_url)
response = requests.get(api_url,headers=headers)
if response.status_code == 200:
data = response.text
print("GET_Response:",data)
else:
print("Error retrieving metrics data:", response.status_code)
print("Error Message:",response.text)
我从这个API得到的响应是200,但json文件只返回{“value”:[] }
是否有其他API可以获取存储账户的大小?
1条答案
按热度按时间6ss1mwsb1#
如何使用Python从Azure中的存储帐户获取大小详细信息。
我在我的环境中尝试了一下,得到了以下结果:
最初,我创建了应用程序来获取Client-id,tenant-id和Client-secret。
为订阅级下的**
appvenkat
分配了contributor
**角色。我试着用同样的代码进行最小的修改,我可以使用Python获取存储帐户的已使用容量。
代码:
输出: