我正在尝试仅使用纯Javascript和SDK来执行此操作。我不正在使用Node.js。我正在将我的应用程序从SDK azure-storage-js-v10的v2转换为v10
azure-storage.blob.js捆绑文件与UMD兼容 标准,如果未找到模块系统,则遵循全局变量 将被导出: azblob
我的代码在这里:
const serviceURL = new azblob.ServiceURL(`https://${account}.blob.core.windows.net${accountSas}`, pipeline);
const containerName = "container";
const containerURL = azblob.ContainerURL.fromServiceURL(serviceURL, containerName);
const blobURL = azblob.BlobURL.fromContainerURL(containerURL, blobName);
const downloadBlobResponse = await blobURL.download(azblob.Aborter.none, 0);
downloadBlobResponse如下所示: downloadBlobResponse
使用v10,如何将downloadBlobResponse转换为新的Blob,以便可以在FileSaver saveAs()函数中使用它?
在azure-storage-js-v2中,此代码适用于较小的文件:
let readStream = blobService.createReadStream(containerName, blobName, (err, res) => {
if (error) {
// Handle read blob error
}
});
// Use event listener to receive data
readStream.on('data', data => {
// Uint8Array retrieved
// Convert the array back into a blob
var newBlob = new Blob([new Uint8Array(data)]);
// Saves file to the user's downloads directory
saveAs(newBlob, blobName); // FileSaver.js
});
我已尽一切努力使v10正常工作,任何帮助将不胜感激。
谢谢
答案 0 :(得分:0)
您需要通过等待blobBody来获取尸体。
downloadBlobResponse = await blobURL.download(azblob.Aborter.none, 0);
// data is a browser Blob type
const data = await downloadBlobResponse.blobBody;
答案 1 :(得分:0)
感谢Xiaoning Liu!
我仍在学习异步javascript函数和promise。猜猜我只是想念另一个“ 等待”。我看到“ downloadBlobResponse.blobBody”是一个承诺,也是一个blob类型,但是,我不知道为什么它不能转换为新的blob。我不断收到“ 迭代器getter不可调用”错误。
这是我最后的工作解决方案:
// Create a BlobURL
const blobURL = azblob.BlobURL.fromContainerURL(containerURL, blobName);
// Download blob
downloadBlobResponse = await blobURL.download(azblob.Aborter.none, 0);
// In browsers, get downloaded data by accessing downloadBlockBlobResponse.blobBody
const data = await downloadBlobResponse.blobBody;
// Saves file to the user's downloads directory
saveAs(data, blobName); // FileSaver.js
答案 2 :(得分:0)
感谢 Mike Coop 和刘晓宁!
我正忙于制作一个 Vuejs 插件来从存储帐户下载 blob。感谢您,我能够完成这项工作。
var FileSaver = require('file-saver');
const { BlobServiceClient } = require("@azure/storage-blob");
const downloadButton = document.getElementById("download-button");
const downloadFiles = async() => {
try {
if (fileList.selectedOptions.length > 0) {
reportStatus("Downloading files...");
for await (const option of fileList.selectedOptions) {
var blobName = option.text;
const account = '<account name>';
const sas = '<blob sas token>';
const containerName = '< container name>';
const blobServiceClient = new BlobServiceClient(`https://${account}.blob.core.windows.net${sas}`);
const containerClient = blobServiceClient.getContainerClient(containerName);
const blobClient = containerClient.getBlobClient(blobName);
const downloadBlockBlobResponse = await blobClient.download(blobName, 0, undefined);
const data = await downloadBlockBlobResponse.blobBody;
// Saves file to the user's downloads directory
FileSaver.saveAs(data, blobName); // FileSaver.js
}
reportStatus("Done.");
listFiles();
} else {
reportStatus("No files selected.");
}
} catch (error) {
reportStatus(error.message);
}
};
downloadButton.addEventListener("click", downloadFiles);