简体   繁体   中英

How to send local file to remote server in azure web app

I have a M(ySQL)EAN stack application that I've deployed to Azure. The application should allow users to upload content (including doc files, images, audio, etc).

On the backend in my express route, when a user uploads content I use the promise-sftp module to send the local file to a remote server for storage as follows:

var AsyncClient  = require('promise-sftp');

var sftp = new AsynClient();
sftp.connect(options).then(function (serverMsg) {
    console.log('sftp connected ... multer uploading');

    // error handling for multer upload
    upload(req, res, function (err) {
        if(err)
            return res.send(err);

        var doc = {...} //create document object from req.files[0]

        sftp.fastPut(doc.doc_storage_path, process.env.REMOTE_URL + doc.doc_file_name,{...}})
            .then(function () { 
                 //do something 
                 return sftp.end(); //end sftp connection
             });


        //save file info to mysql db
        ...
    });

This works fine when I'm developing locally, but when I deploy it to Azure it does not work. The file is saved to the file system locally and the file information is saved to the MySQL database, but sending to the remote server never happens.

I'm not sure if I need to have an sftp server on the Azure VM or not. If it helps at all my web app is utilizing Azure's Shared App Service Plan . I'm not very backend savvy so any help understanding this is appreciated!

Instead of creating sFTP server, I'd recommend you to try leveraging Azure Blob storage to achieve your requirement. For instance, users upload unstructured object data (such as text or binary data) to Azure storage in your app, after that done, set Blob's URL to the MySQL database and then the file can be accessed from anywhere in the world via HTTP or HTTPS.

About how to use Blob storage from Node.js, please refer to https://docs.microsoft.com/en-us/azure/storage/storage-nodejs-how-to-use-blob-storage .

Also, you can refer to the sample on Github at https://github.com/Azure-Samples/storage-blob-node-getting-started .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM