How to Download a File to Browser from Azure Blob Storage

How to download a file to browser from Azure Blob Storage

While blob content may be streamed through a web server, and along to the end user via browser, this solution puts load on the web server, both cpu and NIC.

An alternative approach is to provide the end user with a uri to the desired blob to be downloaded, which they may click in the html content. e.g. https://myaccount.blob.core.windows.net/mycontainer/myblob.ext.

The issue with this is if the content is private, since a uri such as the one above won't work unless using public blobs. For this, you can create a Shared Access Signature (or server-stored Policy), which then results in a hashed querystring appended to the uri. This new uri would be valid for a given length of time (10 minutes, for example).

Here's a small example of creating an SAS for a blob:

var sasConstraints = new SharedAccessBlobPolicy();
sasConstraints.SharedAccessStartTime = DateTime.UtcNow.AddMinutes(-5);
sasConstraints.SharedAccessExpiryTime = DateTime.UtcNow.AddMinutes(10);
sasConstraints.Permissions = SharedAccessBlobPermissions.Read;

var sasBlobToken = blob.GetSharedAccessSignature(sasConstraints);

return blob.Uri + sasBlobToken;

Note that the start time is set to be a few minutes in the past. This is to deal with clock-drift. Here is the full tutorial I grabbed/modified this code sample from.

By using direct blob access, you will completely bypass your VM/web role instance/web site instance (reducing server load), and have your end-user pull blob content directly from blob storage. You can still use your web app to deal with permissioning, deciding which content to deliver, etc. But... this lets you direct-link to blob resources, rather than streaming them through your web server.

Download file from Azure Blob Storage client side

This is about downloading to the dir that you assign using Python.

from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient

filename = "test.txt"
container_name="test-container"
path="D:\\demo\\download\\" #the dir that you download to

blob_service_client = BlobServiceClient.from_connection_string("{Connection string}")
container_client=blob_service_client.get_container_client(container_name)
blob_client = container_client.get_blob_client(filename)

with open(path + filename, "wb") as f:
data = blob_client.download_blob()
data.readinto(f)

path + filename is the file that is downloaded to the local address.

How to download an Azure BLOB Storage file via URL

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.

Azure storage blob library download blob to local machine

The downloadToFile method only available in node.js runtime, so you cannot use it to download files to local, you can refer to downloadToFile.

Sample Image

You can try the following methods to download files:

const downloadFiles = async () => {
if (fileList.selectedOptions.length > 0) {
reportStatus("downloading files...");
for (const option of fileList.selectedOptions) {
console.log("option.text", option.text);

try {
const blobClient = containerClient.getBlobClient(option.text);
console.log("BlobClient",blobClient);

const downloadBlockBlobResponse = await blobClient.download();
const url =window.URL.createObjectURL(await downloadBlockBlobResponse.blobBody);

downloadURI(url,option.text)
console.log("Downloaded blob to string content", url);
}
catch (error) {
reportStatus(error.message);
}
}
reportStatus("Done.");
listFiles();
} else {
reportStatus("No files selected.");
}
};

downloadButton.addEventListener("click", downloadFiles);

function downloadURI(uri, name)
{
var link = document.createElement("a");
link.download = name;
link.href = uri;
link.click();
};


Related Topics



Leave a reply



Submit