Skip to content

Commit

Permalink
feat: adds download blob from azure helper
Browse files Browse the repository at this point in the history
  • Loading branch information
beauraines committed Jun 29, 2023
1 parent e6bf106 commit 08a1571
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
```
23 changes: 23 additions & 0 deletions src/azure.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 08a1571

Please sign in to comment.