You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure环境中Blob触发函数无法绑定Stream参数问题

Azure函数Blob触发器发布后参数转换异常问题

问题现象

本地运行正常的Blob触发Azure函数,通过Visual Studio 2022发布到Azure环境后,触发时抛出参数转换异常,无法执行。异常核心是存储连接字符串解析格式错误(要求name=value格式),导致stream参数无法从GrpcModelBindingData转换为System.IO.Stream。尝试将参数改为byte[]或string仍未解决,Blob上传能触发函数但无法完成处理。

相关代码与配置

函数代码

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/"
}

异常详细信息

Error converting 1 input parameters for Function 'BlobTriggeredFunction': Cannot convert input parameter 'stream' to type 'System.IO.Stream' from type 'Microsoft.Azure.Functions.Worker.Grpc.Messages.GrpcModelBindingData'. Error:System.FormatException: Settings must be of the form "name=value".
at Azure.Storage.StorageConnectionString.<>c.b__67_0(String err)
at Azure.Storage.StorageConnectionString.ParseStringIntoSettings(String connectionString, Action1 error) at Azure.Storage.StorageConnectionString.ParseCore(String connectionString, StorageConnectionString& accountInformation, Action1 error)
at Azure.Storage.StorageConnectionString.Parse(String connectionString)
at Azure.Storage.Blobs.BlobServiceClient..ctor(String connectionString, BlobClientOptions options)
at Microsoft.Azure.Functions.Worker.BlobStorageBindingOptions.CreateClient() in D:\a_work\1\s\extensions\Worker.Extensions.Storage.Blobs\src\Config\BlobStorageBindingOptions.cs:line 35
at Microsoft.Azure.Functions.Worker.BlobStorageConverter.CreateBlobContainerClient(String connectionName, String containerName) in D:\a_work\1\s\extensions\Worker.Extensions.Storage.Blobs\src\BlobStorageConverter.cs:line 253
at Microsoft.Azure.Functions.Worker.BlobStorageConverter.ConvertModelBindingDataAsync(Type targetType, BlobBindingData blobData) in D:\a_work\1\s\extensions\Worker.Extensions.Storage.Blobs\src\BlobStorageConverter.cs:line 118
at Microsoft.Azure.Functions.Worker.BlobStorageConverter.ConvertFromBindingDataAsync(ConverterContext context, ModelBindingData modelBindingData) in D:\a_work\1\s\extensions\Worker.Extensions.Storage.Blobs\src\BlobStorageConverter.cs:line 63

解决方案

1. 修正Azure环境的连接字符串配置

本地使用secrets.json存储配置,但发布到Azure后,函数应用的应用程序设置中需手动配置完整的存储连接字符串:

  • 登录Azure门户,进入目标函数应用
  • 导航至配置 > 应用程序设置
  • 添加或更新BlobStorageConnectionString,值必须是标准格式的完整存储账户连接字符串(示例:DefaultEndpointsProtocol=https;AccountName=xxx;AccountKey=xxx;EndpointSuffix=core.windows.net),不能使用拆分的blob/queue端点

2. 清理本地机密的拆分配置

secrets.json中BlobStorageConnectionString:blob和BlobStorageConnectionString:queue的拆分格式会干扰SDK解析,需删除这两项,仅保留完整的连接字符串:

{
  "BlobStorageConnectionString": "DefaultEndpointsProtocol=https;AccountName=xxx;AccountKey=xxx;EndpointSuffix=core.windows.net"
}

3. 确认发布时的配置同步

Visual Studio发布过程中,在应用设置步骤里,确保本地的BlobStorageConnectionString正确映射到Azure函数应用的对应配置项。注意:用户机密不会自动发布到Azure,需手动在门户配置。

4. 验证连接字符串格式

确保连接字符串是直接从Azure存储账户的访问密钥页面复制的完整内容,不要手动修改格式。存储SDK严格要求name=value键值对格式,任何格式错误都会导致解析失败。


内容的提问来源于stack exchange,提问作者Charlemagne

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 21:05:12