【问题标题】:A way to know if a Blob object exists with Azure API一种通过 Azure API 了解 Blob 对象是否存在的方法
【发布时间】:2014-06-04 21:11:08
【问题描述】:

有没有办法在不获取整个 blob 对象列表的情况下知道容器中是否存在 blob 文件?

谢谢,

【问题讨论】:

    标签: azure azure-storage azure-blob-storage


    【解决方案1】:

    如果您知道 blob 的地址,Azure SDK 的提示是首先构建 CloudBlockBlob(或 CloudPageBlob),然后调用 FetchAttributes。如果找不到 blob,此调用将抛出 StorageClientException

    来自CloudBlobClient.GetBlockBlobReference 文档:

    FetchAttributes 方法执行 HEAD 请求以填充 blob 的属性和元数据,因此是一个轻量级选项 确定 blob 是否存在。

    【讨论】:

      【解决方案2】:

      从 Windows Azure 存储客户端库 2.0 开始,blob 包含方法 Exists(),例如:blob.Exists()

      BlobContainer 也是如此。

      【讨论】:

        【解决方案3】:

        这是我正在使用的代码

            public static bool Exists(this CloudBlob blob)
            {
                try
                {   
                    blob.FetchAttributes();
                    return true;
                }
                catch (StorageClientException e)
                {
                    if (e.ErrorCode == StorageErrorCode.ResourceNotFound)
                    {
                        return false;
                    }
                    else
                    {
                        throw;
                    }
                }
            }
        

        【讨论】:

        猜你喜欢
        • 2019-04-21
        • 2023-01-30
        • 2014-01-29
        • 2018-03-11
        • 2014-10-16
        • 2017-07-23
        • 1970-01-01
        • 2017-07-29
        • 1970-01-01
        相关资源
        最近更新 更多