Arnob
Arnob

Reputation: 527

Error generating SAS url for nodejs using azure storage

I am trying to generate a SAS url that I can send back to the client so that client can use the url to PUT the file directly into storage:

Following is the endpoint:(update 1):

   router.get('/get-upload-url', async (req, res) => {
     const blobName = 'dummyBlobName.jpeg';

     const blobServiceClient = new BlobServiceClient(`https://${ACCOUNT_NAME}.blob.core.windows.net`, new StorageSharedKeyCredential(ACCOUNT_NAME, SAS_TOKEN));
     const containerClient = blobServiceClient.getContainerClient(CONTAINER_NAME);
     const blockBlobClient = containerClient.getBlockBlobClient(blobName);

     const startDate = new Date();
     const expiryDate = new Date(startDate);
     expiryDate.setMinutes(startDate.getMinutes() + 100);

     const sasToken = generateBlobSASQueryParameters({
       containerName: CONTAINER_NAME,
       blobName,
       permissions: BlobSASPermissions.parse('cw'),
       startsOn: startDate,
       expiresOn: expiryDate
     }, blobServiceClient.credential);

     const sasUrl = `${blockBlobClient.url}?${sasToken}`;

     res.json({ sasUrl });
   });

   app.use('/', router);

   app.listen(PORT, () => {
     console.log(`Server running on port ${PORT}`);
   });

And the curl command I am trying:

curl -X PUT -T ~/Downloads/dp.jpeg \
-H "x-ms-date: $(date -u)" \
-H "x-ms-blob-type: BlockBlob" \
"https://appstrolabstoragedev.blob.core.windows.net/imgx/dummyBlobName.jpeg?sv=2024-08-04&st=2024-07-29T12%3A52%3A55Z&se=2024-07-29T14%3A32%3A55Z&sr=b&sp=cw&sig=jsYb6VOm57avYu2eS2H26Q6vMkzSjdnjMI%2B80xygQ%2F8%3D"

Error I am getting:

<?xml version="1.0" encoding="utf-8"?><Error><Code>AuthenticationFailed</Code><Message>Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.
RequestId:beee57d1-201e-005b-7db6-e1f5de000000
Time:2024-07-29T12:53:12.7097016Z</Message><AuthenticationErrorDetail>Signature did not match. String to sign used was cw
2024-07-29T12:52:55Z
2024-07-29T14:32:55Z
/blob/appstrolabstoragedev/imgx/dummyBlobName.jpeg
2024-08-04
b
</AuthenticationErrorDetail></Error>% 

I am not sure what I am doing wrong. I want to create a URL, send it back to frontend so that client can use it to upload files directly to azure blob storage.

Error after updating blobName to 'dummyBlobName.jpeg'

<?xml version="1.0" encoding="utf-8"?><Error><Code>AuthenticationFailed</Code><Message>Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature. RequestId:03849c77-001e-0087-46c0-e15f80000000 Time:2024-07-29T14:06:41.0301691Z</Message><AuthenticationErrorDetail>Signature did not match. String to sign used was cw 2024-07-29T14:05:50Z 2024-07-29T15:45:50Z /blob/appstrolabstoragedev/imgx/dummyBlobName.jpeg    2024-08-04 b       </AuthenticationErrorDetail></Error>%

Upvotes: 0

Views: 48

Answers (1)

Gaurav Mantri
Gaurav Mantri

Reputation: 136346

The reason you are getting this error is because you are generating a SAS token for a blob named dummyBlobName.txt, however you are uploading a different blob (dummyBlobName.jpeg) using that token.

When generating a SAS URL for the blob resource (sr=b), the blob name must match. To fix this issue, please create a SAS token with dummyBlobName.jpeg blob name.

Other option would be to create a SAS token for the blob container (your SAS token will have signed resource as c). Then you can use that SAS token to upload any blob in that container.

Upvotes: 0

Related Questions