Blob 触发了 Azure 函数 - Blob 无法绑定到 Azure 环境中的第一个 azure 函数参数

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

我想创建一个简单的 Azure 函数,该函数在 Blob 上传时触发。

当我在本地运行该函数时,它可以正常工作。 问题是,当我使用 Visual Studio 2022 发布选项发布代码时,出现异常并且该函数根本不执行。据我了解,问题在于该函数的“Stream”参数由于某种奇怪的原因无法绑定。

这是我得到的例外:

转换函数“BlobTriggeredFunction”的 1 个输入参数时出错:无法将输入参数“stream”从类型“Microsoft.Azure.Functions.Worker.Grpc.Messages.GrpcModelBindingData”转换为类型“System.IO.Stream”。错误:System.FormatException:设置必须采用“名称=值”的形式。 在Azure.Storage.StorageConnectionString。<>c.b__67_0(字符串错误) 在Azure.Storage.StorageConnectionString.ParseStringIntoSettings(字符串连接字符串,Action

1 error) at Azure.Storage.StorageConnectionString.ParseCore(String connectionString, StorageConnectionString& accountInformation, Action
1错误) 在Azure.Storage.StorageConnectionString.Parse(字符串连接字符串) 在 Azure.Storage.Blobs.BlobServiceClient..ctor(字符串连接字符串,BlobClientOptions 选项) 在 D: _work \s xtensions\Worker.Extensions.Storage.Blobs\src\Config\BlobStorageBindingOptions.cs 中的 Microsoft.Azure.Functions.Worker.BlobStorageBindingOptions.CreateClient() 处:第 35 行 在 D: _work \s xtensions\Worker.Extensions.Storage.Blobs\src\BlobStorageConverter.cs 中的 Microsoft.Azure.Functions.Worker.BlobStorageConverter.CreateBlobContainerClient(字符串连接名称,字符串容器名称):第 253 行 在 D: _work \s xtensions\Worker.Extensions.Storage.Blobs\src\BlobStorageConverter.cs 中的 Microsoft.Azure.Functions.Worker.BlobStorageConverter.ConvertModelBindingDataAsync(类型 targetType,BlobBindingData blobData):第 118 行 在 D: _work \s xtensions\Worker.Extensions.Storage.Blobs\src\BlobStorageConverter.cs 中的 Microsoft.Azure.Functions.Worker.BlobStorageConverter.ConvertFromBindingDataAsync(ConverterContext context, ModelBindingData modelBindingData):第 63 行

这是功能代码:


public class BlobTriggeredFunction(ILogger<BlobTriggeredFunction> logger)
{
    [Function(nameof(BlobTriggeredFunction))]
    public async Task Run([BlobTrigger("email-attachments/{name}", Connection = "BlobStorageConnectionString")] Stream stream, string name)
    {
        using var blobStreamReader = new StreamReader(stream);
        var content = await blobStreamReader.ReadToEndAsync();
        logger.LogInformation($"C# Blob trigger function Processed blob\n Name: {name} \n Data: {content}");
    }
}

这是Program.cs中的设置:

var host = new HostBuilder()
    .ConfigureFunctionsWebApplication()
    .ConfigureAppConfiguration((hostContext, config) =>
    {
        config.AddJsonFile("host.json");
        if (hostContext.HostingEnvironment.IsDevelopment())
        {
            config.AddJsonFile("local.settings.json");
            config.AddUserSecrets<Program>();
        }
    })
    .ConfigureServices(services =>
    {
        services.AddApplicationInsightsTelemetryWorkerService();
        services.ConfigureFunctionsApplicationInsights();
    })
    .Build();

host.Run();

secrets.json:

{
  "BlobStorageConnectionString": "xxxxxxxxxxxxxxxxxxxxxxx",
  "BlobStorageConnectionString:blob": "https://xxxxxxxxxxxxxxx.blob.core.windows.net/",
  "BlobStorageConnectionString:queue": "https://xxxxxxxxxxxxxxx.queue.core.windows.net/"
}

我尝试将“Stream”参数更改为 byte[] 或字符串,但没有任何帮助。 我不认为问题出在连接字符串中,因为当我上传 blob 时会触发该函数,但无法处理它。 最糟糕的问题是,当我发布代码时,它只发生在Azure环境中。在当地环境下效果很好。

c# azure .net-core azure-functions
1个回答
0
投票

我已经创建了一个示例 Blob 触发器 Azure 函数,并且可以在门户中运行已部署的 Azure 函数。

代码片段:

  • 函数.cs:
public Function1(ILogger<Function1> logger)
{
    _logger = logger;
}

[Function(nameof(Function1))]
public async Task Run([BlobTrigger("sampleitems/{name}", Connection = "storageconnection")] Stream stream, string name)
{
    using var blobStreamReader = new StreamReader(stream);
    var content = await blobStreamReader.ReadToEndAsync();
    _logger.LogInformation($"C# Blob trigger function Processed blob\n Name: {name} \n Data: {content}");
}

如果“StorageAccountConnectionString”在

secrets.json
中定义为用户机密,那么您需要将用户机密添加到您的program.cs中。

.ConfigureAppConfiguration(config => { config.AddUserSecrets<YourFunctionName>(optional: true, reloadOnChange: false); })

程序.cs:

var host = new HostBuilder()
    .ConfigureFunctionsWebApplication()
    .ConfigureAppConfiguration( config =>
    {
        config.AddUserSecrets<Function1>(optional: true, reloadOnChange: false);
    })
    .ConfigureServices(services =>
    {
        services.AddApplicationInsightsTelemetryWorkerService();
        services.ConfigureFunctionsApplicationInsights();
    })
    .Build();

host.Run();
  • 或者您可以直接在 local.settings.json 中添加存储连接字符串。

local.settings.json:

{
    "IsEncrypted": false,
    "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated",
    "storageconnection":"<Storage_connection_string>"
  }
}

将连接字符串添加为环境变量

Azure function App=>Settings=>Environment Variables=>New Application Setting

  • 将Blob Trigger功能部署到Azure。

传送门:

  • 能够成功运行门户中的功能:

enter image description here

参考资料:

  1. GitHub 问题 1

  2. GitHub问题2

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