“系统找不到指定的文件。”发布到 Azure Function 时从 Blob 容器读取时出错

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

我们面临的问题是,当我们尝试从存储帐户读取文件时,它返回错误“系统找不到指定的文件”。这仅在我们将函数发布到 Azure Function App 后才会发生。我们已授予托管身份最大权限,并且功能应用程序可以读取文件名,因此我们认为这不是问题。

我们用来访问文件的代码:

CloudStorageAccount storageAccount = CloudStorageAccount.Parse(_StorageKey);
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference(containerName);
CloudBlockBlob blockBlobReference = container.GetBlockBlobReference(filename);

我们本地没有保存文件

2020 年有一篇关于此问题的几乎准确的帖子,但没有发布答案,所以希望这个碰撞能有所帮助。

如果您还有任何其他问题,请告诉我,我会提供更多详细信息。任何想法都会很棒!感谢您的阅读:)

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

下面是对我有用的代码

函数1.cs:

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;


namespace FunctionApp158
{
    public class Function1
    {
        private readonly ILogger<Function1> _logger;

        public Function1(ILogger<Function1> logger)
        {
            _logger = logger;
        }

        [Function("Function1")]
        public IActionResult Run([HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequest req)
        {
            _logger.LogInformation("Hello Rithwik Bojja!!");
            var rith_con_name = "rithwik";
            var rith_blob_name = "test11.json";
            CloudStorageAccount rith_str = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=rith54;AccountKey=vejgTwCE1RQ==;EndpointSuffix=core.windows.net");
            CloudBlobClient rith_bc = rith_str.CreateCloudBlobClient();
            CloudBlobContainer rithc = rith_bc.GetContainerReference(rith_con_name);
            CloudBlockBlob rithbbr = rithc.GetBlockBlobReference(rith_blob_name);
            string rith_file_content = rithbbr.DownloadTextAsync().Result;
            Console.WriteLine(rith_file_content);
            return new OkObjectResult(rith_file_content);
        }
    }
}

必须提供带扩展名的文件名。

csproj:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <AzureFunctionsVersion>v4</AzureFunctionsVersion>
    <OutputType>Exe</OutputType>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>
  <ItemGroup>
    <FrameworkReference Include="Microsoft.AspNetCore.App" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.20.1" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.1.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore" Version="1.2.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.16.4" />
    <PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.21.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="1.1.0" />
    <PackageReference Include="WindowsAzure.Storage" Version="9.3.3" />
  </ItemGroup>
  <ItemGroup>
    <None Update="host.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="local.settings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </None>
  </ItemGroup>
  <ItemGroup>
    <Using Include="System.Threading.ExecutionContext" Alias="ExecutionContext" />
  </ItemGroup>
</Project>

门户中的文件/Blob:

enter image description here

本地输出:

enter image description here

enter image description here

Portal输出:

enter image description here

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