【问题标题】:Storage Event Trigger does not work when I upload file in Datalake using Azure Function当我使用 Azure 函数在 Datalake 中上传文件时,存储事件触发器不起作用
【发布时间】:2021-10-28 15:10:17
【问题描述】:

我创建了一个存储事件触发器来触发 Azure 数据工厂中的管道。当我手动将文件放入数据湖时,此触发器起作用。但是从 Azure Function 上传文件时,触发器不起作用。

以下是我在datalake中上传文件的功能。

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);
                    DataLakeFileClient fileClient = directoryClient.CreateFile(pathFileDL); // 0kb blob
                    file.Content.Position = 0;
                    fileClient.Upload(file.Content, true);
                    log.LogInformation($"{stage}: {file.Name} uploaded to {pathFileDL}");
                    file.Content.Close();
                }
            }
            finally
            {
                //fileSystemClient.Delete();
            }
        }



FileUpload 是我正在使用的模型类。

public class FileUpload
    {
        public string Name { get; set; }
        public Stream Content { get; set; }
        public string FullPath { get; set; }
    }

提前致谢。

【问题讨论】:

  • 我在这里意识到了我的错误。 FlieClient 首先创建一个空 blob,然后写入该 blob。这导致触发器出现问题。因此,我使用 BlobClient 而不是 FileClient,它得到了解决。

标签: c# .net azure-functions azure-data-factory-2 azure-data-lake-gen2


【解决方案1】:

我尝试使用the blog 使用azure function 在 azure blob 中上传文件

在创建 azure 函数之前,我创建了管道 Storage Event Trigger,以便在 存储 blob 上的新文件到达/创建时触发.

尝试手动使用azure portal上传文件。事件网格已触发

在 azure 上创建并发布 azure function 之后。现在运行 azure 函数。它在存储容器上生成随机文件,并且预期的存储事件触发器也运行。我可以在门户上看到 Triggered 输出。

【讨论】:

  • 感谢您的回答。我使用 BlobClient 而不是 FileClient。改变了nuget。它奏效了。
  • 感谢@NikitaM。我还建议使用 BlobClient 获取更多信息check this link
猜你喜欢
  • 1970-01-01
  • 2022-10-14
  • 2021-06-26
  • 2020-11-02
  • 1970-01-01
  • 1970-01-01
  • 2019-10-20
  • 2017-10-07
  • 1970-01-01
相关资源
最近更新 更多