Download file via Webservice and Push it to Azure Blob Storage via Node/Express

前端 未结 1 1839
半阙折子戏
半阙折子戏 2020-12-20 10:03

I need to retrieve files from a vendor\'s Web service and push these into a unique blob container so users have a unique \"workspace\". Essentially, I would get files down f

相关标签:
1条回答
  • 2020-12-20 10:40

    In your original idea, at first you get file content data in client side and then post the data to the Express web server.

    If the file you get is in a large size, it will slow down your site because of the file data will be transferred twice via HTTP, and it may occur other problem.

    Furthermore, in my test project, it is hardly to handle with file content data directly.

    So I tried another idea as a workaround.

    I just post the API of getting specific file to the server, pull the file save as a file on server directory and upload file to Storage on server side. Here is my code snippet:

    Angular front end:

    $scope.upload =function(){
        var filename = (new Date()).getTime()+'.pdf';//jpg,txt...
        $http.post('http://localhost:1337/uploadfile', { filename: filename, url: 'http://localhost:1337/output/123.pdf'}).success(function (data) {
            console.log(data);
        },function(err){
            console.log(err);
        });
      }
    

    Back end:

    I suspect the API which you get the file form would be like behind.

    router.get('/output/:filename', function (req, res, next) {
        res.download('upload/'+req.params.filename);
    })
    

    The post request handler leverages package request, and there is no necessary to figure out file type or encoding type, createBlockBlobFromLocalFile will upload the file at the location you provide on blob storage, API reference:

    router.post('/uploadfile', function (req, res, next) {
        var request = require('request');
        var filename = req.body.filename;
        var tmpFolder = 'upload/', //this folder is to save files download from vendor URL, and should be created in the root directory previously. 
            tmpFileSavedLocation = tmpFolder + filename; //this is the location of download files, e.g. 'upload/Texture_0.png'
        var r = request(req.body.url).pipe(fs.createWriteStream(tmpFileSavedLocation))//this syntax will download file from the URL and save in the location asyns
       r.on('close', function (){
            blobsrv.createBlockBlobFromLocalFile('vsdeploy', filename, tmpFileSavedLocation, function (error, result, response) {
                if (!error) {
                    console.log("Uploaded" + result);
               }
                else {
                    console.log(error);
                }
            });
        })
    
    })
    
    0 讨论(0)
提交回复
热议问题