How to download an Azure BLOB Storage file via URL

被刻印的时光 ゝ 提交于 2019-12-11 19:45:32

问题


We've created a folder structure on Azure Storage like below:

parentcontainer -> childcontainer -> {pdffiles are uploaded here}

We have the URL of the stored .pdf files. We don't want to hard code any container name, just download the file using its URL.

Our current attempt at doing this:

CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(StorageConnectionString);
CloudBlobClient blobClient = cloudStorageAccount.CreateCloudBlobClient();
CloudBlobContainer cloudBlobContainer = blobClient.GetRootContainerReference();
CloudBlockBlob blockBlob = cloudBlobContainer.GetBlockBlobReference(pdfFileUrl);

var blobRequestOptions = new BlobRequestOptions
{
    RetryPolicy = new NoRetry()
};

// Read content
using (MemoryStream ms = new MemoryStream())
{
    blockBlob.DownloadToStream(ms, null, blobRequestOptions);
    var array = ms.ToArray();
    return ms.ToArray();
}     

But we're getting a "400 Bad Request" here:

 blockBlob.DownloadToStream(ms, null, blobRequestOptions);

How can we download an Azure BLOB Storage file using only its URL?


回答1:


GetBlockBlobReference takes the filename as an argument in its constructor, not the URL.

In order to download an Azure BLOB Storage item by its URL, you need to instantiate a CloudBlockBlob yourself using the item's URL:

var blob = new CloudBlockBlob(new Uri(pdfFileUrl), cloudStorageAccount.Credentials);

This blob can then be downloaded with the code you originally posted.



来源:https://stackoverflow.com/questions/56726380/how-to-download-an-azure-blob-storage-file-via-url

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