简体   繁体   English

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

[英]How to download an Azure BLOB Storage file via URL

We've created a folder structure on Azure Storage like below: 我们在Azure存储上创建了一个文件夹结构,如下所示:

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

We have the URL of the stored .pdf files. 我们具有存储的.pdf文件的URL。 We don't want to hard code any container name, just download the file using its URL. 我们不想硬编码任何容器名称,只需使用其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: 但是我们在这里收到“ 400错误请求”:

 blockBlob.DownloadToStream(ms, null, blobRequestOptions);

How can we download an Azure BLOB Storage file using only its URL? 我们如何仅使用URL下载Azure BLOB存储文件?

GetBlockBlobReference takes the filename as an argument in its constructor, not the URL. GetBlockBlobReference将文件名作为其构造函数而不是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: 为了通过URL下载Azure BLOB存储项目,您需要使用该项目的URL自己实例化CloudBlockBlob

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

This blob can then be downloaded with the code you originally posted. 然后可以使用您最初发布的代码下载该blob

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM