How to generate an azure blob url with SAS signature in nodejs sdk v12?
Asked Answered
S

2

8

previously (in older sdk like v2) you can generate a sas url (a signed shareable url for a blob) like following :

var azure = require('azure-storage');
var blobService = azure.createBlobService();

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

var sharedAccessPolicy = {
  AccessPolicy: {
    Permissions: azure.BlobUtilities.SharedAccessPermissions.READ,
    Start: startDate,
    Expiry: expiryDate
  }
};

var token = blobService.generateSharedAccessSignature(containerName, blobName, sharedAccessPolicy);
var sasUrl = blobService.getUrl(containerName, blobName, token);

I'm wondering how we can generate that url in sdk v12? I could not find any documentation for Sas URL in v12.

BlobUtilities and getUrl() methods also not available in v12 (in v12 there are separate packages for every module , in my case I'm using require("@azure/storage-blob");)

Thanks.

Schwenk answered 30/4, 2020 at 18:31 Comment(0)
D
15

Regarding the issue, please refer to the following code

var storage = require("@azure/storage-blob")
  const accountname ="blobstorage0516";
    const key = "";
    const cerds = new storage.StorageSharedKeyCredential(accountname,key);
    const blobServiceClient = new storage.BlobServiceClient(`https://${accountname}.blob.core.windows.net`,cerds);
    const containerName="test";
    const client =blobServiceClient.getContainerClient(containerName)
    const blobName="help.txt";
    const blobClient = client.getBlobClient(blobName);

    const blobSAS = storage.generateBlobSASQueryParameters({
      containerName, 
      blobName, 
      permissions: storage.BlobSASPermissions.parse("racwd"), 
      startsOn: new Date(),
      expiresOn: new Date(new Date().valueOf() + 86400)
    },
    cerds 
  ).toString();

    const sasUrl= blobClient.url+"?"+blobSAS;
    console.log(sasUrl);

enter image description here

Deryl answered 1/5, 2020 at 4:39 Comment(4)
This is the best answer i found on this. I tried writing the could from scratch and kept getting Signature did not match. I copied and pasted this code, changed the details and SAS url worked. I spent 6 hours figuring out this issue. Thanks a million.Primate
If you are looking to upload the Blob using the signedURL, don't forget to add x-ms-blob-type: BlockBlob in the request header, otherwise it will fail. Refer here: learn.microsoft.com/en-us/rest/api/storageservices/…Diggins
Thank you!! I also spent hours trying to fight my way through the docs. Never could get it to work. This worked!!Urey
What to do incase of a cdn?Kianakiang
U
5

You can do so by using generateBlobSASQueryParameters. For example, see the code below:

const AZURE_STORAGE_ACCOUNT = 'account-name';
const AZURE_STORAGE_ACCESS_KEY = 'account-key';
const { StorageSharedKeyCredential, BlobServiceClient, generateBlobSASQueryParameters, BlobSASPermissions } = require("@azure/storage-blob");
const sharedKeyCredential = new StorageSharedKeyCredential(AZURE_STORAGE_ACCOUNT, AZURE_STORAGE_ACCESS_KEY);


const blobServiceClient = new BlobServiceClient(
  `https://${AZURE_STORAGE_ACCOUNT}.blob.core.windows.net`,
  sharedKeyCredential
);

const containerName = 'container-name';
const blobName = 'blob-name';
const containerClient = blobServiceClient.getContainerClient(containerName);
const blockBlobClient = containerClient.getBlockBlobClient(blobName);
// const credentials = new StorageSharedKeyCredential()
const sasToken = generateBlobSASQueryParameters({
  containerName: containerName,
  blobName: blobName,
  expiresOn: new Date(new Date().valueOf() + 86400),
  permissions: BlobSASPermissions.parse("racwd")
}, sharedKeyCredential);

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

console.log(sasUrl);
Unlikely answered 1/5, 2020 at 4:39 Comment(2)
Thank you. so generateBlobSASQueryParameters is what I was need to replace sharedAccessPolicy and token call in old sdk .Schwenk
BE CAREFUL? Each letter in the parse("racwd") seem to be defined here learn.microsoft.com/en-us/javascript/api/@azure/storage-blob/…Chingchinghai

© 2022 - 2024 — McMap. All rights reserved.