简体   繁体   English

即使函数失败,也会创建输出 blob

[英]Output blob gets created even though Function fails

I have an Azure Function that is triggered by some blob creation, is doing some transforms (OCR mostly) and writes the result again to Blob:我有一个由一些 blob 创建触发的 Azure 函数,正在执行一些转换(主要是 OCR)并将结果再次写入 Blob:

[FunctionName("Ocr")]
public static async Task Run([BlobTrigger("input/{name}")]CloudBlockBlob myBlob,[Blob("output/{name}_{DateTime}.txt", FileAccess.Write)] TextWriter resultTextFile, string name,ILogger log)
{
    try
    {
        var ocrResult = await DoOcr(name);
        // This only happens if no exception was thrown
        await resultTextFile.WriteAsync(ocrResult.Text);
    }
    catch (Exception e)
    {
        log.LogError(e, $"Exception during processing. Cannot process document {name}");
    }
}

This works all fine and well.这一切都很好。 However, if something goes wrong inside the "DoOcr()" function, an exception is thrown - and caught it inside my catch block - the Function ends and an empty new blob was created at "output/{name}_{DateTime}.txt".但是,如果“DoOcr()”函数内部出现问题,则会抛出异常 - 并在我的 catch 块中捕获它 - 函数结束并在“output/{name}_{DateTime}”处创建一个空的新 blob。文本”。

"WriteAsync()" gets never called but why is the file still created? “WriteAsync()”永远不会被调用,但为什么仍然创建文件? It does not happen if I use "CloudBlockBlob" instead of "TextWriter" in the binding.如果我在绑定中使用“CloudBlockBlob”而不是“TextWriter”,则不会发生这种情况。

As Kyle said, TextWriter writer = new StreamWriter(stream);正如凯尔所说, TextWriter writer = new StreamWriter(stream); . . So when you use TextWriter as blob output, it will initializes a new instance of the StreamWriter class for the specified file on the specified path.因此,当您使用TextWriter作为 blob 输出时,它将为指定路径上的指定文件初始化StreamWriter类的新实例。 If the file exists, it can be either overwritten or appended to.如果文件存在,它可以被覆盖或附加到。 If the file does not exist, this constructor creates a new file.如果文件不存在,则此构造函数创建一个新文件。

While you use CloudBlockBlob as blob output, it will initializes a new instance of the CloudBlockBlob class using an absolute URI to the blob which read as an in-memory stream and then get dumped to the file.当您使用CloudBlockBlob作为 blob 输出时,它将使用 blob的绝对 URI 初始化CloudBlockBlob类的新实例,该 blob读取为内存中的流,然后转储到文件中。 So, if the stream is interrupt, it will not create a output new file.因此,如果流被中断,则不会创建输出新文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Azure 事件网格(已创建 Blob) - Azure EventGrid (Blob Created) Function output stream 函数在 UI 更改为可见之前运行,即使 UI 在函数运行之前设置为可见 - Function runs before UI gets changed to visible even though the UI was set to visible before the function was ran 即使实际和预期匹配,断言也失败 - Assert Fails even though actual and expected match 即使我调用 CloudBlob.SetMetadata 也不会保存 Blob 元数据 - Blob metadata is not saved even though I call CloudBlob.SetMetadata 即使IncludeSubdirectories设置为false,FileSystemWatcher也会获得子目录事件 - FileSystemWatcher gets subdirectory events even though IncludeSubdirectories is set to false 即使取消选中框,也会选中checkedListBox中的C#项 - C# Item in checkedListBox gets selected even though box is unchecked 指定的资源名称包含无效字符 为 blob 生成 SAS url 时出错,即使 blob 名称有效 - The specified resource name contains invalid characters Error generating SAS url for blob even though blob name is valid 即使调用TraceEvent也不创建跟踪文件? - Trace file isn't being created even though TraceEvent is called? C#单元测试失败,即使AssertAreEqual看起来相等 - C# Unit Test Fails, even though AssertAreEqual appear to be equal 即使我有隐式运算符,类型转换也会失败 - Type casting fails even though I have implicit operators
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM