使用multer sftp在快速节点js中将文件上传到远程服务器?

Kee*_*san 10 javascript file-upload remote-server node.js multer

我正在尝试使用节点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});
          }
      });
}
Run Code Online (Sandbox Code Playgroud)

上传文件时,返回错误

    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":[]}
Run Code Online (Sandbox Code Playgroud)

在我的域名中还打开了22号端口.等待建议,提前致谢.

Jac*_*kie 7

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

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

使用文件multer-sftp轻松灵活地将文件上传到远程服务器.我们也可以使用节点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});
          }
      });
}
Run Code Online (Sandbox Code Playgroud)

注意:使用'multer-sftp'端口时,远程服务器中没有打开22.

希望能帮助到你 !