I am unable to upload a file using azure-blob js sdk given a sas token

Ian Davis 0 Reputation points
2025-04-30T20:11:29.19+00:00

When trying to upload via JS, I get this error:

RestError: Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.

RequestId:a441c1f7-001e-0021-2b0b-bad5b1000000

Why? Here is the sas url: https://zcast.blob.core.windows.net/episodes/temp-queued-files/PvweJkDoRT.mp3?sv=2023-08-03&st=2025-04-30T20%3A04%3A39Z&se=2025-05-01T20%3A04%3A39Z&sr=b&sp=racw&sig=dYv

Azure Blob Storage
Azure Blob Storage
An Azure service that stores unstructured data in the cloud as blobs.
3,168 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Venkatesan S 2,260 Reputation points Microsoft External Staff Moderator
    2025-05-20T08:42:24.66+00:00

    Hi @Ian Davis

    Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature

    The above error occurs when your Azure Blob Storage rejected your request due to an invalid or improperly formatted Shared Access Signature (SAS) token.

    You can use the below code which will create Shared Access Signature (SAS) token and will upload the file in the Azure Blob storage using Azure JavaScript SDK.

    Code:

    // Replace with your own values
    const accountName = "xxxxx";
    const accountKey = "xxxxx";
    const containerName = "xxx";
    const blobName = "test/PvweJkDoRT.mp3";
    const filePath = "xxxxxxx";
    
    const sharedKeyCredential = new StorageSharedKeyCredential(accountName, accountKey);
    const blobServiceClient = new BlobServiceClient(
      `https://${accountName}.blob.core.windows.net`,
      sharedKeyCredential
    );
    const containerClient = blobServiceClient.getContainerClient(containerName);
    
    function getBlobSasUri(containerClient, blobName, sharedKeyCredential, storedPolicyName = null) {
      const sasOptions = {
        containerName: containerClient.containerName,
        blobName: blobName
      };
    
      if (storedPolicyName == null) {
        sasOptions.startsOn = new Date();
        sasOptions.expiresOn = new Date(new Date().valueOf() + 3600 * 1000); // 1 hour
        sasOptions.permissions = BlobSASPermissions.parse("cw"); // create + write
      } else {
        sasOptions.identifier = storedPolicyName;
      }
    
      const sasToken = generateBlobSASQueryParameters(sasOptions, sharedKeyCredential).toString();
      return `${containerClient.getBlockBlobClient(blobName).url}?${sasToken}`;
    }
    
    // Upload the file using fetch + PUT + SAS URL
    async function uploadViaSasUrl(sasUrl, filePath) {
      const fileBuffer = fs.readFileSync(filePath);
      const fileSize = fs.statSync(filePath).size;
    
      const response = await fetch(sasUrl, {
        method: "PUT",
        headers: {
          "x-ms-blob-type": "BlockBlob",
          "Content-Length": fileSize,
          "Content-Type": "audio/mpeg"
        },
        body: fileBuffer
      });
    
      if (response.ok) {
        console.log("Upload successful.");
      } else {
        const errText = await response.text();
        console.error("Upload failed:", response.status, errText);
      }
    }
    
    (async () => {
      const sasUrl = getBlobSasUri(containerClient, blobName, sharedKeyCredential);
      console.log("SAS URL:", sasUrl);
      await uploadViaSasUrl(sasUrl, filePath);
    })();
    

    Output:enter image description here

    Portal:enter image description here

    Reference:

    Create a service SAS for a container or blob with JavaScript - Azure Storage | Microsoft Learn

    Hope this answer helps! please let us know if you have any further queries. I’m happy to assist you further.

    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.

    0 comments No comments

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.