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

使用Azure Function上传文件至Data Lake时Storage Event Trigger不生效

问题排查方案

核心原因定位

你当前的上传逻辑分为两步操作:

  1. 调用directoryClient.CreateFile(pathFileDL)创建0KB的空文件,这一步会先触发一次BlobCreated事件,但此时文件是空的
  2. 调用fileClient.Upload(file.Content, true)覆写已有文件,这一步要么没有正确触发最终的文件提交事件,要么属于BlobUpdated事件类型,不在你触发器的监听范围内,就会导致ADF触发器无响应。如果触发器开启了忽略空Blob的配置,第一步产生的空文件事件也会被直接过滤。

修复方案

方案1:合并两步上传操作,直接调用UploadFile方法

不要单独调用CreateFile创建空文件,直接通过DataLakeDirectoryClient.UploadFile方法一次性上传完整文件,确保只产生一次有效的BlobCreated事件:

public void UploadFileToDatalake(FileUpload file, string containerSasUri, string stage, string dir, ILogger log)
{
    log.LogInformation("inside uploadFileTodatalake");
    UriBuilder sasUri = new UriBuilder(containerSasUri);
    DataLakeServiceClient dataLakeServiceClient = new DataLakeServiceClient(sasUri.Uri);

    string container = Globals._transientContainer;
    var fileSystemClient = dataLakeServiceClient.GetFileSystemClient(container);

    string pathFileDL = file.Name;
    string directory = dir;

    MemoryStream ms = new MemoryStream();
    StreamWriter sw = new StreamWriter(ms);
    sw.Write(File.ReadAllText(file.FullPath));
    sw.Flush();
    ms.Seek(0, SeekOrigin.Begin);

    file.Content = ms;

    DataLakeDirectoryClient directoryClient = fileSystemClient.GetDirectoryClient(directory);
    try
    {
        if (directoryClient.Exists())
        {
            log.LogInformation(directoryClient.Name);
            file.Content.Position = 0;
            // 直接上传文件,overwrite参数设为true即可覆盖同名文件
            directoryClient.UploadFile(pathFileDL, file.Content, true);
            log.LogInformation($"{stage}: {file.Name} uploaded to {pathFileDL}");
            file.Content.Close();
        }
    }
    finally
    {
        //fileSystemClient.Delete();
    }
}

方案2:保留现有逻辑,修改触发器事件过滤规则

如果必须保留两步操作的逻辑,可以调整ADF存储事件触发器的配置:

  • 进入ADF触发器设置页面,找到「事件类型」配置,确保同时勾选了Blob created和Blob updated事件
  • 检查触发器的Blob路径匹配规则,确认你上传的文件路径完全符合规则,没有大小写、路径前缀不匹配的问题
  • 关闭触发器的「忽略空Blob」选项

额外验证步骤

  • 可以进入Azure存储账户的「事件」页面,查看事件订阅的传递日志,确认Function上传文件时有没有产生对应的事件、事件有没有被成功投递到ADF
  • 检查你Function使用的SAS权限,确保有足够的权限写入ADLS Gen2,同时写入的文件确实存在于存储账户中,和你手动上传的路径完全一致

内容的提问来源于stack exchange,提问作者Nikita M.

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.06 17:54:03