Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何通过URL下载Azure BLOB存储文件_C#_Azure_Azure Storage_Azure Storage Blobs - Fatal编程技术网

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

C# 如何通过URL下载Azure BLOB存储文件,c#,azure,azure-storage,azure-storage-blobs,C#,Azure,Azure Storage,Azure Storage Blobs,我们在Azure存储上创建了一个文件夹结构,如下所示: parentcontainer -> childcontainer -> {pdffiles are uploaded here} 我们有存储的.pdf文件的URL。我们不想硬编码任何容器名称,只需使用其URL下载文件即可 我们目前的尝试是: CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(StorageConnectionString);

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

parentcontainer -> childcontainer -> {pdffiles are uploaded here}
我们有存储的
.pdf
文件的URL。我们不想硬编码任何容器名称,只需使用其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个错误请求”:

如何仅使用Azure BLOB存储文件的URL下载该文件?

将文件名作为其构造函数中的参数,而不是URL

为了通过Azure BLOB存储项目的URL下载该项目,您需要使用该项目的URL自己实例化一个存储项目:

var blob = new CloudBlockBlob(new Uri(pdfFileUrl), cloudStorageAccount.Credentials);
这个
blob
可以与您最初发布的代码一起下载

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