MuleSoft – SFTP – Write content to file and upload to server



This content originally appeared on DEV Community and was authored by elliot

1. Add modue of SFTP

After adding module of SFTP, you’ll see below maven dependency added in pom

<dependency>
    <groupId>org.mule.connectors</groupId>
    <artifactId>mule-sftp-connector</artifactId>
    <version>2.5.1</version>
    <classifier>mule-plugin</classifier>
</dependency>

 

2. Setup local sftp server by docker

This section is omitted; please refer to the following article for details:
install sftp server

 

3. Config sftp in yaml file

  • yaml
sftp:
  host: "127.0.0.1"  
  port: "2222"              
  username: "sftpuser"    
  password: "123456"   
  remotePath: "~/sftp"

 

4. Add SFTP in global element config properties

SFTP global config

Here’s xml code of it:

<sftp:config name="SFTP_Config" doc:name="SFTP Config" doc:id="8217af71-2499-4cc2-9506-b81ab62b0e21" >
  <sftp:connection host="${sftp.host}" port="${sftp.port}" username="${sftp.username}" password="${sftp.password}" workingDir="/"/>
</sftp:config>

 

5. Drag and drop an SFTP write processor and config it

SFTP write

Here’s xml code of it:

<sftp:write doc:name="Write" doc:id="40143955-915c-4a62-8aeb-c65f8fb344b8" config-ref="SFTP_Config" path="/upload/test2.txt"/>

 

6. Use Bruno & FileZilla to verify the program

  • Bruno

Bruno Test

  • FileZilla

After requesting successfully in bruno, you’ll see below result in FileZilla:

FileZilla File

Download the file from FileZilla, and the content of the file should be like this:

{
    "ftpHost": "127.0.0.1",
    "ftpPort": 2222,
    "username": "sftpuser",
    "password": "sftppass",
    "remotePath": "/uploads",
    "fileName": "local-test.txt",
    "fileContent": "SGVsbG8gTG9jYWxTdG9yZSE="
}


This content originally appeared on DEV Community and was authored by elliot