使用Azure Storage Blob的React Js

9jyewag0  于 2023-08-07  发布在  React
关注(0)|答案(1)|浏览(124)

我有问题,从Azure存储帐户下载文件,使用React JS。我通过npm install安装了@azure/storage-blob,并导入了它。

const storageAccount = '<storage-account-name>';
const sas = '<sas-token>'
const blobService = new BlobServiceClient(
      `https://${storageAccount}.blob.core.windows.net/${sas}`
);

const containerClient = blobService.getContainerClient('<container-name>');
const blobClient = containerClient.getBlockBlobClient('<file-name>');
blobClient.download().then(val => console.log(val));

字符串
当我尝试使用readableStreamBody来下载Microsoft示例https://learn.microsoft.com/en-us/azure/storage/blobs/storage-blob-download-javascript#download-to-a-string中提到的文件时,我可以访问帐户,但我总是将readableStreamBody检索为undefined。
x1c 0d1x是否需要为Azure配置任何设置?
先谢谢你了。
我期待readableStreamBody的值,所以我可以下载文件。

uqzxnwby

uqzxnwby1#

考虑到您使用ReactJS下载blob(换句话说,在浏览器中使用SDK),blob内容在blobBody中可用。从您共享的文档链接:


的数据
请尝试以下操作:

// Download and convert a blob to a string
const downloadBlockBlobResponse = await blobClient.download();
const downloaded = await blobToString(await downloadBlockBlobResponse.blobBody);
console.log(
  "Downloaded blob content",
  downloaded
);

async function blobToString(blob: Blob): Promise<string> {
  const fileReader = new FileReader();
  return new Promise<string>((resolve, reject) => {
    fileReader.onloadend = (ev: any) => {
      resolve(ev.target!.result);
    };
    fileReader.onerror = reject;
    fileReader.readAsText(blob);
  });
}

字符串
代码参考:https://learn.microsoft.com/en-us/javascript/api/%40azure/storage-blob/blobclient?view=azure-node-latest#@azure-storage-blob-blobclient-download。

相关问题