升级 Azure Blob API 11->12,在 12 中找不到 blob 名称中的前导斜杠

Upgrading Azure Blob API 11->12, leading slash in blob name cannot be found in 12

我的项目是很久以前使用 Microsoft.Azure.Storage.Blob 构建的,我有许多包含许多文件的容器。我不确定它是怎么发生的,但我所有的文件夹结构中都有一个前导斜杠。在我尝试将我的代码升级到 Azure.Blobs (v12) 之前,这一直不是问题。
当我在 Azure 门户中查看我的 blob 时,它在根文件夹中显示一个空白 /,向下浏览我看到它看起来像这样 [容器]//[第一个文件夹]/[第二个文件夹]/[文件名]

A​​zure 门户显示和下载文件没有问题。当我查看属性时,URL 看起来像 https://[账户].blob.core.windows.net/[容器]//folder1/folder2/file.ext

更新我的代码后,我发现 container.GetBlobClient([文件夹+文件名]) 不会检索任何文件。它总是得到一个 404。当我在调试器中查看 URL 它试图访问的内容时,它消除了双斜杠,所以它看起来像 https://[账户].blob.core.windows.net/[容器]/folder1/folder2/file.ext

我试过在 [文件夹+文件名] 前加一个斜杠,但它总是会把它去掉。在前面加上两个斜线也是如此。

我在谷歌上搜索了一整天,在这里找不到答案。有解决方法吗?我认为必须存在,因为 Azure 门户和 Cloudberry Explorer 都可以访问和下载我的 blob。

一种可能的解决方法是使用 blob 的完整 URL 创建 BlobClient 的实例。这样可以保留前导斜杠。

请看下面的代码。它利用 Azure.Storage.Blobs version 12.10.0.

using System;
using System.Threading.Tasks;
using Azure.Storage;
using Azure.Storage.Blobs;

namespace SO71302870
{
    class Program
    {
        private const string accountName = "account-name";

        private const string accountKey = "account-key";

        private const string containerName = "container-name";
        private const string blobName = "/000/1.txt";//notice the leading slash (/) here.
        static async Task Main(string[] args)
        {
            StorageSharedKeyCredential credential = new StorageSharedKeyCredential(accountName, accountKey);
            BlobClient blobClient =
                new BlobClient(new Uri($"https://{accountName}.blob.core.windows.net/{containerName}/{blobName}"), credential);
            var properties = await blobClient.GetPropertiesAsync();
            Console.WriteLine(properties.Value.ContentLength);
        }
        
    }
}