简体   繁体   English

无法删除触发 Azure 函数 Blob 输入绑定的 Blob

[英]Cannot delete the blob that triggers an Azure Function Blob Input Binding

This is a follow-up question to this question:这是这个问题的后续问题:

How to delete a blob using Azure Functions? 如何使用 Azure Functions 删除 Blob?

When a blob triggers my Azure Function, I need to delete it once its processing is done.当 blob 触发我的 Azure 函数时,我需要在处理完成后将其删除。 Otherwise, I will end up with many blobs in the container.否则,我最终会在容器中出现许多斑点。

When I run the following code:当我运行以下代码时:

CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connection);
var blobClient = storageAccount.CreateCloudBlobClient();
var container = blobClient.GetContainerReference("process");
var blockBlob = container.GetBlockBlobReference($"process/in/{name}"); // ==> This was the problem. See the answer for more info. 
bool deleted = blockBlob.DeleteIfExists();

the method blockBlob.DeleteIfExists() always returns false and it never deletes the blob.方法blockBlob.DeleteIfExists()总是返回 false 并且它从不删除 blob。

My guess is that the blob is somehow locked by the function execution since it just triggered it.我的猜测是 blob 以某种方式被函数执行锁定,因为它刚刚触发了它。

[Update 1] [更新1]

... ...

[Update 2] [更新2]

Many thanks to @Jerry Liu, the issue had nothing to do with Azure Fundctions.非常感谢@Jerry Liu,这个问题与 Azure Fundctions 无关。

The trick is that blockBlob.DeleteIfExists() returns false when caller sends a wrong path by mistake.诀窍是当调用者错误地发送错误路径时, blockBlob.DeleteIfExists()返回 false。

A better approach could be using 'blockBlob.Delete' and find out what is the actual issue.更好的方法可能是使用“blockBlob.Delete”并找出实际问题。

See DeleteIfExists source code for more info.有关详细信息,请参阅 DeleteIfExists 源代码。

https://github.com/Azure/azure-storage-net/blob/master/Lib/ClassLibraryCommon/Blob/CloudBlob.cs#L1993 https://github.com/Azure/azure-storage-net/blob/master/Lib/ClassLibraryCommon/Blob/CloudBlob.cs#L1993

Another related question: Azure CloudBlockBlob.DeleteIfExists() - Does false always mean the blob doesn't exist?另一个相关问题: Azure CloudBlockBlob.DeleteIfExists() - false 是否总是意味着 blob 不存在?

Problem locates at this line问题出现在这一行

var blockBlob = container.GetBlockBlobReference($"process/in/{name}");

The blob name should be $"in/{name}" because we call GetBlockBlobReference based on specific container which we have already got in GetContainerReference . blob 名称应为$"in/{name}"因为我们根据已在GetContainerReference获得的特定容器调用GetBlockBlobReference

The duplicate causes Storage fail to find the blob.重复导致存储无法找到 blob。 We may be confused about no related prompt/exception because DeleteIfExists also return false when blob doesn't exist.我们可能会对没有相关提示/异常感到困惑,因为DeleteIfExists在 blob 不存在时也会返回 false。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM