diff --git a/README.md b/README.md index 5fbcefe..54bf5f2 100644 --- a/README.md +++ b/README.md @@ -5,3 +5,22 @@ This is a set of helpers for node. They're written for quick reuse rather than r My use is primarily in quicker one off scripts that sometime morph into something long lasting... +## Azure Storage + +Get blob from Azure example, downloads `bar.jpg` from `foo` container to `baz.jgp` + +```javascript +const AzureStorage = require('./src/azure.js') + + +let storageAccountName = 'devstoreaccount1' +let storageAccountKey = 'Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==' +let options = {cloudName: 'Azurite'} + +let azure = new AzureStorage(storageAccountName,storageAccountKey,options) + +let containerName = 'foo' +let blob = 'bar.jpg' +let file = 'baz.jpg' +await azure.downloadBlobToFile(containerName,blob,file) +``` \ No newline at end of file diff --git a/src/azure.js b/src/azure.js index 970458d..dbceb00 100644 --- a/src/azure.js +++ b/src/azure.js @@ -163,6 +163,29 @@ getStorageQueueSignedURL(queueUrl,options) { }); } + /** + * Downloads a blob to a local file + * + * @param {string} containerName the name of the container to download the blob from + * @param {string} blob The blob to download + * @param {string} file The path to the location to write the file + */ + async downloadBlobToFile(containerName,blob,file) { + const blobService = azure.createBlobService(this.storageAccountName, this.storageAccountKey, this.host('blob',this.cloudName)); + const options = { + access: 'container' + }; + + blobService.getBlobToLocalFile(containerName,blob,file,options,function(error,response){ + if( error) { + console.error(error.message) + } else { + // console.log(response) + console.log(`${response.name} downloaded to ${file}`) + } + }); + } + } module.exports = AzureStorage \ No newline at end of file