Azure blob URL comes back incorrectly for text file load

三世轮回 提交于 2019-12-13 06:15:50

问题


Herein lies my problem, the working path to the file I am trying to load into a string variable, when copied from Azure Explorer works fine.

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

When I try to do it via code:

 [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);
    }

Here is the code for 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;
    }

It fails to Download the stream because the endpoint path is wrong. It comes back as

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

Note that my method to return a blob, has the container as part of the url, whereas the path from azure explorer does not.

I can't see any way to solve this. I've tried accessing the files container directly but I'm either doing it wrong or it isn't doable.

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


回答1:


Please change this line of code:

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

to

var blob = sampleContainer.GetBlockBlobReference(filename);

Based on your note:

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

It is my understanding that the name of your container is files. When you use container.GetBlockBlobReference construct, you don't need to specify the container name again in the name parameter. It will be taken care of by the library.



来源:https://stackoverflow.com/questions/39018775/azure-blob-url-comes-back-incorrectly-for-text-file-load

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!