You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用OAuth与Azure SDK for C++判断分层命名空间是否启用

如何通过OAuth方式判断Azure存储账户的分层命名空间是否启用?

GetAccountInfo操作确实不支持OAuth授权,这一点从你提供的单元测试注释也能得到验证。要通过OAuth方式判断分层命名空间状态,可以改用Azure Resource Manager (ARM) API,该API完全支持OAuth授权,且能直接获取存储账户的分层命名空间属性。

具体实现方案

调用ARM存储账户详情接口

通过ARM API查询存储账户的完整属性,其中包含分层命名空间的启用状态:

  • 请求方法:GET
  • 请求路径:/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Storage/storageAccounts/{accountName}?api-version=2023-01-01
  • 响应中properties.isHnsEnabled字段的值即为状态(true=启用,false=未启用)

C++代码示例(基于Azure SDK)

#include <azure/identity.hpp>
#include <azure/resourcemanager/storage.hpp>
#include <iostream>

int main()
{
    std::string subscriptionId = "你的订阅ID";
    std::string resourceGroupName = "你的资源组名称";
    std::string accountName = "你的存储账户名称";

    // 使用默认凭据获取OAuth令牌(适配本地开发、Azure虚拟机等多种场景)
    auto credential = std::make_shared<Azure::Identity::DefaultAzureCredential>();
    auto storageAccountClient = Azure::ResourceManager::Storage::StorageAccountsClient(
        subscriptionId, credential);

    // 获取存储账户详情
    auto accountResponse = storageAccountClient.Get(resourceGroupName, accountName);
    auto& accountProperties = accountResponse.Value.Properties;

    // 提取分层命名空间状态
    bool isHnsEnabled = accountProperties.IsHnsEnabled.HasValue() 
        ? *accountProperties.IsHnsEnabled 
        : false;

    std::cout << "分层命名空间启用状态:" << (isHnsEnabled ? "已启用" : "未启用") << std::endl;

    return 0;
}

权限要求

确保你的OAuth身份(用户或服务主体)拥有Microsoft.Storage/storageAccounts/read权限,可通过Azure RBAC为其分配存储账户读取者角色。

内容的提问来源于stack exchange,提问作者John Doe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 09:51:04