【问题标题】:upload files to remote server using multer sftp in express Node js?在 Express Node js 中使用 multer sftp 将文件上传到远程服务器?
【发布时间】:2018-04-23 05:36:00
【问题描述】:

我正在尝试使用节点 js 中的 multer-sftp 将文件上传到远程服务器。因为我正在关注官方文档npm multer-sftp。以前我将文件上传到 Amazon S3 而不是远程服务器。现在我想将文件上传到远程服务器。

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 号端口。等待建议, 提前致谢。

【问题讨论】:

  • host 不应该有http://。正确方式:host: 'www.port*****es.in',
  • @MukeshSharma 我也试过了,但同样的错误

标签: javascript node.js file-upload remote-server multer


【解决方案1】:

对于你的错误,有两种可能

  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 号端口是打开的。

希望对你有帮助!

【讨论】:

    猜你喜欢
    • 2015-03-09
    • 1970-01-01
    • 1970-01-01
    • 2021-07-25
    • 2020-10-10
    • 2021-08-14
    • 2015-12-13
    • 2017-03-01
    • 2015-10-21
    相关资源
    最近更新 更多