【问题标题】:Why does AzureBlobClient.ExistsAsync throw a 404 error?为什么 AzureBlobClient.ExistsAsync 会引发 404 错误?
【发布时间】:2020-12-09 23:26:16
【问题描述】:

我正在像这样创建BlobClient

private BlobClient CreateBlobClient(string container, string key) =>
    new BlobContainerClient(ConnectionString, container).GetBlobClient(key);

有时,当我在此 BlobClient 上调用 ExistsAsync 而不是返回 false 时,客户端会抛出带有 e.Status == 404RequestFailedException

谁能告诉我这是为什么?

我当然不应该用一个 catch 来结束这个调用?

            var client = CreateBlobClient(container, key);
            try
            {
                // Surprisingly, ExistsAsync throws an exception if the blob is not found.
                Azure.Response<bool> result = await client.ExistsAsync().ConfigureAwait(false);
                return result.Value;
            }
            catch (RequestFailedException e) when (e.Status == 404)
            {
                return false;
            }

附加信息:

Microsoft.Azure.WebJobs.Extensions.Storage 版本 4.0.2

这段代码的入口如下

  1. Blob 已上传
  2. 一条消息发布到 ServiceBus
  3. 此函数运行

然后我创建一个 BlobClient 来检查一对文件是否存在 uploaded.txtuploaded.txt.sig - 这是 ExistsAsync 检查有时会引发错误代码 404 的异常,而不仅仅是返回 false。

【问题讨论】:

    标签: azure-blob-storage


    【解决方案1】:

    我在GetBlobClient 中重现了将key 替换为blobName 的代码。它有效。

    BlobClient client = new BlobContainerClient("connectionString", "containerName").GetBlobClient("blobName");
                    
    Azure.Response<bool> result = await client.ExistsAsync().ConfigureAwait(false);
    Console.WriteLine("result: " + result.Value);
    

    【讨论】:

    • 感谢您的尝试。我知道它有效,问题是为什么有时它会在 Azure 上引发 404 异常。
    • @PeterMorris 你的 NuGet 包的版本是什么?抛出 404 错误时有什么特别之处,例如特点 ”#%&*”?我尝试使用 Azure.Storage.Blobs 12.5.1,但未能重现该错误。这可能是一个错误。
    • 这是一个 .NET Core、Azure Function 应用程序。我在问题中添加了额外信息。
    • 我找不到重现错误的方法。如果错误不经常发生,可以使用try-catch 检查文件是否存在。您可以反馈给UserVoice
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-29
    • 1970-01-01
    • 2015-09-02
    • 2014-09-15
    • 2011-01-19
    • 2014-12-11
    • 1970-01-01
    相关资源
    最近更新 更多