Ballerina FTP client
File Transfer Protocol (FTP) is a communication standard that allows transferring files from/to servers over a computer network. FTP client is the one that establishes the connection between the host computer and the remote FTP server. FTP client is capable of transferring files between the local and the remote server in dual directions.
There are hundreds of FTP clients now available and here I am going to explain how easily Ballerina FTP client can be used to communicate with an FTP server.
As the server, I will be using a docker image of VSFTPD server. You can find more details about the docker image from here.
First, you can run the FTP server using the following command.docker run -d -p 21:21
-p 21000-21010:21000-21010 \
-e USERS="one|1234|/home/in" \
-e ADDRESS=localhost \
delfer/alpine-ftp-server
Here I have given the username one
and the password 1234
which will be used for user authentication. And then again I have added /home/in
as the folder path where it will allow us to create/read files in that particular path.
Let’s start writing our Ballerina client now. I have used the Ballerina Swan Lake Beta 2 version for this. You can refer to this guide for the Ballerina installation.
To establish a connection, the FTP client needs to be configured by specifying the host, username, and password of the connecting server. Following is a simple code sample for adding a file to an FTP server and reading the content back again.
Here I have only shown how to copy a file from a host to a remote FTP server and get the file from the remote server. There are plenty of APIs provided by Ballerina to play with files and securely communicate with the FTP server. You can refer them here.
Happy coding with Ballerina!