如何通过URL下载Azure BLOB存储文件 [英] How to download an Azure BLOB Storage file via URL

查看:557
本文介绍了如何通过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.

这篇关于如何通过URL下载Azure BLOB存储文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆