简体   繁体   English

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

[英]upload files to remote server using multer sftp in express Node js?

I'm trying to upload the files to remote server using multer-sftp in node js. 我正在尝试使用节点js中的multer-sftp将文件上传到远程服务器。 since i'm following the official docs npm multer-sftp . 因为我遵循的是官方文档npm multer-sftp Previously i've uploading the files to Amazon S3 instead of remote server. 以前,我将文件上传到Amazon S3,而不是远程服务器。 now i want to upload the files to remote server. 现在我想将文件上传到远程服务器。

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

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. 我的域中也开放了22号端口。 Awaiting Suggestions, Thanks in Advance. 等待建议,在此先感谢。

For Your Error, there are two possibilities 对于您的错误,有两种可能性

  1. port no 22 is not open state, also not able to access that folder 22号端口未处于打开状态,也无法访问该文件夹
  2. Check your folder directory in domain 检查域中的文件夹目录

Uploading Files to remote server using multer-sftp is easy and flexible way. 使用multer-sftp将文件上传到远程服务器是简单而灵活的方式。 also we can upload the files to remote server with scp, ssh techniques in node js. 我们也可以使用node js中的scp,ssh技术将文件上传到远程服务器。

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. 注意:当使用“ multer-sftp”端口时,远程服务器中没有打开22号端口。

Hope it helps ! 希望能帮助到你 !

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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