如何通过URL下载Azure BLOB存储文件

如何通过URL下载Azure BLOB存储文件

问题描述:

我们已经在Azure存储上创建了一个文件夹结构,如下所示:

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

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

我们具有存储的.pdf文件的URL.我们不想硬编码任何容器名称,只需使用其URL下载文件即可.

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.

我们当前的尝试:

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

但是我们在这里收到"400错误请求":

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

 blockBlob.DownloadToStream(ms, null, blobRequestOptions);

我们如何仅使用URL下载Azure BLOB存储文件?

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

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

为了通过URL下载Azure BLOB存储项目,需要实例化

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

然后可以使用您最初发布的代码下载此blob.

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