首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ssh2 nodejs将文件上载到sftp \\错误:在101时拒绝权限

ssh2 nodejs将文件上载到sftp \\错误:在101时拒绝权限
EN

Stack Overflow用户
提问于 2022-05-25 06:56:57
回答 1查看 453关注 0票数 0

当试图将文件上载到sftp服务器权限时,会出现错误。如果文件是通过FilezIlla传输的,则相同的操作可以工作。

代码语言:javascript
复制
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();

当上面的代码在下面运行时,会出现错误:

代码语言:javascript
复制
events.js:377
      throw er; // Unhandled 'error' event
      ^

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

如果我遗漏了什么,请告诉我。

下面的代码段列出了目录中的文件,但是writestream不起作用。

代码语言:javascript
复制
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();
            }
          );
EN

回答 1

Stack Overflow用户

发布于 2022-05-25 09:44:21

我必须给出要上传数据的文件名。

代码语言:javascript
复制
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)
          });
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72373130

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档