Azure CloudBlockBlob.DeleteIfExists()-是否始终为false表示blob不存在? [英] Azure CloudBlockBlob.DeleteIfExists() - Does false always mean the blob doesn't exist?

查看:208
本文介绍了Azure CloudBlockBlob.DeleteIfExists()-是否始终为false表示blob不存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道CloudBlockBlob.DeleteIfExists()如果blob存在,则返回true,否则,则返回false.

I know that CloudBlockBlob.DeleteIfExists() returns true if the blob exists and false when it does not.

但是我很好奇,如果blob确实存在会发生什么,但是在Azure中出了点问题,导致文件删除没有发生(我找不到有关该行为的任何文档).

However I'm curious the know what happens if the blob does exist, but something goes wrong in Azure which causes the file deletion to not occur (I can't find any documentation on that behavior).

我担心的是,它将返回false而不是引发某种异常,因此,我相信该blob实际上仍然存在时会被删除.

My concern is that it will return false instead of throwing some kind of exception, so I'll believe the blob is deleted, when it's actually still there.

简而言之,如果我得到false的返回值,它是否是 总是 ,表示该blob不存在,不需要删除,我将如果Azure的某个地方出了问题,会得到某种例外吗?

In short, if I get a value of false back, does it always mean that the blob didn't exist, no deletion was necessary, and I'll get some kind of exception if something goes wrong on Azure's end?

谢谢.

推荐答案

查看此方法的源代码

Looking at the source code for this method here, you will get true if the blob is deleted, false if the blob (or blob container) doesn't exist. In all other circumstances (say the blob is leased and thus can't be deleted), an exception will be raised. Here's the relevant code:

    public virtual bool DeleteIfExists(DeleteSnapshotsOption deleteSnapshotsOption = DeleteSnapshotsOption.None, AccessCondition accessCondition = null, BlobRequestOptions options = null, OperationContext operationContext = null)
    {
        BlobRequestOptions modifiedOptions = BlobRequestOptions.ApplyDefaults(options, BlobType.Unspecified, this.ServiceClient);
        operationContext = operationContext ?? new OperationContext();

        try
        {
            this.Delete(deleteSnapshotsOption, accessCondition, modifiedOptions, operationContext);
            return true;
        }
        catch (StorageException e)
        {
            if (e.RequestInformation.HttpStatusCode == (int)HttpStatusCode.NotFound)
            {
                if ((e.RequestInformation.ExtendedErrorInformation == null) ||
                    (e.RequestInformation.ExtendedErrorInformation.ErrorCode == BlobErrorCodeStrings.BlobNotFound) ||
                    (e.RequestInformation.ExtendedErrorInformation.ErrorCode == BlobErrorCodeStrings.ContainerNotFound))
                {
                    return false;
                }
                else
                {
                    throw;
                }
            }
            else
            {
                throw;
            }
        }
    }  

这篇关于Azure CloudBlockBlob.DeleteIfExists()-是否始终为false表示blob不存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆