【问题标题】:How to download files in azure blob storage into local folder using node js如何使用节点 js 将 azure blob 存储中的文件下载到本地文件夹
【发布时间】:2019-08-26 03:29:44
【问题描述】:

我正在使用 node js 将 azure blob 存储文件下载到我们的本地计算机中。我可以将其下载到我的项目路径中,但无法下载到我的本地计算机中。我正在使用 html、Express 和节点 js。目前仅在本地主机上工作。如何下载?

以下是我用来将 blob 文件下载到本地文件夹的代码。

app.get("/downloadImage", function (req, res) {
    var fileName = req.query.fileName;
    var downloadedImageName = util.format('CopyOf%s', fileName);
    blobService.getBlobToLocalFile(containerName, fileName, downloadedImageName, function (error, serverBlob) {
    });
});

我可以将它下载到我的项目文件夹,但我想将它下载到我的下载文件夹。请帮我解决这个问题?

【问题讨论】:

  • 我认为我们需要更多信息。服务器是在您的本地计算机上运行,​​还是您正在向另一台计算机发出请求?根据答案,了解文件扩展名也可能很有用。
  • 是的,我正在本地计算机上运行我的节点 js 解决方案。我正在尝试从 azure blob 存储下载 pdf 和图像文件。
  • 看看this

标签: node.js azure-blob-storage


【解决方案1】:

根据blobService.getBlobToLocalFile方法的引用,参数localFileName的值应该是本地文件路径和相关的dir路径。

localFileName 字符串要下载的文件的本地路径

所以我创建了一个名为downloadImages 的目录并更改了您的代码,如下所示。

var downloadDirPath = 'downloadImages'; // Or the absolute dir path like `D:/downloadImages`

app.get("/downloadImage", function (req, res) {
    var fileName = req.query.fileName;
    var downloadedImageName = util.format('%s/CopyOf%s', path, fileName);
    blobService.getBlobToLocalFile(containerName, fileName, downloadedImageName, function (error, serverBlob) {
    });
});

它对我有用,图像文件已下载到我的downloadImages 目录中,而不是在我的node app.js 运行路径下。

注意:如果以后要部署在Azure WebApp上,请务必使用D:/home/site/wwwroot/<your defined directory for downloading images>这样的绝对目录路径,因为相关的目录路径总是与IIS启动节点的路径相关。

【讨论】:

    【解决方案2】:

    从 Azure Blob 存储下载文件

    • ConnectionString:到 Blob 存储的连接字符串
    • blobContainer:Blob 容器名称
    • sourceFile:容器中的文件名,即 sample-file.zip
    • destinationFilePath:保存文件的路径,即${appRoot}/download/${sourceFile}

    const azure = require('azure-storage');
    
    async function downloadFromBlob(
        connectionString,
        blobContainer,
        sourceFile,
        destinationFilePath,
      ) {
        logger.info('Downloading file from blob');
        const blobService = azure.createBlobService(connectionString);
        const blobName = blobContainer;
        return new Promise((resolve, reject) => {
          blobService.getBlobToLocalFile(blobName, sourceFile, destinationFilePath, (error, serverBlob) => {
            if (!error) {
              logger.info(`File downloaded successfully. ${destinationFilePath}`);
              resolve(serverBlob);
            }
            logger.info(`An error occured while downloading a file. ${error}`);
            reject(serverBlob);
          });
        });
      };

    【讨论】:

      猜你喜欢
      • 2021-02-22
      • 2014-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-20
      • 1970-01-01
      • 2015-12-30
      • 1970-01-01
      相关资源
      最近更新 更多