上传具有托管不确定性的 Blob 时出现授权错误

问题描述 投票:0回答:1

我正在尝试从具有托管身份的azure函数应用程序上传一个简单的blob:

public class BlobUtil
{
    static public async Task UploadBlob(string accountName, string containerName, string blobName, string blobContents)
    {
        // Construct the blob container endpoint from the arguments.
        string containerEndpoint = string.Format("https://{0}.blob.core.windows.net/{1}",
                                                    accountName,
                                                    containerName);

        // Get a credential and create a client object for the blob container.
        BlobContainerClient containerClient = new BlobContainerClient(new Uri(containerEndpoint),
                                                                        new DefaultAzureCredential());

        try
        {
            // Create the container if it does not exist.
            await containerClient.CreateIfNotExistsAsync();

            // Upload text to a new block blob.
            byte[] byteArray = Encoding.ASCII.GetBytes(blobContents);

            using (MemoryStream stream = new MemoryStream(byteArray))
            {
                await containerClient.UploadBlobAsync(blobName, stream);
            }
        }
        catch (Exception e)
        {
            throw e;
        }
    }
}

await BlobUtil.UploadBlob("stintxxxxatadev", "xxx", Guid.NewGuid().ToString()+".json", "yo bro");

我收到以下错误:

由于线程退出或 I/O 操作已中止 申请请求

还有

状态:403(此请求无权执行此操作 操作。)

访问存储帐户后已提供该功能: enter image description here

我是否缺少一些进一步的访问设置或任何有关如何解决此问题的想法?

c# azure azure-functions azure-blob-storage
1个回答
0
投票

我确实同意@Gaurav Mantri,问题在于网络,下面应该是网络的情况:

enter image description here

或者如果它是通过选择性网络启用的,那么您需要与功能应用程序和存储帐户的虚拟网络集成,并参考此SO-Thread1SO-Thread2

并且应该为 Function 应用托管身份提供 Storage Blob Contributor。

对我有用的代码:

using Microsoft.Azure.Functions.Worker;
using Azure.Storage.Blobs;
using System.Text;
using Azure.Identity;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
public class TestFunc
{
    [Function("TestFunc")]
    public async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest ri)
    {
        var ri_bsc = new BlobServiceClient(
            new Uri("https://rithwik.blob.core.windows.net"),
            new DefaultAzureCredential());
        string ri_con = "rithcont1";
        var ri_cc = ri_bsc.GetBlobContainerClient(ri_con);
        var ri_bn = "Test.txt";
        var test = "Hello Rithwik Bojja, The blob is created";
        var rbc = ri_cc.GetBlobClient(ri_bn);
        using (var rith = new MemoryStream(Encoding.UTF8.GetBytes(test)))
        {
            await rbc.UploadAsync(rith, true);
        }
        return new OkObjectResult($"Hello Rithwik Bojja, The blob  with name '{ri_bn}'is created  and uploaded plz check.");
    }
}

输出:

enter image description here

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.