0
votes

I am using node js to download azure blob storage files into our local machine. I am able to download it into my project path but not able to download into my local machine. I am using html, Express,and node js. Currently working on localhost only. How to download ?

Below is the code that i am using to download blob file to local folder.

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) {
    });
});

I am able to to download it to my project folder but i want to download it to my downloads folder. Please help me on this ?

2
I think we need some more information here. Is the server running on your local computer, or are you making the request to a different machine? Depending on the answer, it might be useful to know the file extension too.Enslev
Yes i am running my node js solution on local computer. I am trying to download pdf and image files from azure blob storage.Sagara Purna Pradeep Gopal
Have a look at thisEnslev

2 Answers

0
votes

According to the reference of the method blobService.getBlobToLocalFile, as below, the value of parameter localFileName should be the local file path with related dir path.

localFileName string The local path to the file to be downloaded.

So I created a directory named downloadImages and changed your code as below.

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) {
    });
});

It works for me, the image file was downloaded into my downloadImages directory, not under the path of my node app.js running.

Note: If you want to deploy it on Azure WebApp later, please must use the absolute directory path like D:/home/site/wwwroot/<your defined directory for downloading images>, because the related directory path is always related the path of IIS started up node.

0
votes

To download a file from Azure blob storage

  • ConnectionString: Connection string to blob storage
  • blobContainer: Blob Container Name
  • sourceFile: Name of file in container i.e sample-file.zip
  • destinationFilePath: Path to save file i.e ${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);
      });
    });
  };