Last active
February 7, 2020 22:23
-
-
Save MForMarlon/c0efa637dd227431d8a0b35c89bed3ec to your computer and use it in GitHub Desktop.
Downloading blobs from Azure Blob Storage
This file contains 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
/*** | |
* The ABS JS examples from Microsoft demonstrate how to download blobs as text. | |
* This example shows a generalized solution. | |
* Assumes node v12 and express framework | |
***/ | |
const { BlobServiceClient } = require('@azure/storage-blob'); | |
const STORAGE_CONN = "your_azure_storage_connection_string"; | |
const CONTAINER_NAME = "your_azure_container_name"; | |
module.exports = app => { | |
app.get('/rest/path/to/blob', async (req, res) => { | |
const blobName = 'theNameOfYourBlob'; | |
const blobServiceClient = await BlobServiceClient.fromConnectionString(STORAGE_CONN); | |
const containerClient = await blobServiceClient.getContainerClient(CONTAINER_NAME); | |
const blockBlobClient = await containerClient.getBlockBlobClient(blobName); | |
try { | |
const downloadBlockBlobResponse = await blockBlobClient.download(0); | |
downloadBlockBlobResponse.readableStreamBody.pipe(res); | |
} catch (err) { | |
res.json({payload: null, error: err}); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment