Azure blob URL在文本文件加载时返回错误
本文关键字:加载 返回 错误 文件 文本 blob URL Azure | 更新日期: 2023-09-27 18:03:01
这就是我的问题,我试图加载到字符串变量的文件的工作路径,当从Azure资源管理器复制时工作正常。
工作: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);
}
下面是gettazureblob的代码:
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;
}
由于端点路径错误,下载流失败。它返回为
不工作:https://container.blob.core.windows.net/container/files/emailtemplates/EmailMaster.html
请注意,我的方法返回一个blob,有容器作为url的一部分,而从azure资源管理器的路径没有。
我想不出任何办法来解决这个问题。我试过直接访问文件容器,但我要么做错了,要么是不可行的。
目录树(即使在Azure中技术上没有)是mystorageaccountname/files/emailtemplates/filename。
请更改这行代码:
var blob = sampleContainer.GetBlockBlobReference(@"files'" + filename);
var blob = sampleContainer.GetBlockBlobReference(filename);
根据你的注释:
目录树(即使在Azure中技术上没有)mystorageaccountname/文件/emailtemplates/文件名。
我理解你的集装箱名称是files
。当使用container.GetBlockBlobReference
构造时,不需要在name参数中再次指定容器名称。图书馆会处理的