FTP Integration in Rails App

Posted on the 18 January 2021 by Botreetechnologeis @botreetech

FTP stands for File transfer protocol. It is basically used to transfer files from the local computer to the client/remote server and manage client and server side data of the application.

Ruby provides a 'net/ftp' library to implement FTP into your web application.

In this article we will learn how to

  • Integrate FTP in rails application.
  • Methods of FTP library and use of these methods.

First you need a library of FTP class.

require 'net/ftp'

There are some methods which will be useful to users.

1. ::open

This method is similar to FTP.new with mandatory host parameters.

Users have to provide the mandatory parameters as an argument in this method.

This method will pass to the FTP object and make a connection to the remote server and close it when the blocks finish or exception is raised.

Net::FTP.open(SFTP_CONFIGURATION_URL, username:
SFTP_CONFIGURATION_USERNAME, password: SFTP_CONFIGURATION_PASSWORD) do
|ftp|
end

2. Getbinaryfilefile(remote_file_path, local_file_path, blocksize = DEFAULT_BLOCKSIZE )

This method is used for retrieving files from the remote server in binary mode and storing the results in a specified local file. If the local file is nil, it returns retrieved data.

DEFAULT_BLOCKSIZE value is by default 1024 bytes per block(Not Mandatory).

Net::FTP.open(SFTP_CONFIGURATION_URL, username:SFTP_CONFIGURATION_USERNAME,
password: SFTP_CONFIGURATION_PASSWORD) do |ftp|
ftp.getbinaryfile("/uploads/tmp_data/inventory_30_09_2020.xls",
'/home/er/projects/inventory.xls', blocksize = DEFAULT_BLOCKSIZE)
end

3. Gettextfile(remote_file_path, local_file_path)

This method retrieves files in ASCII(text) mode from the remote server and stores the results in a specified local file. If the local file is nil, it returns retrieved data.

If blocks supplied it is passed retrieved data one line at a time.

Net::FTP.open(SFTP_CONFIGURATION_URL, username:SFTP_CONFIGURATION_USERNAME,
password: SFTP_CONFIGURATION_PASSWORD) do |ftp|
ftp.gettextfile("/uploads/tmp_data/inventory_30_09_2020.xls",
'/home/er/projects/inventory.xls', blocksize = DEFAULT_BLOCKSIZE)
end

4. Putbinaryfile(local_file_path, remote_file_path)

Transfer local file to the remote server in binary mode at the specified file location given in the remotefile parameter and store the data into the remote file.

5. Puttextfile

Transfer local file to the remote server in ASCII(text) mode at the specified file location given in the remotefile parameter and store the data into the remote file.

6. Chdir

Changes the remote directory.

List method returns all the files of the current remote directory.

7. Rename

Used for renaming a file.

8. Delete

Used to delete files from the remote server.

def delete
Filename = '/uploads/tmp_data/inventory_30_09_2020.xls'
resp = sendcmd("DELE #{filename}")
end

So, using the above method and the use of getbinaryfile and putbinaryfile, we can download files from the FTP server and send the file to the FTP server.