简体   繁体   English

使用 Node 和 SSH2 将缓冲区数据写入 SFTP 服务器

[英]Write buffer data to SFTP server using Node and SSH2

I am trying to implement writing files to an SFTP server in Node using the SSH2 module.我正在尝试使用 SSH2 模块将文件写入 Node 中的 SFTP 服务器。 For my use case, the file source is Azure Blob Storage, and files are relatively large (more than 5 gigs) and so the idea is to capture data from blob storage in chunks and write them to the server.对于我的用例,文件源是 Azure Blob Storage,文件相对较大(超过 5 gig),因此想法是从块存储中捕获数据并将其写入服务器。 Don't want to download the whole file and then perform the write as files are large and don't want to have a disk space issue during runtime.不想下载整个文件然后执行写入,因为文件很大并且不想在运行时出现磁盘空间问题。

I have a working implementation of this by making use of downloadToBuffer() and write() functions available and incrementing the 'offset' until all the bytes and written.我通过使用可用的 downloadToBuffer() 和 write() 函数并增加“偏移量”直到所有字节都被写入,从而实现了这一点。 As seen in the code snippet如代码片段所示

sftp.open('remoteFilePath','w', async (openError,handle) => {
  if (openError) throw openError;
  var blobOffset=0;
  try{
    while(blobOffset<file.size){
      await client.downloadToBuffer(blobOffset, blobOffset+ length > file.size? file.size - blobOffset: length).then((buffer) => {
        sftp.write(handle,buffer,0,blobOffset + length > file.size? buffer.length:length, blobOffset, (writeError)=>{if(writeError) throw writeError});
      });
      blobOffset += length;
    }
  }
  catch(e){
    console.log(e);
  }
}

This solution works but does not feel very efficient for large files.此解决方案有效,但对于大文件感觉不是很有效。 Is there a much better way to implement this?有没有更好的方法来实现这一点? Maybe using streams and don't have to use loops?也许使用流而不必使用循环?

Regarding the issue, please refer to the following code关于这个问题,请参考以下代码

var Client = require("ssh2").Client;
var {
  BlobServiceClient,
  StorageSharedKeyCredential,
} = require("@azure/storage-blob");

var accountName = "andyprivate";
  var accountKey =
    "";
  var creds = new StorageSharedKeyCredential(accountName, accountKey);
  var blobServiceClient = new BlobServiceClient(
    `https://${accountName}.blob.core.windows.net`,
    creds
  );
  var containerClient = blobServiceClient.getContainerClient("output");
  let blob = containerClient.getBlockBlobClient("5m Sales Records.csv");
  let read = (await blob.download(0)).readableStreamBody;
  var conn = new Client();

  conn.connect({
    host: "",
    port: 22,
    username: "",
    password: "!",
  });

  conn.on("ready", async () => {
    conn.sftp((err, sftp) => {
      if (err) throw err;
      var write = sftp.createWriteStream("/home/testqw/test.csv");

      read.pipe(write);
      write
        .on("error", function (error) {
          throw error;
        })
        .on("finish", () => {
          console.log("All writes are now complete.");
          sftp.end();
        });
    });
  });
  conn.on("end", () => {
    console.log("close the connection");
  });

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

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