Transfer Eleven Labs generated MP3 to server via SFTP

Hey all, newbie to active pieces and part time chaos coder here. I have worked with the active pieces pro gpt to write some javascript code for the code node as I would like to deposit the mp3 generated by Eleven labs text to speech onto a server where I am running some python code with pydub to combine it with various other elements. GPT suggests that the file needs to be downloaded to a temp file before sending it via SFTP. Is this something that is available to users for this purpose? If not what are the workarounds for flows like this? Here is the code I have so far.

// Setup and Installation
const Client = require('ssh2-sftp-client');
const axios = require('axios');
const fs = require('fs');
const path = require('path');
const sftp = new Client();

// Access the mapped keys directly
const user_id = keys.user_id; // mapped from keys
const sesh_id = keys.sesh_id; // mapped from keys
const aff_mp3 = keys.aff_mp3; // mapped from keys
const sftp_user = keys.sftp_user; // mapped from keys
const sftp_pass = keys.sftp_pass; // mapped from keys

// Create Directory Path from Previous Step Data
const newDirectoryName = `${user_id}_${sesh_id}`;
console.log(newDirectoryName); // For debugging purposes

// SFTP Connection and Directory Creation
const remotePath = `/root/affappaudio/audio_files/main/${newDirectoryName}`;

// Temporary local file path
const localFilePath = path.join('/tmp', 'file.mp3');

// Function to download the file
async function downloadFile(url, filePath) {
  const response = await axios({
    method: 'get',
    url: url,
    responseType: 'stream'
  });

  return new Promise((resolve, reject) => {
    const writer = fs.createWriteStream(filePath);
    response.data.pipe(writer);
    writer.on('finish', resolve);
    writer.on('error', reject);
  });
}

// Main function to handle the SFTP process
async function main() {
  try {
    // Download the file from the URL
    console.log(`Downloading file from ${aff_mp3}`);
    await downloadFile(aff_mp3, localFilePath);
    console.log('File downloaded successfully');

    // Connect to the SFTP server
    await sftp.connect({
      host: '159.203.189.35',
      port: '22',
      username: sftp_user,
      password: sftp_pass
    });
    console.log(`Connected to SFTP server. Creating directory: ${remotePath}`);

    // Create the directory on the SFTP server
    await sftp.mkdir(remotePath, true);
    console.log(`Directory created: ${remotePath}. Uploading file.`);

    // Upload the file to the SFTP server
    await sftp.put(localFilePath, `${remotePath}/file.mp3`);
    console.log('File uploaded successfully');
  } catch (err) {
    console.error('Error during SFTP operation:', err.message);
  } finally {
    // Clean up and end the SFTP session
    await sftp.end();
    // Optionally delete the temporary local file
    fs.unlinkSync(localFilePath);
  }
}

// Execute the main function
main();

Or is this solved using file helper node? I haven’t got either to work yet so looking for direction or a solution if someone else has already achieved this. Thanks.

Has anyone done anything like this before? I can save the files to my gDrive but I would prefer to ftp them to a server. The sftp node only deposits files with the URL in it for some reason rather than the file. I am confused because, as I say, the file lands in the gDrive fine.

I thought I had it then. Does anyone know if I am on the right track? This seems like it should be much easier than this.



Hi @LloydBH , Welcome to the community :wave:,

You can use following flow to upload mp3 file (output of Text to Speech step) to SFTP server.

On ElevenLabs step, action will return mp3 file URL. Pass this URL in Read File action of Files Helper step and choose output format as Base64 as shown in following image.

Now in code step, pass base64 output of previous step as fileUrl field. You can pass other fields as per your use case and use following code.

import Client from 'ssh2-sftp-client';
import { Buffer } from 'buffer';

export const code = async (inputs) => {
    const sftp = new Client();
    const sftpConfig = {
        host: 'host',
        port: 22,
        username: 'username',
        password: 'password',
    };

    try {
       await sftp.connect(sftpConfig);
       const fileContent = Buffer.from(inputs.fileUrl, 'base64');
       
        await sftp.put(fileContent, 'speech.mp3');

    } catch (error) {
        console.error('Error during SFTP operation:', error);
         throw error;
    } finally {
        await sftp.end();
    }
};

Let me know if you face any issue with this.

Awesome, thank you for the reply. I have made the required inputs available (but removed them for the purpose of the screenshot) as per the screenshot but get an error. "missing ) after argument list"

Checking the code below and through gpt etc I can not see the source of this error.

export const code = async (inputs) => {
  return true;
};
import Client from 'ssh2-sftp-client';
import { Buffer } from 'buffer';

export const code = async (inputs) => {
    const sftp = new Client();
    const sftpConfig = {
        host: 'inputs.host',
        port: 22,
        username: 'inputs.username',
        password: 'inputs.password',
    };

    try {
        await sftp.connect(sftpConfig);
        const fileContent = Buffer.from(inputs.fileUrl, 'base64');
        
        await sftp.put(fileContent, 'speech.mp3');
    } catch (error) {
        console.error('Error during SFTP operation:', error);
        throw error;
    } finally {
        await sftp.end();
    }
};

You have two code variables in snippet, please remove the first one. Also make sure you have added ‘ssh2-sftp-client’ and ‘buffer’ package by clicking “Add NPM package” in code editor.

image

1 Like

Ah! This is all making sense now TY. It was throwing errors but I have worked out that the inputs.key shouldn’t be within the quote marks. Just mentioning that in case it is useful for others.

Thanks again for your help.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.