简体   繁体   English

使用托管服务身份对Azure功能进行授权以从Azure存储容器中获取Blob

[英]Authorization for Azure Function using Managed Service Identity to fetch blob from Azure Storage container

When I attempt to invoke an Azure Function in an Azure Function App using a system assigned managed identity to fetch a blob from an Azure Storage container, I'm encountering: 当我尝试使用系统分配的托管身份在Azure Function应用程序中调用Azure Function从Azure存储容器中获取Blob时,遇到了:

System.Private.CoreLib: Exception while executing function:<FunctionName>. Microsoft.WindowsAzure.Storage: Unauthorized.

I'm adapting the approach outlined here . 我正在调整此处概述的方法。

Here's the code: 这是代码:

[FunctionName("TestFetchTileViaSvcPrinId")]
public static async Task<HttpResponseMessage> RunAsync(
    [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
    ILogger log) {
    log.LogInformation("C# HTTP trigger function processed a request.");

    const string blobName = "https://<storageaccount>.blob.core.windows.net/...path.../<file>.jpg";

    // Get the initial access token and the interval at which to refresh it.
    var azureServiceTokenProvider = new AzureServiceTokenProvider();
    NewTokenAndFrequency tokenAndFrequency = TokenRenewerAsync(azureServiceTokenProvider, CancellationToken.None).GetAwaiter().GetResult();

    // Create storage credentials using the initial token, and connect the callback function to renew the token just before it expires
    var tokenCredential = new TokenCredential(tokenAndFrequency.Token, TokenRenewerAsync, azureServiceTokenProvider, tokenAndFrequency.Frequency.Value);

    var storageCredentials = new StorageCredentials(tokenCredential);

    var cloudBlockBlob = new CloudBlockBlob(new Uri(blobName), storageCredentials);

    using (var memoryStream = new MemoryStream()) {
        await cloudBlockBlob.DownloadToStreamAsync(memoryStream);  // Unauthorized exception is thrown here
        var httpResponseMessage = new HttpResponseMessage(HttpStatusCode.OK) {
            Content = new ByteArrayContent(memoryStream.ToArray())
        };
        httpResponseMessage.Headers.Add("Cache-Control", "max-age=31536000"); //31536000 seconds ~ 1 year
        httpResponseMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");
        return httpResponseMessage;
    }

}

The Azure Function App has a system assigned managed identity which has Storage Blob Data Contributor role for the target blob's entire storage account. Azure功能应用程序具有系统分配的托管身份,该身份具有目标Blob的整个存储帐户的Storage Blob数据贡献者角色。

I got this working. 我工作了。 As Rohit noticed, the redacted full-path to the blob (as originally posted) incorrectly specified the Azure function path rather than the storage account path. 正如Rohit所注意到的那样,已删除的Blob完整路径(如最初发布的那样)错误地指定了Azure函数路径而不是存储帐户路径。 I've subsequently fixed up the question. 我后来解决了这个问题。 Nevertheless, I did have a typo in the path as implemented. 不过,在实施过程中我确实有错别字。 Correcting the path resolved the issue. 更正路径可以解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 允许从托管服务身份到Azure功能的无密钥授权 - Allow Keyless Authorization from Managed Service Identity to Azure Function 使用托管服务标识从Data Factory调用Azure函数 - Call Azure Function from Data Factory using Managed Service Identity 使用 SDK azblob 和托管服务标识从 Azure VM 将文件上传到 Azure 存储 - Upload files to Azure Storage from Azure VM using SDK azblob and Managed Service Identity Azure PHP web 应用程序使用系统分配的托管身份连接到 Azure 存储 Blob - Azure PHP web app using system assigned managed identity connecting to Azure Storage Blob Azure Blob 存储授权 - Azure Blob Storage Authorization 使用 Service Principle 或 Managed Identity 从 ADF 验证 Azure 表存储 - Authenticate Azure Table storage from ADF using Service Principle or Managed Identity 使用托管标识从 Azure 逻辑应用到 Azure Function 进行身份验证 - Authenticate from Azure Logic app to Azure Function using Managed Identity 使用托管标识从 VM 访问 Azure 存储 - Accessing Azure Storage from VM using Managed Identity Azure 媒体服务从私有 blob 存储容器提交 JobInputHttp - Azure Media Service Submit JobInputHttp from a private blob storage container 使用来自容器实例的托管标识连接 Azure SQL 数据库 - Connecting Azure SQL database using managed identity from container instance
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM