[英]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.