How download blob with SAS url using javascript?

Amruthavarshini M 46 Reputation points
2021-07-12T17:01:44.797+00:00

Hello all,

I am trying to download the file stored in azure blob to local machine using javascript.

I am using this url -

Url = https://storageaccount.blob.core.windows.net/containerName/blobName?SASToken

I am using javascript client library, which include function "blobService.getUrl()".

But its not providing correct url, i am getting authorization header not formed correctly error.

I don't want to use azureblob Node.js library as i have to install bundler.

I used below code

function getUrlOfAttachmentFileFromBlob(new_fileurl,new_fileName) {

var blobUri = 'https://' + 'Storage_Account_Name' + '.blob.core.windows.net';
var containerName = 'trial';
var sas_token = 'sastoken' ;

var blobService = AzureStorage.Blob.createBlobServiceWithSas(blobUri, sas_token);

var downloadLink = https://storageaccount.blob.core.windows.net/containerName/blobName?SASToken;

if (downloadLink != null)
{

  downloadURI(downloadLink, new_fileName);

}
}

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

With this code, I can see the content of the file in browser.
But i dont want to see the content. I want file to be downloaded to my local machine.

I tried using azure 'get blob' rest api. It also shows the content of the file.

How can i download the file?

Azure Blob Storage
Azure Blob Storage
An Azure service that stores unstructured data in the cloud as blobs.
3,192 questions
0 comments No comments
{count} vote

3 answers

Sort by: Most helpful
  1. Savita Midgule 6 Reputation points
    2022-03-01T07:04:43.383+00:00

    if ContentDisposition property is set to attachment of a blob properties. it will get downloaded. with below code.

    var link = document.createElement("a");
    link.setAttribute('download', name);
    link.href = uri;
    document.body.appendChild(link);
    link.click();

    178801-image.png
    It worked for me.

    1 person found this answer helpful.
    0 comments No comments

  2. Sumarigo-MSFT 47,466 Reputation points Microsoft Employee Moderator
    2021-07-13T04:30:04.193+00:00

    @Amruthavarshini M Welcome to Microsoft Q&A Forum, Thank you for posting your query here!

    You can download the blob directly with the URL since it’s a simple GET and you have already provided the credential via the SAS token. No library is needed.
    For example, open this link in the browser (will prompt to download the Blob) .

    For more complicate usages, we would suggest to use our latest JavaScript storage library instead of the legacy one for better support.
    @azure/storage-blob - npm (npmjs.com)

    Hope this helps!
    Kindly let us know if the above helps or you need further assistance on this issue.

    ------------------------------------------------------------------------------------------------------------------------------------------

    Please do not forget to "Accept the answer” and “up-vote” wherever the information provided helps you, this can be beneficial to other community members.


  3. Anvaria, Aman 1 Reputation point
    2021-11-18T22:31:28.943+00:00

    Hi,

    Have you got any solution, I am also trying to achieve the same thing but file is not downloading it is just opening in a new tab.
    Could you help ?


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.