c# - Azure 函数在 blob 触发期间找不到 blob

标签 c# azure azure-functions azure-blob-storage

当 HTML 文件放入 Azure Blob 存储时,会触发一个 Azure 函数。该函数打开 HTML 文件,并将其转换为 JSON。有一小部分触发文件(小于 1%)会导致以下异常:

Microsoft.WindowsAzure.Storage.StorageException

确实有第二个函数是由 blob 的放置触发的,它会更改文件内容类型,但我不确定这是否会影响第一个函数打开文件的能力。

如何才能让 Azure 函数正确处理 HTML 文件而不引发此类异常?

异常属性:

消息:执行函数时出现异常:[此处为函数名称] 不满足使用 HTTP 条件 header 指定的条件。

异常类型:Microsoft.WindowsAzure.Storage.StorageException

失败的方法:HtmlAgilityPack.HtmlDocument.Load

异常类型:Microsoft.WindowsAzure.Storage.StorageException

函数 1(为简洁起见,省略了支持方法、类和命名空间):

using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using HtmlAgilityPack;
using System.Threading.Tasks;

[FunctionName("Function name")]
public static async Task Run([BlobTrigger("container-name/html/{name}", Connection = "ConnectionString")]Stream myBlob, ILogger log, Binder binder)
{
    var doc = new HtmlDocument();
    doc.Load(myBlob);
    var form = doc.DocumentNode.SelectSingleNode("//form");
    var elements = form.SelectNodes("//input");
    CustomType MyObject = BuildObject(elements);

    var attributes = new Attribute[]
    {
        new BlobAttribute("container-name/json/" + MyObject.ID + ".json"),
        new StorageAccountAttribute("ConnectionString")
    };

    using (var writer = await binder.BindAsync<TextWriter>(attributes))
    {
        writer.Write(BuildJSON(MyObject));
    }

}

函数 2 相同的触发器,但在不同的函数中,并且有自己的 .cs 文件。为简洁起见,省略了类和命名空间:

using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;
using Microsoft.WindowsAzure.Storage.Blob;

[FunctionName("Function name")]
public static async Task Run([BlobTrigger("container-name/html/{name}", Connection = "ConnectionString")]ICloudBlob myBlob)
{
    if (myBlob.Properties.ContentType == "text/html; charset=utf-8")
    return;

    myBlob.Properties.ContentType = "text/html; charset=utf-8";
    await myBlob.SetPropertiesAsync();
}

最佳答案

我认为你的错误应该是这样的:Funtion1检索了blob,然后function2对blob的操作导致了Etag的变化。然后function1尝试加载检索到的blob,却发现Etag发生了变化,所以给你返回异常。

如果资源被多个应用访问或更改,请尝试确保原始文件没有被更改。否则 blob 的 Etag 将自动更改。

Azure 存储 blob 使用“强 Etag 验证”。两个资源表示的内容必须逐字节相同,并且所有其他实体字段(例如内容语言)也保持不变。

请引用:https://www.microsoftpressstore.com/articles/article.aspx?p=2224058&seqNum=12

关于c# - Azure 函数在 blob 触发期间找不到 blob,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60402155/

相关文章:

azure - 通过 Startup.cs 添加依赖项注入(inject)到 Azure Functions 会导致主机错误

azure - 将新的 VM IP 地址添加到 Azure 网关后端池失败

azure - 如何将 Azure SQL 数据库中的函数应用程序列入白名单

c# - 使用 C# 防止屏幕休眠

c# - NotNull 属性

Azure 混合连接管理器卡在 "Saving please wait"

azure - Microsoft.Azure.WebJobs.Extensions.EventGrid : Object reference not set to an instance of an object

azure - 微软图: Can I (re)use the user's Bearer Token forwarded by AAD in order to make "delegated" calls to the Graph API?

c# - 如何将自定义数据传递给 HandleChallengeAsync

c# - 如何存储数十亿个 JSON 文件并进行查询