3

enter code hereI am trying to download the file stored in azure blob using javascript and getting this error.

Here is my code

function getUrlOfAttachmentFileFromBlob(new_fileurl,new_fileName) {
  var fileHyperlink = '';
  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);
  //.withFilter(new AzureStorage.Blob.ExponentialRetryPolicyFilter());

  var downloadLink = blobService.getUrl(new_fileName, new_fileurl.replace('/'+containerName+'/',''), sas_token);

  if (downloadLink != null)
  {
      alert("Link " + downloadLink);
      downloadURI(downloadLink, new_fileName);
  }
}

I am not generating shared signature and sas token here. I am directly assigning the sas token to the variable.

When I try to open the link in browser I am getting this error

AuthenticationFailed</Code><Message>Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.
RequestId:504f2ef5-f01e-0021-0e02-74e194000000
Time:2021-07-08T14:09:47.9951927Z</Message><AuthenticationErrorDetail>sp is mandatory. Cannot be empty</AuthenticationErrorDetail></Error>

I am not passing any headers in my code.

What i am missing here?

SAS Token = ?sp=r&st=2021-07-08T12:19:08Z&se=2021-07-08T20:19:08Z&spr=https&sv=2020-02-10&sr=c&

Without sig part.

This is what I am getting from geturl function =

?%3Fsv=2020-02-10&ss=bfqt&srt=sco&sp=rwdlacuptfx&se=2021-07-11T12%3A30%3A23Z&st=2021-06-11T04%3A30%3A23Z&spr=https%2chttp&

I think I am not getting correct url from this function, so i replaced that line with this

const downloadLink = blobUri +'/' + containerName + '/' + new_fileName + sas_token;

But still not able to download the file. But not getting authentication error.

Thank you...

amrutha varshini
  • 121
  • 2
  • 2
  • 7
  • Did you make sure the value of the Authorization header is formed correctly, including the signature? – Robert Harvey Jul 08 '21 at 15:20
  • 1
    @RobertHarvey - The authorization header part in the error details is kind of misleading :). The issue is with SAS token only. I have provided that as an answer. – Gaurav Mantri Jul 08 '21 at 15:21
  • Does this answer your question? [AzureStorage Blob Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature](https://stackoverflow.com/questions/24492790/azurestorage-blob-server-failed-to-authenticate-the-request-make-sure-the-value) – Michael Freidgeim Jan 26 '22 at 20:28

2 Answers2

2

The reason you're getting this error is because your SAS token is not valid. If you look closely at the error message you will see the following:

<AuthenticationErrorDetail>sp is mandatory. Cannot be empty</AuthenticationErrorDetail>

Essentially your SAS token is missing signed permissions (sp) parameter. Please check your SAS token and make sure that it is correctly formed.

Gaurav Mantri
  • 128,066
  • 12
  • 206
  • 241
  • I am providing correct sas token. But I am not getting proper sas token from this function, which is in Azure blob storage library. var downloadLink = blobService.getUrl(new_fileName, new_fileurl.replace('/'+containerName+'/',''), sas_token); – amrutha varshini Jul 08 '21 at 16:16
  • Can you edit your question and include the SAS token? You can obfuscate the `sig` portion of your SAS token. – Gaurav Mantri Jul 08 '21 at 16:17
  • Also share the downloadLink. – Gaurav Mantri Jul 08 '21 at 16:18
  • I have edited my question including sas token. Thank you – amrutha varshini Jul 08 '21 at 16:34
  • Thanks. There’s definitely an issue with your getUrl function. Is this a custom function you wrote? I don’t recall this function in the SDK. – Gaurav Mantri Jul 08 '21 at 16:51
  • Its not a custom function. I referred this link https://dmrelease.blob.core.windows.net/azurestoragejssample/samples/sample-blob.html. Using this library – amrutha varshini Jul 08 '21 at 16:57
  • @amruthavarshini - you are using a legacy Javascript library `Storage SDK v2 for JavaScript`. I recommend that you use `Storage SDK v12 for JavaScript` https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/storage – John Hanley Jul 08 '21 at 23:38
1

I resolve the related issues on shared access signature (SAS) Allowed resource types by clicking Service

Container

Object and finally generate new token works fine.

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 09 '22 at 03:21