I have a SFTP server hosting some files. How to hit my SFTP server from Catalyst and upload the files to Catalyst filestore?

I have a SFTP server hosting some files. How to hit my SFTP server from Catalyst and upload the files to Catalyst filestore?

1. Create a catalyst project and initialize the project in your local machine. You can refer to the help documentation for the same here.
2. Install Catalyst CLI in order to work with Catalyst in your local machine. You can refer to the help documentation for the same here.
2. Create an advancedIO function from the Catalyst CLI while initializing the project.
3. Navigate to your function directory(~/{project_name}/functions/{function_name}) in your terminal
4. Install the necessary npm packages that are used in the function by executing the commands below

  1. npm install ssh2-sftp-client
5. Paste the given code. Modify your hostname, port number, username and password. Navigate back to your project directory.

  1. "use strict";
  2. const Client = require('ssh2-sftp-client');
  3. module.exports = async(req, res) => {
  4.   connectToSFTPServer();
  5.   res.write("hit");
  6.   res.end();
  7. };
  8. async function connectToSFTPServer() {
  9.     const sftp = new Client();
  10.     const remoteHost = ''; //replace your SFTP host 
  11.     const remotePort = ;  //replace your port number (default is 22 for SFTP)
  12.     const remoteUsername = ''; //replace your SFTP server username
  13.     const remotePassword = ''; //replace your SFTP server password
  15.     try {
  16.         await sftp.connect({
  17.           host: remoteHost,
  18.           port: remotePort,
  19.           username: remoteUsername,
  20.           password: remotePassword
  21.         });
  22.          console.log('Connected to SFTP server');
  23.          // Perform SFTP operations here
  24.          const remoteFilePath = '{your_remote_path}';
  25.          const files = await getFilesInDirectory(sftp, remoteFilePath);
  26.          console.log('Files in the remote directory:');
  27.          files.forEach((file) => {
  28.             console.log(file);
  29.           });
  30.         // Disconnect from the SFTP server
  31.         await sftp.end();
  32.         console.log('Disconnected from SFTP server');
  33.       } catch (err) {
  34.         console.error('Error:', err.message);
  35.       }}
  36.   async function getFilesInDirectory(client, remotePath) {
  37.     const entries = await client.list(remotePath);
  38.     const files = [];
  39.     for (const entry of entries) {
  40.       if (entry.type === 'd') {
  41.         // If the entry is a directory, recursively call the function
  42.         const subdirectoryPath = `${remotePath}/${entry.name}`;
  43.         const subdirectoryFiles = await getFilesInDirectory(client, subdirectoryPath);
  44.         files.push(...subdirectoryFiles);
  45.       } else if (entry.type === '-') {
  46.         // If the entry is a regular file, add it to the files array
  47.         files.push(entry.name);
  48.       }}
  49.     return files;
  50.   }

6. Run catalyst serve in order to test your function in local debugging.
7. Run catalyst deploy in order to deploy your function to the development environment. You can hit your function by using the URL generated while deploying the function. You can refer the help documentation for the same here.

    • Announcements

    • Important Announcements in Support for Catalyst Features

      Hello Catalyst Users, This announcement is to bring to your notice some of the recent updates in our support for various Catalyst components, to ensure that your existing Catalyst applications function properly. You can continue to build robust applications
    • Introducing Catalyst 2.0, the simplest cloud-based pro-code development platform.

      Dear Catalyst Community, We are beyond thrilled to present you Catalyst 2.0—the new-age, pro-code development platform that redefines the way you build, deploy, and scale apps. Catalyst 2.0 is not a mere upgrade, but a significant leap into the future
    • Set budgets to optionally disable your Catalyst production environment

      Hello all,    We are pleased to inform you that you can now configure the automatic disabling of a project's production environment upon reaching a specified budget's threshold. Budget alerts in Catalyst facilitate setting usage limits in amounts or Catalyst
    • #CatalystServerless Hackathon 2022

      We are pleased to announce #CatalystServerless Hackathon with prizes worth $5000 up for grabs. Create a working prototype aimed to solve a critical business issue using Catalyst and stand a chance to win exciting prizes. The Hackathon will happen in 3
    • Share your interest to participate in the #CatalystServerless Blogathon

      Hi, we are working on announcing the next edition of the #CatalystServerless Blogathon. Please fill up this form to stay posted on all updates related to the Blogathon. https://zfrmz.com/JN9ChxPU9JQpPgB1vRp8

      Catalyst Community