gpt4 book ai didi

javascript - 列出来自 Azure Blob 存储的 Blob 和 Node.js 中的 Readstream

转载 作者:行者123 更新时间:2023-12-03 01:29:56 24 4
gpt4 key购买 nike

这是我如何列出 blob 的代码。

已编辑!!!

------------------------------------编辑---------- --------------------------------

我编辑了代码并在此 TypeScript 中重写:

    const blobServiceClient = new BlobServiceClient(`https://${accountName}.blob.core.windows.net?${sasToken}`,
pipeline)
const containerClient = blobServiceClient.getContainerClient(containerName)
console.log(containerClient)
if (!containerClient.exists()) {
console.log("the container does not exit")
await containerClient.create()

}
const client = containerClient.getBlockBlobClient(this.currentFile.name)

//name of uploded blob
console.log(this.currentFile.name)
//metaata from the blob
console.log(client)

//List each blobs in the container
for await (const blob of containerClient.listBlobsFlat()) {
console.log('\t', blob.name);
const blockBlobClient = containerClient.getBlockBlobClient(blob.name);
const downloadBlockBlobResponse = await blockBlobClient.download(0);
console.log('\nDownloaded blob content...');
console.log('\t', await streamToString(downloadBlockBlobResponse.readableStreamBody));
//end of loop
}



async function streamToString(readableStream) {
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);
});
}

但是后来我在浏览器中收到一个错误,调用:

ERROR Error: Uncaught (in promise): TypeError: Cannot read property'on' of undefined TypeError: Cannot read property 'on' of undefined

最佳答案

通过调用 download 方法下载 blob。示例代码包含一个名为 streamToString 的辅助函数,用于将 Node.js 可读流读取为字符串

将此代码添加到主函数的末尾:

const blockBlobClient = containerClient.getBlockBlobClient(blob.name);
const downloadBlockBlobResponse = await blockBlobClient.download(0);
console.log('\nDownloaded blob content...');
console.log('\t', await streamToString(downloadBlockBlobResponse.readableStreamBody));

在主函数之后添加此辅助函数:

// A helper function used to read a Node.js readable stream into a string
async function streamToString(readableStream) {
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);
});
}

更多详情可以引用这个article .

关于javascript - 列出来自 Azure Blob 存储的 Blob 和 Node.js 中的 Readstream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63395738/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com