【问题标题】:Reactjs How to download file from Azure Storage ContainerReactjs 如何从 Azure 存储容器下载文件
【发布时间】:2020-12-29 20:24:29
【问题描述】:

我正在处理reactjs/typescript 应用程序。我正在尝试从 azure storage v2 下载一些文件。下面是我应该下载文件的示例路径。在此路径中,enrichment 为容器名称,其余均为文件夹。我正在尝试从reportdocument 文件夹下载最后修改的文件。

enrichment/report/SAR-1234-56/reportdocument/file1.docs

我在下面尝试了一些东西。

@action
    public async reportDownload(sarNumber: string) {
        let storage = globals.getGlobals('StorageAccount03');
        console.log(storage);
        let containerName = globals.getGlobals('StorageAccount03ContainerName');
        let marker = undefined;
        let allUploadPromise: Array<Promise<unknown>> = [];
        const config = {
            path: `/Storage/getsastoken/?storageName=${storage}&containerName=${containerName}`,
            method: "GET",
            success: (url: any) => {

                const containerURL: ContainerURL = new ContainerURL(
                    url,
                    StorageURL.newPipeline(new AnonymousCredential()));
                 
                const listBlobsResponse =  containerURL.listBlobFlatSegment(
                    Aborter.none,
                    marker,
                );
            }
        };
        await handleRequest(config);
    }

从这里我正在努力从上述路径下载最新修改的文​​件。 有人可以帮我解决这个问题吗?任何帮助将不胜感激。谢谢

【问题讨论】:

    标签: javascript reactjs typescript azure-storage


    【解决方案1】:

    最好使用@azure/storage-blob 库,然后代码如下所示,而不是像您在代码中那样直接尝试调用blob REST API,这似乎没有必要重新发明轮子。图书馆已经为你做了。详情请参考this

    const { BlobServiceClient } = require("@azure/storage-blob");
     
    const account = "<account name>";
    const sas = "<service Shared Access Signature Token>";
    const containerName = "<container name>";
    const blobName = "<blob name>";
     
    const blobServiceClient = new BlobServiceClient(`https://${account}.blob.core.windows.net${sas}`);
     
    async function download() {
      const containerClient = blobServiceClient.getContainerClient(containerName);
      const blobClient = containerClient.getBlobClient(blobName);
     
      // Get blob content from position 0 to the end
      // In browsers, get downloaded data by accessing downloadBlockBlobResponse.blobBody
      const downloadBlockBlobResponse = await blobClient.download();
      const downloaded = await blobToString(await downloadBlockBlobResponse.blobBody);
      console.log("Downloaded blob content", downloaded);
     
      // [Browsers only] A helper method used to convert a browser Blob into string.
      async function blobToString(blob) {
        const fileReader = new FileReader();
        return new Promise((resolve, reject) => {
          fileReader.onloadend = (ev) => {
            resolve(ev.target.result);
          };
          fileReader.onerror = reject;
          fileReader.readAsText(blob);
        });
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-04-25
      • 1970-01-01
      • 1970-01-01
      • 2020-12-12
      • 2021-05-25
      • 2021-12-08
      • 1970-01-01
      相关资源
      最近更新 更多