在Express Node.js中使用Multer SFTP上传文件到远程服务器?

10
我正在尝试使用Node.js中的multer-sftp将文件上传到远程服务器。由于我正在遵循官方文档 npm multer-sftp,之前我已将文件上传到Amazon S3而不是远程服务器。现在我想将文件上传到远程服务器。

API:

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

上传文件时,返回错误。

    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":[]}

并且我的域名中也开放了22端口。等待建议,谢谢。


2
host should not have http://. Correct way: host: 'www.port*****es.in', - Mukesh Sharma
@MukeshSharma 我也尝试过那个,但是出现了同样的错误。 - Keerthivasan
1个回答

7

对于您的错误,有两种可能性:

  1. 端口号22未开启状态,也无法访问该文件夹
  2. 检查域中文件夹目录

使用 multer-sftp 将文件上传到远程服务器是一种简单灵活的方式。我们还可以使用 Node.js 中的 scp、ssh 技术将文件上传到远程服务器。

工作代码:

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

注意:使用“multer-sftp”时,远程服务器开放22端口。
希望这有所帮助!

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接