Blob触发器在.NET孤立的工作函数中使用事件网格

huangapple go评论116阅读模式
英文:

Blob trigger using event grid in .NET Isolated Worker functions

问题

以下是您提供的代码的翻译部分:

  1. 我有一个来自Microsoft.Azure.WebJobsBlobTrigger.NET内部处理函数)。
  1. 原因是当使用BlobTriggerSource.LogsAndContainerScan时存在延迟。微软确实说它可能需要最多10分钟才能触发触发器,但那是完全不准确的。有时候它需要长达一小时,所以我设置了一个事件网格并更改了数据源。
  1. 现在我想将此触发器迁移到.NET 7隔离的工作器函数,但在微软的文档中找不到任何信息。
  2. 我尝试使用来自Microsoft.Azure.Functions.Worker.Extensions.Storage.BlobsBlobTrigger,但它没有Source属性,因此以下代码不起作用。

请注意,代码中的HTML编码(如")已经被翻译成了相应的引号字符。如果您需要进一步的帮助或翻译其他部分,请随时提问。

英文:

I have this BlobTrigger that comes from Microsoft.Azure.WebJobs (.NET in-process functions).

  1. public class SftpFileUploadTrigger
  2. {
  3. private readonly IDtoBuilderManager _dtoBuilderManager;
  4. public SftpFileUploadTrigger(IDtoBuilderManager dtoBuilderManager)
  5. {
  6. _dtoBuilderManager = dtoBuilderManager;
  7. }
  8. [FunctionName(nameof(SftpFileUploadTrigger))]
  9. public async Task Run(
  10. [BlobTrigger("%BlobStorageContainers:AzureSftp%/{name}", Source = BlobTriggerSource.EventGrid, Connection = "Storage:ConnectionString")] Stream dataFileBlob,
  11. string name,
  12. [DurableClient] IDurableOrchestrationClient orchestrationClient,
  13. ILogger log)
  14. {
  15. log.LogInformation("{FunctionName} function processed blob with name {Name} and size {Length} Bytes", nameof(SftpFileUploadTrigger), name, dataFileBlob.Length);
  16. // Create data transfer object
  17. var dataFileCreateDto = _dtoBuilderManager.DataFileCreateDtoBuilder
  18. .WithName(name)
  19. .AddSession()
  20. .WithStatus(DataFileStatus.Pending)
  21. .WithSessionType(DataFileSessionType.DataFileIntake)
  22. .WithStartedOn(DateTime.UtcNow)
  23. .Build();
  24. // Start data file intake workflow
  25. string instanceId = await orchestrationClient.StartNewAsync(nameof(DataFileIntakeOrchestration), dataFileCreateDto);
  26. log.LogInformation("Started {OrchestrationName} with id {InstanceId}", nameof(DataFileIntakeOrchestration), instanceId);
  27. }
  28. }

The reason why I used BlobTriggerSource.EventGrid is because of the delays when using BlobTriggerSource.LogsAndContainerScan. Microsoft does say that it can take up to 10 minutes for the trigger to fire but that is a total lie. Sometimes it took up to an hour so I just setup an event grid and changed the source.

Now I want to migrate this trigger to .NET 7 Isolated Worker functions and I can't seem to find anything in Microsoft's documentation.

I have tried using the BlobTrigger from Microsoft.Azure.Functions.Worker.Extensions.Storage.Blobs but it does not have a Source property so the below code does not work.

  1. public class SftpFileUploadTrigger
  2. {
  3. private readonly ILogger<SftpFileUploadTrigger> _logger;
  4. public SftpFileUploadTrigger(ILogger<SftpFileUploadTrigger> logger)
  5. {
  6. this._logger = logger;
  7. }
  8. [Function(nameof(SftpFileUploadTrigger))]
  9. public async Task Run(
  10. [BlobTrigger("%BlobStorageContainers:AzureSftp%/{name}", Source = BlobTriggerSource.EventGrid, Connection = "Storage:ConnectionString")] Stream dataFileBlob,
  11. string name,
  12. [DurableClient] DurableTaskClient client)
  13. {
  14. this._logger.LogInformation("{FunctionName} function processed blob with name {Name} and size {Length} Bytes", nameof(SftpFileUploadTrigger), name, dataFileBlob.Length);
  15. }
  16. }

答案1

得分: 1

是的,Microsoft.Azure.Functions.Worker.Extensions.Storage.Blobs 包不具有 Source 属性,因为 SourceMicrosoft.Azure.WebJobs 包的一部分,而隔离函数不使用 Microsoft.Azure.WebJobs

我使用了以下步骤来以另一种方式实现相同的功能-

代码:

  1. using System;
  2. using System.IO;
  3. using Microsoft.Azure.Functions.Worker;
  4. using Microsoft.Extensions.Logging;
  5. namespace Company.Function
  6. {
  7. public class BlobTriggerEventGrid
  8. {
  9. private readonly ILogger _logger;
  10. public BlobTriggerEventGrid(ILoggerFactory loggerFactory)
  11. {
  12. _logger = loggerFactory.CreateLogger<BlobTriggerEventGrid>();
  13. }
  14. [Function("BlobTriggerEventGrid")]
  15. public void Run([BlobTrigger("samples-workitems/{name}", Connection = "afreensa01_STORAGE")] string myBlob, string name)
  16. {
  17. _logger.LogInformation($"C# Blob trigger function Processed blob\n Name: {name} \n Data: {myBlob}");
  18. }
  19. }
  20. }

使用 cmd 命令运行 ngrok.exe http http://localhost:7071 并复制 https://xxxxxxxx.ngrok.io

根据 ms 文档,你的事件驱动的 blob 存储触发器应该看起来像 http://localhost:7071/runtime/webhooks/blobs?functionName={your function name}

http://localhost:7071 替换为 https://xxxxxxxx.ngrok.io,最终的终端点将看起来像 http://xxxxxxxx.ngrok.io/runtime/webhooks/blobs?functionName={your function name}

在创建事件之前,请确保你的函数在本地运行,并且 ngrok 也在线。

使用以下 http://xxxxxxxx.ngrok.io/runtime/webhooks/blobs?functionName={your function name} 终端点并单击创建。

一旦事件创建完成,你可以上传一个 blob 来测试是否正常工作。

输出:

我为 Microsoft.Azure.Functions.Worker.Extensions 包的不具备 source 参数支持问题创建了一个 github 问题,你可以参考它以获取最新的更新。

英文:

> I have tried using the BlobTrigger fromMicrosoft.Azure.Functions.Worker.Extensions.Storage.Blobs but it does not have a Source property

Yes, Microsoft.Azure.Functions.Worker.Extensions.Storage.Blobs package doesn't have a Source property as Source is a part of Microsoft.Azure.WebJobs package and isolated function doesn't use Microsoft.Azure.WebJobs .

I have used an alternative way to achieve the same by following below steps-

code:

  1. using System;
  2. using System.IO;
  3. using Microsoft.Azure.Functions.Worker;
  4. using Microsoft.Extensions.Logging;
  5. namespace Company.Function
  6. {
  7. public class BlobTriggerEventGrid
  8. {
  9. private readonly ILogger _logger;
  10. public BlobTriggerEventGrid(ILoggerFactory loggerFactory)
  11. {
  12. _logger = loggerFactory.CreateLogger&lt;BlobTriggerEventGrid&gt;();
  13. }
  14. [Function(&quot;BlobTriggerEventGrid&quot;)]
  15. public void Run([BlobTrigger(&quot;samples-workitems/{name}&quot;,Connection = &quot;afreensa01_STORAGE&quot;)] string myBlob, string name)
  16. {
  17. _logger.LogInformation($&quot;C# Blob trigger function Processed blob\n Name: {name} \n Data: {myBlob}&quot;);
  18. }
  19. }
  20. }

Run ngrok using ngrok.exe http http://localhost:7071 command in cmd and copy https://xxxxxxxx.ngrok.io

Blob触发器在.NET孤立的工作函数中使用事件网格

As per ms docs, your event-driven blob storage trigger should look like http://localhost:7071/runtime/webhooks/blobs?functionName={your function name}.

Replace http://localhost:7071 with https://xxxxxxxx.ngrok.io and your final endpoint would look like http://xxxxxxxx.ngrok.io/runtime/webhooks/blobs?functionName={your function name}.

Make sure before creating event, your function is running locally and ngrok is online too.

Blob触发器在.NET孤立的工作函数中使用事件网格

Blob触发器在.NET孤立的工作函数中使用事件网格

Use http://xxxxxxxx.ngrok.io/runtime/webhooks/blobs?functionName={your function name} endpoint below and click create.

Blob触发器在.NET孤立的工作函数中使用事件网格

Once the event is created then you can upload a blob to test if its working.

Output:

Blob触发器在.NET孤立的工作函数中使用事件网格

Blob触发器在.NET孤立的工作函数中使用事件网格

Blob触发器在.NET孤立的工作函数中使用事件网格

I have opened a github issue for Microsoft.Azure.Functions.Worker.Extensions package not having source parameter support. You can refer it for any latest update.

huangapple
  • 本文由 发表于 2023年7月31日 20:34:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76803684.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定