How to Read SFTP files in Ruby

  • Post last modified:January 21, 2023
  • Reading time:3 mins read

Introduction

  • SFTP is very common way to transfer the file from one server to another.
  • Often as a client we recieve SFTP server endpoint to pull the file to our system from other system.
  • In this article we will use ruby client to read the remote file which is located on SFTP server.

Note: We are using username and password in this article, but in future article we will use public , private key pair to connect.

SFTP Server

  • If you dont have SFTP server then you can setup one on local system or Cloud environment for test purpose.
  • I have setup one for my use case on GCP. My Linux environment is based on Ubuntu and I followed this guide to setup SFTP server.

Business Logic

Library

  • We will use a popular ruby gem called ‘net-sftp’ to connect to SFTP server.
  • get it using gem install
gem install net-sftp

Session Instance & Connection

  • Here we are using password-based authentication and not strictly checking the host in the “~/.ssh/known_hosts” file. That’s why we are passing check_host_ip as false.
def connect
        @session ||= Net::SSH.start(@host, @user, :password=>@password, check_host_ip: false)
        @sftp_client ||= Net::SFTP::Session.new(@session)
        @sftp_client.connect!
    end

Read File Contents

  • Reading content is simple, we download the file and print the content. we can also write to local file.
data = sftp_client.connect.download!("/sftp_user/touch.txt")
puts data

Disconnecting Session

  • Once our work is done it’s good practice to close all the resource connections.
    def disconnect
        @sftp_client.close_channel
        @session.close
    end

Client code

  • Client code is simple, we first initialize session then connect it , after that we download the file and print the content. once done we close connection.

sftp_client = SFTPClient.new("34.125.22.200", "sftp_user", "sftp12345" )
data = sftp_client.connect.download!("/sftp_user/touch.txt")
puts data
sftp_client.disconnect

Entire Logic

require 'net/sftp'

class SFTPClient
    
    def initialize(host, user, password)
        @host = host
        @user = user
        @password = password
    end

    def connect
        @session ||= Net::SSH.start(@host, @user, :password=>@password, check_host_ip: false)
        @sftp_client ||= Net::SFTP::Session.new(@session)
        @sftp_client.connect!
    end

    def disconnect
        @sftp_client.close_channel
        @session.close
    end

end

sftp_client = SFTPClient.new("34.125.22.200", "sftp_user", "sftp12345" )
data = sftp_client.connect.download!("/sftp_user/touch.txt")
puts data

Result

  • I downloaded the file through the command line to check the content of the file touch.txt. Its just print “success”.
  • Below is the result of the ruby execution

Conclusion

  • In this article, we used net-sftp library to connect and read the file on the SFTP server using a username and password.
  • In the future article, i will cover how we can connect to sftp server using public private key.

Leave a Reply