Skip to content

SFTP

SFTP is a secure file transfer protocol that provides file access, file transfer, and file management functionalities. The sftp.py module provides a set of methods for working with SFTP.

To use the sftp.py module, you need to have the paramiko library installed. You can install it by running the following command:

    $ pip install everysk-lib[paramiko]

If you are running this inside the Everysk environment, you don't need to install the paramiko library, it is already installed.

For quick reference:

from everysk.core.sftp import SFTP

with SFTP(username='', password='', hostname='', port=22) as sftp:
    filename = sftp.search_by_last_modification_time(path='/dir', prefix='file_')
    file_object = sftp.get_file(filename)

print(filename)
'file_2024-09-17.csv'

print(file_object)
<_io.BytesIO object at 0x7f8b3c7b3b90>

Below we will explain in more details all the options that the SFTP class provides.

The SFTP class is used in order to interact with an SFTP server.

Please keep in mind that the SFTP class is a context manager, so it should be used with the with statement to ensure that the connection is closed properly and the resources are released.

If for some reason you need to use a for loop, keep in mind that you need to use it inside the with statement.

from everysk.core.sftp import SFTP

with SFTP(username='', password='', hostname='', port=22) as sftp:
    for filename in SFTP_FILES:
        file_object = sftp.get_file(filename)


The SFTP class has the following attributes:

  • username: The username to connect to the SFTP server.
  • password: The password to connect to the SFTP server.
  • hostname: The hostname of the SFTP server.
  • private_key: The private key to connect to the SFTP server, defaults to None.
  • passphrase: The passphrase for the private key, used for encrypted private keys, defaults to None.
  • port: The port of the SFTP server, defaults to 22.
  • timeout: The timeout for the connection, defaults to 60 seconds.
  • compress: A boolean indicating if the connection should be compressed, defaults to True.
  • date: A date/datetime object that could be used to parse the python date in file name, defaults to today.

Retrieve File

The get_file() method is used in order to get a file from the SFTP server. The method takes a filename string as input and returns a file object.

If the file does not exist, the method will return None.

from everysk.core.sftp import SFTP

with SFTP(username='', password='', hostname='', port=22) as sftp:
    file_object = sftp.get_file(filename='file_2024-09-17.csv')

print(file_object)
<_io.BytesIO object at 0x7f8b3c7b3b90>

The filename must be a string, and it should contain the full path to the file. If needed, you can use some of the Python date formatting to help you with the filename. For example, if you need to get a file from a specific date, you can use the date attribute to help you with that. The /dir/%Y/%b/file_%y_%m_%d.csv will be parsed to /dir/2024/Nov/file_24_11_01.csv in the example below.

from everysk.core.datetime import Date
from everysk.core.sftp import SFTP

with SFTP(username='', password='', hostname='', port=22, date=Date(2024, 11, 1)) as sftp:
    file_object = sftp.get_file(filename='/dir/%Y/%b/file_%y_%m_%d.csv')

print(file_object)
<_io.BytesIO object at 0x7f8b3c7b3b90>

Search by Modification Time

The search_by_last_modification_time() method is used in order to search for a file by the last modification time, the search will happen recursively inside the path. The method takes a path string and a prefix string as input and returns the first filename of the file that matches the criteria.

Both the path and the prefix must be strings and if needed you could use the Python date formatting too.

from everysk.core.datetime import Date
from everysk.core.sftp import SFTP

with SFTP(username='', password='', hostname='', port=22, date=Date(2024, 11, 1)) as sftp:
    filename = sftp.search_by_last_modification_time(path='/dir01/%Y', prefix='file_%y_%m_%d')

print(filename)
'/dir01/2024/dir02/dir03/file_24_11_01.csv'

Direct Access

If by any chance you need to access the paramiko client and make your own code, you can do it by accessing the client attribute.

from everysk.core.sftp import SFTP

with SFTP(username='', password='', hostname='', port=22) as sftp:
    print(sftp.client)

<paramiko.SFTPClient object at 0x7f8b3c7b3b90>

Connecting to a SFTP Server Using the Private Key

Apart from connecting to a SFTP server using only the password, we have the option to connect using a private key. Keep in mind that the private_key attribute will be used as a path to a private key file. Leading to a code like this:

from everysk.core.sftp import SFTP

with open('path/to/private_key.pem', 'r') as f:
    private_key = f.read()

with SFTP(username='username', hostname='hostname', port=22, private_key=private_key) as sftp:
    files = sftp.client.listdir_attr('/home/test')

for file in files:
    print(file.filename)

'file1.txt'
'file2.txt'


This is useful if you need to make some custom code that is not covered by the methods provided by the SFTP class, here is the documentation for the paramiko library: https://docs.paramiko.org/en/stable/api/sftp.html