Azure blob URL 错误地返回用于文本文件加载

Azure blob URL comes back incorrectly for text file load

这就是我的问题,当从 Azure Explorer 复制时,我尝试加载到字符串变量中的文件的工作路径工作正常。

Working: https://container.blob.core.windows.net/files/emailtemplates/EmailMaster.html

当我尝试通过代码完成时:

 [TestMethod]
    public void TestMethod3()
    {
        string templateHtml;
        var blob = AzureStorageMethods.GetAzureBlob(AzureFileFolder + "EmailMaster.html");
        using (var memoryStream = new MemoryStream())
        {
            blob.DownloadToStream(memoryStream);
            templateHtml = Encoding.UTF8.GetString(memoryStream.ToArray());
        }
        Assert.AreNotEqual(0, templateHtml.Length);
    }

这是 GetAzureBlob 的代码:

 public static CloudBlockBlob GetAzureBlob(string filename)
    {
        var creds = ConfigurationManager.AppSettings["azurestorageconn"];
        var storageAccount = CloudStorageAccount.Parse(creds);
        var client = storageAccount.CreateCloudBlobClient();
        //create a blob container and make it publicly accessibile
        var sampleContainer = client.GetContainerReference(ConfigurationManager.AppSettings["azurecontainer"]);
        sampleContainer.CreateIfNotExists();
        sampleContainer.SetPermissions(new BlobContainerPermissions()
        {
            PublicAccess = BlobContainerPublicAccessType.Blob
        });
        var blob = sampleContainer.GetBlockBlobReference(@"files\" + filename);
        return blob;
    }

端点路径错误,下载流失败。 它返回为

Not Working: https://container.blob.core.windows.net/container/files/emailtemplates/EmailMaster.html

请注意,我对 return blob 的方法将容器作为 url 的一部分,而来自 azure explorer 的路径则没有。

我看不出有什么办法可以解决这个问题。我试过直接访问文件容器,但我要么做错了,要么不可行。

目录树(尽管从技术上讲 Azure 中没有目录树)是 mystorageaccountname/files/emailtemplates/filename。任何解决方案表示赞赏。

请更改这行代码:

var blob = sampleContainer.GetBlockBlobReference(@"files\" + filename);

var blob = sampleContainer.GetBlockBlobReference(filename);

根据您的说明:

The directory tree (even though there technically isn't one in Azure) is mystorageaccountname/files/emailtemplates/filename.

据我了解,您的容器名称是 files。使用 container.GetBlockBlobReference 构造时,不需要在 name 参数中再次指定容器名称。它将由图书馆处理。