Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to download an Azure BLOB Storage file via URL

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?

like image 326
Oxygen Avatar asked Jun 23 '19 17:06

Oxygen


People also ask

How do I get Azure blob from URL?

By default, the URL for accessing the Blob service in a storage account is https://<your account name>. blob.core.windows.net.

How do I download a folder from Azure blob storage?

You can download blobs and directories from Blob storage by using the AzCopy v10 command-line utility.

How do I download from blob?

Go to the SaveFrom.net page and paste the blob URL into the designated field. The website will show a video thumbnail, and you can choose the desired video quality. Then, select the “Download” button. You'll be asked to save the file in a specific location.


1 Answers

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.

like image 51
Tobias Tengler Avatar answered Nov 03 '22 18:11

Tobias Tengler