Skip to content

Instantly share code, notes, and snippets.

@1c7
Created April 22, 2020 13:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 1c7/ae1dcab2088c51051fb6ee2f7da93eb4 to your computer and use it in GitHub Desktop.
Save 1c7/ae1dcab2088c51051fb6ee2f7da93eb4 to your computer and use it in GitHub Desktop.
Azure generateAccountSASQueryParameters example (Node.js demo) (Node v12.11.0) (package.json "@azure/storage-blob": "12.1.1")
module.exports.azure_sas = function () {
var accountName = '<storage account name>'
var accountKey = '<account key like: xxxTAujFeBxdic/6a1SgEaoeByhqQeUpRdWaQ8Y7kRJ1rhwbXEoW8WAg036B4jkzTE9AYmtKnFu03dt6g1jJyA==>'
var sharedKeyCredential = new StorageSharedKeyCredential(accountName, accountKey)
var last_day_of_this_year = new Date(new Date().getFullYear(), 11, 31)
var resource_types = new AccountSASResourceTypes()
resource_types.container = true
resource_types.object = true
resource_types.service = true
var permission = new AccountSASPermissions()
permission.create = true
permission.delete = true
permission.list = true
permission.write = true
var sas_signature_val = {
expiresOn: last_day_of_this_year,
services: {
blob: true
},
permissions: permission,
resourceTypes: resource_types
}
var SASQueryParameters = generateAccountSASQueryParameters(sas_signature_val, sharedKeyCredential)
var string = SASQueryParameters.toString() // sv=2019-07-07&se=2020-12-30T16%3A00%3A00Z&sp=wdlc&sig=v6x7gATh3B2D5h1H5pyEd6Q2ZXmnxd2tjZEBsF7XuWc%3D
return string
}
@1c7
Copy link
Author

1c7 commented Apr 22, 2020

The final result is SAS, for example:

sv=2019-07-07&se=2020-12-30T16%3A00%3A00Z&sp=wdlc&sig=v6x7gATh3B2D5h1H5pyEd6Q2ZXmnxd2tjZEBsF7XuWc%3D

Browser can use this SAS. for example:

function azure_blob_service_client() {
  const account = "<storage account name>";
  const sas = "?<sas string, don't forget the question mark at beginning, because it's HTTP GET parameter>"

  const blobServiceClient = new BlobServiceClient(
    `https://${account}.blob.core.windows.net${sas}`
  );
  return blobServiceClient
}

async function azure_container_exist(container_name) {
  var blobServiceClient = azure_blob_service_client();
  let iter = await blobServiceClient.listContainers();
  for await (const container of iter) {
    if (container.name == container_name) {
      return true;
    }
  }
  return false;
}

@1c7
Copy link
Author

1c7 commented Apr 22, 2020

Explain: This demo is for @azure/storage-blob

use Node.js get a SAS, and then the browser can use this to upload file to Azure Container

one more example

  var containerName = 'tern'
  var blobServiceClient = azure_blob_service_client();
  const containerClient = blobServiceClient.getContainerClient(containerName);
  const blockBlobClient = containerClient.getBlockBlobClient(this.file.name);

  function onProgress(ev) {
    console.log(ev)
  }

  const uploadBlobResponse = await blockBlobClient.uploadBrowserData(this.file, {
    onProgress: onProgress
  })

this.file is just a standard File object you can get in Browser

@1c7
Copy link
Author

1c7 commented Apr 22, 2020

I hope this is helpful for the reader who is searching "How to use generateAccountSASQueryParameters" on Google

@1c7
Copy link
Author

1c7 commented Apr 23, 2020

seem like this still doesn't work, let me fix it...

@1c7
Copy link
Author

1c7 commented May 31, 2020

Final Solution:

I stop playing with SAS, just use the SDK

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment