URL对于文本文件加载错误返回

URL对于文本文件加载错误返回

本文介绍了Azure Blob URL对于文本文件加载错误返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的问题所在,当我从Azure资源管理器复制时,我要加载到字符串变量中的文件的工作路径可以正常工作.

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.

当我尝试通过代码进行操作时:

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

这是GetAzureBlob的代码:

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

请注意,我的返回blob的方法将容器作为url的一部分,而来自azure Explorer的路径却没有.

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.

目录树(即使从技术上讲,即使在Azure中也不是一个)也是mystorageaccountname/files/emailtemplates/filename.任何解决方案表示赞赏.

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

推荐答案

请更改以下代码行:

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

var blob = sampleContainer.GetBlockBlobReference(filename);

根据您的注释:

据我了解,您的容器名称为files.使用container.GetBlockBlobReference构造时,无需在name参数中再次指定容器名称.图书馆会负责处理.

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.

这篇关于Azure Blob URL对于文本文件加载错误返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 00:18