Created
April 22, 2020 13:43
-
-
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")
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
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
I hope this is helpful for the reader who is searching "How to use generateAccountSASQueryParameters" on Google
seem like this still doesn't work, let me fix it...
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
The final result is SAS, for example:
Browser can use this SAS. for example: