ssh2 nodejs |将文件上传到SFTP |错误:允许在101处拒绝

发布于 2025-02-01 02:22:33 字数 2476 浏览 4 评论 0原文

尝试将文件上传到SFTP服务器权限时,出现错误。如果文件通过Filezilla传输,则相同的操作可行。

const UploadFiletoFTP = () => {
      let Client = require('ssh2').Client;
      var connSettings = {
        host: 'abc.com',
        port: 22,
        username: 'user',
        password: 'pass',
      };

  var conn = new Client();
  conn
    .on('ready', function () {
      conn.sftp(function (err, sftp) {
        try {
          if (err) {
            console.log(err);
            throw 'error ' + err;
          }
          console.log('connected');
          var fs = require('fs'); // Use node filesystem
          var readStream = fs.createReadStream(
            require('path').join(
              __dirname +
                '/audio/test_data_25_05_2022_09_58_00.zip'
            )
          );

          sftp.readdir(
            'speech/non-english',
            function (err, list) {
              if (err) throw err;
              // List the directory in the console
              console.dir(list);
              // Do not forget to close the connection, otherwise you'll get troubles
              conn.end();
            }
          );

          var writeStream = sftp.createWriteStream('SpeechIQ', {
            flags: 'a', // w - write and a - append
            encoding: null, // use null for binary files
            mode: 0o666, // mode to use for created file (rwx)
          });

          writeStream.on('close', function () {
            console.log('- file transferred succesfully');
          });

          writeStream.on('end', function () {
            console.log('sftp connection closed');
            conn.end();
          });

          readStream.pipe(writeStream);
        } catch (err) {
          console.error(err);
        }
      });
    })
    .connect(connSettings);
};

UploadFiletoFTP();

出现以下代码以下错误时出现错误时:

events.js:377
      throw er; // Unhandled 'error' event
      ^

Error: Permission denied
    at 101
Emitted 'error' event on Client instance at:
.
.
.
.
  code: 3
}

如果我缺少某些内容,请告知我建议。

下面的摘要列出了目录中的文件,但Writestream不起作用。

sftp.readdir(
            'speech/non-english',
            function (err, list) {
              if (err) throw err;
              // List the directory in the console
              console.dir(list);
              // Do not forget to close the connection, otherwise you'll get troubles
              conn.end();
            }
          );

When trying to upload a file to sftp server permission denied error appears. The same operation works if file is transferred via FilezIlla.

const UploadFiletoFTP = () => {
      let Client = require('ssh2').Client;
      var connSettings = {
        host: 'abc.com',
        port: 22,
        username: 'user',
        password: 'pass',
      };

  var conn = new Client();
  conn
    .on('ready', function () {
      conn.sftp(function (err, sftp) {
        try {
          if (err) {
            console.log(err);
            throw 'error ' + err;
          }
          console.log('connected');
          var fs = require('fs'); // Use node filesystem
          var readStream = fs.createReadStream(
            require('path').join(
              __dirname +
                '/audio/test_data_25_05_2022_09_58_00.zip'
            )
          );

          sftp.readdir(
            'speech/non-english',
            function (err, list) {
              if (err) throw err;
              // List the directory in the console
              console.dir(list);
              // Do not forget to close the connection, otherwise you'll get troubles
              conn.end();
            }
          );

          var writeStream = sftp.createWriteStream('SpeechIQ', {
            flags: 'a', // w - write and a - append
            encoding: null, // use null for binary files
            mode: 0o666, // mode to use for created file (rwx)
          });

          writeStream.on('close', function () {
            console.log('- file transferred succesfully');
          });

          writeStream.on('end', function () {
            console.log('sftp connection closed');
            conn.end();
          });

          readStream.pipe(writeStream);
        } catch (err) {
          console.error(err);
        }
      });
    })
    .connect(connSettings);
};

UploadFiletoFTP();

When the above code is run below error appears:

events.js:377
      throw er; // Unhandled 'error' event
      ^

Error: Permission denied
    at 101
Emitted 'error' event on Client instance at:
.
.
.
.
  code: 3
}

Please advise if I am missing something.

Below snippet lists the files in the directory but the writestream is not working.

sftp.readdir(
            'speech/non-english',
            function (err, list) {
              if (err) throw err;
              // List the directory in the console
              console.dir(list);
              // Do not forget to close the connection, otherwise you'll get troubles
              conn.end();
            }
          );

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

那请放手 2025-02-08 02:22:33

我必须提供必须上传数据的文件名

var writeStream = sftp.createWriteStream('SpeechIQ/filename.zip', {
            flags: 'a', // w - write and a - append
            encoding: null, // use null for binary files
            mode: 0o666, // mode to use for created file (rwx)
          });

I had to give the filename to which the data has to be uploaded

var writeStream = sftp.createWriteStream('SpeechIQ/filename.zip', {
            flags: 'a', // w - write and a - append
            encoding: null, // use null for binary files
            mode: 0o666, // mode to use for created file (rwx)
          });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文