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

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

Blob trigger using event grid in .NET Isolated Worker functions

问题

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

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

我尝试使用来自Microsoft.Azure.Functions.Worker.Extensions.Storage.Blobs的BlobTrigger,但它没有Source属性,因此以下代码不起作用。

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

英文:

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

public class SftpFileUploadTrigger
{
    private readonly IDtoBuilderManager _dtoBuilderManager;

    public SftpFileUploadTrigger(IDtoBuilderManager dtoBuilderManager)
    {
        _dtoBuilderManager = dtoBuilderManager;
    }

    [FunctionName(nameof(SftpFileUploadTrigger))]
    public async Task Run(
        [BlobTrigger("%BlobStorageContainers:AzureSftp%/{name}", Source = BlobTriggerSource.EventGrid, Connection = "Storage:ConnectionString")] Stream dataFileBlob,
        string name,
        [DurableClient] IDurableOrchestrationClient orchestrationClient,
        ILogger log)
    {
        log.LogInformation("{FunctionName} function processed blob with name {Name} and size {Length} Bytes", nameof(SftpFileUploadTrigger), name, dataFileBlob.Length);

        // Create data transfer object
        var dataFileCreateDto = _dtoBuilderManager.DataFileCreateDtoBuilder
            .WithName(name)
            .AddSession()
            .WithStatus(DataFileStatus.Pending)
            .WithSessionType(DataFileSessionType.DataFileIntake)
            .WithStartedOn(DateTime.UtcNow)
            .Build();

        // Start data file intake workflow
        string instanceId = await orchestrationClient.StartNewAsync(nameof(DataFileIntakeOrchestration), dataFileCreateDto);

        log.LogInformation("Started {OrchestrationName} with id {InstanceId}", nameof(DataFileIntakeOrchestration), instanceId);
    }
}

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.

public class SftpFileUploadTrigger
{
    private readonly ILogger<SftpFileUploadTrigger> _logger;

    public SftpFileUploadTrigger(ILogger<SftpFileUploadTrigger> logger)
    {
        this._logger = logger;
    }

    [Function(nameof(SftpFileUploadTrigger))]
    public async Task Run(
        [BlobTrigger("%BlobStorageContainers:AzureSftp%/{name}", Source = BlobTriggerSource.EventGrid, Connection = "Storage:ConnectionString")] Stream dataFileBlob,
        string name,
        [DurableClient] DurableTaskClient client)
    {
        this._logger.LogInformation("{FunctionName} function processed blob with name {Name} and size {Length} Bytes", nameof(SftpFileUploadTrigger), name, dataFileBlob.Length);
    }
}

答案1

得分: 1

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

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

代码:

using System;
using System.IO;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;

namespace Company.Function
{
    public class BlobTriggerEventGrid
    {
        private readonly ILogger _logger;

        public BlobTriggerEventGrid(ILoggerFactory loggerFactory)
        {
            _logger = loggerFactory.CreateLogger<BlobTriggerEventGrid>();
        }

        [Function("BlobTriggerEventGrid")]
        public void Run([BlobTrigger("samples-workitems/{name}", Connection = "afreensa01_STORAGE")] string myBlob, string name)
        {
            _logger.LogInformation($"C# Blob trigger function Processed blob\n Name: {name} \n Data: {myBlob}");
        }
    }
}

使用 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:

using  System;
using  System.IO;
using  Microsoft.Azure.Functions.Worker;
using  Microsoft.Extensions.Logging;
namespace  Company.Function
{
public  class  BlobTriggerEventGrid
{
private  readonly  ILogger  _logger;
public  BlobTriggerEventGrid(ILoggerFactory  loggerFactory)
{
_logger  =  loggerFactory.CreateLogger&lt;BlobTriggerEventGrid&gt;();
}
[Function(&quot;BlobTriggerEventGrid&quot;)]
public  void  Run([BlobTrigger(&quot;samples-workitems/{name}&quot;,Connection  =  &quot;afreensa01_STORAGE&quot;)] string  myBlob, string  name)
{
_logger.LogInformation($&quot;C# Blob trigger function Processed blob\n Name: {name}  \n Data: {myBlob}&quot;);
}
}
}

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:

确定