I have been trying to download 5 files uploaded as blobs already in azure cloud in container: updateappnew via a nodejs script. However I am getting error as in the image Cannot read property 'on' of undefined after the first blob of the five gets downloaded. I am sharing my code here and have been hoping to find on where the issue is actually happening and how can it be modified to download all the blobs one after other.
const { BlobServiceClient } = require('@azure/storage-blob');
// A helper function used to read a Node.js readable stream into a string
return new Promise((resolve, reject) => {
const chunks = [];
readableStream.on("data", (data) => {
chunks.push(data.toString());
});
readableStream.on("end", () => {
resolve(chunks.join(""));
});
readableStream.on("error", reject);
});
}
async function main() {
const AZURE_STORAGE_CONNECTION_STRING = process.env.AZURE_STORAGE_CONNECTION_STRING;
console.log('Azure Blob storage v12 - JavaScript quickstart sample'); ```
// Quick start code goes here
// Create the BlobServiceClient object which will be used to create a container client
```const blobServiceClient = BlobServiceClient.fromConnectionString(AZURE_STORAGE_CONNECTION_STRING);```
// Create a unique name for the container
```const containerName = 'updateappnew';
console.log('\nFinding Container for updating file...');
console.log('\t', containerName);```
// Get a reference to a container
```const containerClient = blobServiceClient.getContainerClient(containerName);
console.log('\nListing blobs...');```
// List the blob(s) in the container.
```for await (const blob of containerClient.listBlobsFlat()) {
console.log('\t', blob.name);
const blockBlobClient = containerClient.getBlockBlobClient(blob.name);
}```
// List blobs
```i = 1;
for await (const blob of containerClient.listBlobsFlat()) {
console.log(`Blob ${i++}: ${blob.name}`);
const blockBlobClient = containerClient.getBlockBlobClient(blob.name);
const downloadBlockBlobResponse = await blockBlobClient.downloadToFile(`/home/administrator/file_${i-1}`);
console.log('\nDownloaded blob content...');
console.log('\t', await streamToString(downloadBlockBlobResponse.readableStreamBody));
}
// In Node.js, get downloaded data by accessing downloadBlockBlobResponse.readableStreamBody // In browsers, get downloaded data by accessing downloadBlockBlobResponse.blobBody
}
main().then(() => console.log('Done')).catch((ex) => console.log(ex.message));
This error usually occurs when the accessed value does not exists. make sure to check the value within its condition.
Below is the sample code to download blobs:
const downloadResponse = await blockBlobURL.download(aborter, 0);
const downloadedContent = await streamToString(downloadResponse.readableStreamBody);
console.log(`Downloaded blob content: "${downloadedContent}"`);
Also we have a blog in GIT where we have the complete project with steps