FTP Operations using Ballerina Integrator

Image for post
Image for post
image courtesy: https://devdojo.com/articles/local-vs-remote-servers

The WSO2 FTP Connector enables you to connect to an FTP server and perform operations on files and folders stored on the server. FTP connector offers FTP Client, which supports the generic FTP operations such as get, delete, put, append, mkdir, rmdir, isDirectory, rename, size, and list, as well as an FTP listener that listens to the remote FTP server and triggers events.

  1. FTP Client
  2. FTP Listener

Let’s look into how we can use these FTP operations in the FTP Client.

  1. Download and install Ballerina Integrator for your operating system.
  2. Install Oracle JDK 1.8.*
  3. Install VS Code
  4. Install the Ballerina Integrator Extension from the VS Code marketplace
  5. Setup an FTP server.
  6. Make sure you have uploaded an employees.xml file to the ftp location /home/ftp-server/in since we are going to read the file through this folder.

It has been already given a sample at WSO2 docs on writing a file to an FTP server. You can find it here.

Let us focus on the rest of the scenarios.

Reading a file from the remote FTP server.

First, it needs to configure the FTP Client as below where we should provide the FTP server credentials in the ballerina.conf file which is created in the project root directory.

For creating a ballerina project from scratch please refer https://ei.docs.wso2.com/en/next/ballerina-integrator/get-started/quick-start-guide/

ballerina.conf

FTP Client

My base path will be configured as company.

The resource configuration looks like below.

We need to import the following.

The complete source will look as below.

We can simply build the module by running by the following command which will build all the modules. A jar will be created in the target folder.

Run the jar as below and the service will be started in the port 9090.

Invoke the resource as below.

It will output the content of the file.

Deleting a file from the remote FTP server.

Creating a folder in the remote FTP server.

Remove a folder in the remote FTP server.

Rename a file in the remote FTP server.

Retrieve a file size of a file in the remote FTP server.

To read about the FTP listener operations you can refer here.

Written by

Senior Software Engineer — QA at WSO2

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store