【问题标题】:Azur Functions is not authorized to list blobs from afolder in a container in Data LakeAzur Functions 无权列出 Data Lake 容器中文件夹中的 blob
【发布时间】:2021-04-19 13:42:33
【问题描述】:

我想在 Azure Functions 中列出 Azure Data Lake 中容器的文件夹中的 blob。

为了进行身份验证,我想使用系统分配的 Azure Functions 托管标识。我已经在我的 azure Functions 中激活了它,并且在 Data Lake 端赋予它 Storage Blob Data Contributor 角色。

这是我的代码:

 string dfsUri = "https://<myDataLake>.dfs.core.windows.net";
 DataLakeClientOptions options = new DataLakeClientOptions(DataLakeClientOptions.ServiceVersion.V2019_07_07);             
 DataLakeServiceClient dataLakeServiceClient = new DataLakeServiceClient(new Uri(dfsUri), new Azure.Identity.DefaultAzureCredential(),options);
 DataLakeFileSystemClient dataLakeFileSystemClient = dataLakeServiceClient.GetFileSystemClient("my-file-system");
 IAsyncEnumerator<PathItem> enumerator = dataLakeFileSystemClient.GetPathsAsync("testfolder").GetAsyncEnumerator();
  await enumerator.MoveNextAsync();
  PathItem item = enumerator.Current;
  while (item != null)
  {
    log.LogInformation($"File Name {item.Name}.");

       if (!await enumerator.MoveNextAsync())
          {
             break;
          }

            item = enumerator.Current;
  }

如果我运行我的代码,我会收到以下错误消息:

This request is not authorized to perform this operation using this permission.
RequestId:cd00a570-401f-0024-4d21-35badb000000
Time:2021-04-19T13:39:30.8429070Z
Status: 403 (This request is not authorized to perform this operation using this permission.)
ErrorCode: AuthorizationPermissionMismatch

Headers:
Server: Windows-Azure-HDFS/1.0,Microsoft-HTTPAPI/2.0
x-ms-error-code: AuthorizationPermissionMismatch
x-ms-request-id: cd00a570-401f-0024-4d21-35badb000000
x-ms-version: 2019-07-07
x-ms-client-request-id: b0510f6a-5798-476c-a95e-6f206bf2a9cc
Date: Mon, 19 Apr 2021 13:39:29 GMT
Content-Length: 227
Content-Type: application/json; charset=utf-8

但在容器级别,以下代码对我来说可以正常工作:

 string dfsUri = "https://<myDataLake>.dfs.core.windows.net";
     DataLakeClientOptions options = new DataLakeClientOptions(DataLakeClientOptions.ServiceVersion.V2019_07_07);             
     DataLakeServiceClient dataLakeServiceClient = new DataLakeServiceClient(new Uri(dfsUri), new Azure.Identity.DefaultAzureCredential(),options);
     DataLakeFileSystemClient dataLakeFileSystemClient = dataLakeServiceClient.GetFileSystemClient("my-file-system");
     IAsyncEnumerator<PathItem> enumerator = dataLakeFileSystemClient.GetPathsAsync("").GetAsyncEnumerator();
      await enumerator.MoveNextAsync();
      PathItem item = enumerator.Current;
      while (item != null)
      {
        log.LogInformation($"File Name {item.Name}.");
    
           if (!await enumerator.MoveNextAsync())
              {
                 break;
              }
    
                item = enumerator.Current;
      }

谁能告诉我应该怎么做才能列出容器中文件夹中的 blob?

【问题讨论】:

  • 你给了正确的RBAC,所以可能有两个原因导致这个问题:1、DefaultAzureCredential()没有得到正确的凭据,请直接使用ManagedIdentityCredential()(我之前回答的一个问题是由这个问题引起的)。 2、也许只是RBAC角色需要时间来激活。我写了一个简单的代码,你可以试试。
  • 嗨,有更新吗?

标签: azure authentication azure-functions azure-data-lake azure-managed-identity


【解决方案1】:

第 1 部分

我对用户分配的身份做了类似的事情,但两者应该相同。听起来您的配置应该是正确的,但是您可以执行以下操作来确认。在 Function App 的 Identity 部分中,单击“Azure Role Assignments”:

您看到列出的任何内容吗?如果不是,那么您当前的配置将无法识别。您可以尝试“添加角色分配”功能,这是一个非常简单的向导。

第 2 部分

同样,不完全是您的方案,但它可能会有所帮助。我最近编写了一些通过托管标识连接到 Synapse 的 Azure 函数。我使用了ManagedIdentityCredential,而不是 DefaultAzureCredential:

// Build the credentials
var clientId = SettingsHelper.Get(Settings.ManagedIdentityClientId);
var credential = new ChainedTokenCredential(new ManagedIdentityCredential(clientId), 
                                            new AzureCliCredential());

如您所见,我将托管身份客户端 ID 值存储在函数应用设置中。 ChainedTokenCredential 通过 AzureCliCredential 支持本地开发。我使用完全相同的代码连接到 Key Vault。

【讨论】:

    【解决方案2】:

    更新:

    经测试,the document description确实有问题,'Storage Blob Data Contributor'可以给予所需权限:

        var uri = new Uri("https://datalakename.dfs.core.windows.net/");
        var tokenCredential = new ManagedIdentityCredential();
        DataLakeServiceClient dataLakeServiceClient = new DataLakeServiceClient(uri,tokenCredential);
        var fileSystemClient = dataLakeServiceClient.GetFileSystemClient("test");
        Pageable<PathItem> items = fileSystemClient.GetPaths();
        foreach (var item in items) {
            Console.WriteLine(item.Name);
        }
    

    Storage Blob Data Contributor RBAC角色的功能应用身份添加到数据湖后,我仍然收到错误:

    但几分钟后,我可以毫无问题地获取 blob。我怀疑你只需要等待几分钟就可以了。

    【讨论】:

    • 我认为这是文本中的疏忽。 Owner 应包含所有 Contributor 权限,Contributor 应包含所有 Reader 权限。
    猜你喜欢
    • 1970-01-01
    • 2021-02-20
    • 1970-01-01
    • 2019-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    相关资源
    最近更新 更多