【问题标题】:Cannot reference block blobs with blank space in filename无法引用文件名中带有空格的块 blob
【发布时间】:2019-05-31 17:29:39
【问题描述】:

我正在使用 Azure SDK for .Net(版本 9.3.1,平台 .NET-Standard 2.0)来处理 Azure Blob 存储,并且在引用 Blob 名称中有空格的块 Blob 时遇到了问题。 我已通过 Azure 存储资源管理器 1.6.1 将块 blob JSON Test.json 上传到私有容器中。

根据 Azure 存储资源管理器的 Blob 属性:

Name: `JSON Test.json`
URI: `https://<myaccountname>/<mycontainername>/JSON%20Test.json`

现在,我正在尝试通过 CloudBlock​Blob.​Exists​Async() 方法传递给 GetBlockBlobReference 非编码文件名 JSON Test.json 来检查 Blob 是否存在

结果为 FALSE。

现在,我正在另一个容器中以编程方式创建一个 blob,传递相同的非编码文件名,使用相同的 GetBlockBlobReference,并创建一个带有编码文件名的 blob。

Name: `JSON%20Test.json` 
URI: `https://<myaccountname>/<mycontainername2>/JSON%20Test.json`

我做错了什么?为什么在使用非编码文件名引用它时找不到通过 Azure 存储资源管理器创建的名称中有空格的块 blob?当以编程方式创建块 blob 时传递未编码的文件名,为什么文件名通过线路进行编码?

请帮忙。

提前非常感谢!

public async Task<bool> CheckExistsAsync(string connectionString, string containerName, string fileName)
        {
            var blockBlob = GetBlockBlobReference(connectionString, containerName, fileName);
            return await blockBlob.ExistsAsync();
        }

private static CloudBlockBlob GetBlockBlobReference(string connectionString, string containerName, string fileName)
        {
            return CloudStorageAccount
                .Parse(connectionString)
                .CreateCloudBlobClient()
                .GetContainerReference(containerName)
                .GetBlockBlobReference(fileName);
        }

【问题讨论】:

标签: c# azure-blob-storage azure-sdk-.net


【解决方案1】:

要检查 blob 是否存在,请尝试以下代码:

CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);
        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

var found = await blobClient.GetBlobReferenceFromServerAsync(new Uri(filename));

您可能需要通过 Uri 而不是最后一行中的字符串访问文件。我没有真正使用过你使用的异步方法,但上面的代码是适合我的代码。

【讨论】:

    【解决方案2】:

    请尝试将WindowsAzure.Storage更新到最新版本v9.3.3。

    我使用您的代码进行测试,并且 blob 名称包含空格没有问题。

    示例代码:

    using Microsoft.WindowsAzure.Storage;
    using Microsoft.WindowsAzure.Storage.Blob;
    using System;
    using System.Threading.Tasks;
    
    namespace AzureBlobConsole
    {
        class Program
        {
            static void Main(string[] args)
            {         
                string conn = "xxxx";
                bool x = CheckExistsAsync(conn, "f11", "222 json test.json").GetAwaiter().GetResult();
    
                //to see if the file exists or not
                Console.WriteLine(x);
                Console.WriteLine("completed.");
                Console.ReadLine();
            }
    
            public static async Task<bool> CheckExistsAsync(string connectionString, string containerName, string fileName)
            {
                var blockBlob = GetBlockBlobReference(connectionString, containerName, fileName);
                return await blockBlob.ExistsAsync();
            }
    
            private static CloudBlockBlob GetBlockBlobReference(string connectionString, string containerName, string fileName)
            {
                return CloudStorageAccount
                    .Parse(connectionString)
                    .CreateCloudBlobClient()
                    .GetContainerReference(containerName)
                    .GetBlockBlobReference(fileName);
            }
    
        }
    }
    

    测试结果:

    【讨论】:

    • @VSorokoumov,你能帮我把我的帖子标记为答案吗?谢谢:)
    猜你喜欢
    • 1970-01-01
    • 2012-04-03
    • 2013-10-05
    • 2015-06-05
    • 2015-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-08
    相关资源
    最近更新 更多