为eastus2存储容器创建告警规则时遇API版本错误求助
问题分析与解决方案
错误核心原因
你的错误提示明确指出:后台使用了不被支持的API版本2018-01-01访问storageAccounts/blobServices资源类型,尽管eastus2区域是支持该资源类型的。你实际调用时指定的2018-03-01未被正确使用,大概率是以下两个原因:
- SDK版本过低:你使用的Azure Monitor Java SDK版本默认绑定了旧版API(2018-01-01),无法正确识别你期望的API版本。
- Scope路径问题:代码中指定的监控Scope指向了存储容器,但旧版SDK不支持直接对容器创建告警,导致内部降级调用了不兼容的API版本。
具体解决步骤
1. 升级Azure Management Monitor SDK版本
确保你的项目依赖的是较新版本的Azure Monitor SDK,比如使用Maven引入最新稳定版:
<dependency> <groupId>com.azure.resourcemanager</groupId> <artifactId>azure-resourcemanager-monitor</artifactId> <version>2.30.0</version> <!-- 替换为最新稳定版 --> </dependency>
新版本SDK会默认使用支持的API版本(如错误提示中列出的2018-03-01-preview及以上),避免内部调用旧版API。
2. 修正监控Scope路径
如果你的告警是针对存储容器级别的指标,确认Scope路径格式正确(注意containers后需要加/):
String containerScope = "/subscriptions/" + subscriptionId + "/resourceGroups/" + storageResourceGroup + "/providers/Microsoft.Storage/storageAccounts/" + accountName + "/blobServices/default/containers/" + dto.getMetricOf();
如果告警是针对Blob服务级别的指标(如容器总数、Blob总数等),则Scope应指向blobServices/default:
String blobServiceScope = "/subscriptions/" + subscriptionId + "/resourceGroups/" + storageResourceGroup + "/providers/Microsoft.Storage/storageAccounts/" + accountName + "/blobServices/default";
3. 强制指定API版本(可选)
如果升级SDK后仍存在API版本不匹配问题,可以手动指定MetricAlertsClient使用的API版本:
// 创建自定义API版本的MetricAlertsClient MetricAlertsClient metricAlertsClient = new MetricAlertsClientBuilder() .credential(azureCredentials) .subscriptionId(subscriptionId) .apiVersion("2018-03-01-preview") // 使用错误提示中支持的版本 .buildClient(); // 使用该客户端创建告警 Response<MetricAlertResourceInner> response = metricAlertsClient.createOrUpdateWithResponse( storageResourceGroup, dto.getName(), new MetricAlertResourceInner() .withLocation(region) .withDescription(dto.getDescription()) .withSeverity(3) .withEnabled(true) .withScopes(List.of(containerScope)) .withEvaluationFrequency(Duration.parse(dto.getEvaluationFrequency())) .withWindowSize(Duration.parse(dto.getWindowSize())) .withCriteria(new MetricAlertSingleResourceMultipleMetricCriteria().withAllOf( List.of(new MetricCriteria().withName(dto.getName()) .withMetricName(dto.getMetricName()) .withTimeAggregation(AggregationTypeEnum.fromString(dto.getTimeAggregation())) .withOperator(operator) .withThreshold(dto.getAlarmConditionThreshold())))) .withActions(azureActionsService.handleActions(subscriptionId, storageResourceGroup, dto.getTopicName(), region, dto.getSendTo(), azureCredentials)), Context.NONE);
验证步骤
- 确认存储账户的资源提供商
Microsoft.Storage已在eastus2区域注册(通常默认已注册,可通过Azure Portal或CLI验证)。 - 检查要监控的指标是否确实存在于目标Scope级别(容器或Blob服务)。
内容的提问来源于stack exchange,提问作者gkhand
相关产品推荐
相关产品推荐

