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:
Portal:
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.