download from blob when given its uri fails in c#

旧时模样 提交于 2019-12-12 05:29:33

问题


I'm trying to download a file from Azure blob and save it locally, but it seems to fail. Here's the relevant code:

    var blobClientCode = client.CreateCloudBlobClient();
    string codeUri = "https://???.blob.core.windows.net/...../mycode.exe";
    using (var codeContent = File.OpenWrite("C:\\code.exe")) {
        blobClientCode.GetBlockBlobReference(codeUri).DownloadToStream(codeContent);
    }

I get an error in which the container doens't exist. What am I doing wrong?


回答1:


Try getting a reference to the container first then defining the CloudBlockBlob from this using just the relative path.

This is the code that works for me:

CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference("myContainerName");

CloudBlockBlob blockBlob = container.GetBlockBlobReference("/subfolder/filename.exe");
using (fileStream == System.IO.File.OpenWrite("C:\code.exe")) {
    blockBlob.DownloadToStream(fileStream);
}


来源:https://stackoverflow.com/questions/15992989/download-from-blob-when-given-its-uri-fails-in-c-sharp

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