简体   繁体   English

无法从 Azure IOT 集线器下载容器中的所有 Blob 并出现错误:无法读取未定义的属性“on”

[英]Unable to Download all the Blobs in a container from Azure IOT Hub and getting error: Cannot read property 'on' of undefined

I have been trying to download 5 files uploaded as blobs already in azure cloud in container: updateappnew via a nodejs script.我一直在尝试通过 nodejs 脚本在容器中的 azure 云中下载 5 个已作为 blob 上传的文件:updateappnew。 However I am getting error as in the image Cannot read property 'on' of undefined after the first blob of the five gets downloaded.但是,在下载了五个中的第一个 blob 后,我收到了图像无法读取未定义的属性“on”的错误。 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.我在这里分享我的代码,并一直希望找到问题实际发生的位置以及如何修改它以一个接一个地下载所有 blob。

const { BlobServiceClient } = require('@azure/storage-blob');

// A helper function used to read a Node.js readable stream into a string // 一个帮助器 function 用于将 Node.js 可读 stream 读入字符串

  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 // 在 Node.js 中,通过访问 downloadBlockBlobResponse.readableStreamBody 获取下载数据 // 在浏览器中,通过访问 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:以下是下载 blob 的示例代码:

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此外,我们在 GIT 中有一个博客,其中我们有完整的项目和步骤

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM