upload files to remote server using multer sftp in

2019-02-21 13:19发布

I'm trying to upload the files to remote server using multer-sftp in node js. since i'm following the official docs npm multer-sftp. Previously i've uploading the files to Amazon S3 instead of remote server. now i want to upload the files to remote server.

API:

exports.newFileUpload =  function(req , res , next){     
    var storage = sftpStorage({
      sftp: {
        host: 'http://www.port*****es.in/',
        port: 22,
        username: 'username',
        password: 'password'

      },
      destination: function (req, file, cb) {
        cb(null, 'images/')
      },
      filename: function (req, file, cb) {
        cb(null, file.fieldname + '-' + Date.now())
      }
    })

    var upload = multer({ storage: storage }).array('file');

    upload(req,res,function(err){
        logger.debug(JSON.stringify(req.body));
              logger.debug(JSON.stringify(req.files));
          if(err){
               logger.debug("Error Occured", JSON.stringify(err));
               res.json({error_code:1,err_desc:err});

               return;
          } else{
              res.json({error_code:0,err_desc:null});
          }
      });
}

While uploading the file, returning the error

    2017-11-10T02:39:48.297Z - debug: Error Occured {"code":"ENOTFOUND","errno":"ENOTFOUND",
"syscall":"getaddrinfo","hostname":"http://www.port****es.in/","host":"http://www.port****es.in/",
"port":22,"level":"client-socket","storageErrors":[]}

And also port no 22 is open in my domain. Awaiting Suggestions, Thanks in Advance.

1条回答
够拽才男人
2楼-- · 2019-02-21 13:37

For Your Error, there are two possibilities

  1. port no 22 is not open state, also not able to access that folder
  2. Check your folder directory in domain

Uploading Files to remote server using multer-sftp is easy and flexible way. also we can upload the files to remote server with scp, ssh techniques in node js.

Working Code:

exports.newFileUpload =  function(req , res , next){     
    var storage = sftpStorage({
      sftp: {
        host: 'hostname',
        port: 22,
        username: 'username',
        password: 'password'

      },
      destination: function (req, file, cb) {
        cb(null, 'images/')
      },
      filename: function (req, file, cb) {
        cb(null, file.fieldname + '-' + Date.now())
      }
    })

    var upload = multer({ storage: storage }).array('file');

    upload(req,res,function(err){
        logger.debug(JSON.stringify(req.body));
              logger.debug(JSON.stringify(req.files));
          if(err){
               logger.debug("Error Occured", JSON.stringify(err));
               res.json({error_code:1,err_desc:err});
          } else{
               logger.debug("Files uploaded successfully");
              res.json({error_code:0,err_desc:null});
          }
      });
}

Note: When using 'multer-sftp' port no 22 is open in remote server.

Hope it helps !

查看更多
登录 后发表回答