<< Back to tutorials

Quick Storage API in Python with the SFTP protocol

Published {$created} by Carsten Blum


Sometimes you don’t need a complex SDK — you just want a simple way to store files in the cloud. With Python and the Paramiko library, you can easily connect to ftpGrid over SFTP and upload files programmatically. This tutorial shows you how to create a small text file and push it to your ftpGrid account.


As always it is recommended read our getting started with FTP/SFTP guide to create your account. To read more in-depth about ftpGrid managed FTP/SFTP cloud hosting checkout our guide to ftpGrid's cloud hosting. Finally, if you need help creating SSH keys, follow our SSH key creation tutorial.



Requirements

  • Python 3.8+

  • paramiko library (install with pip3 install paramiko)

  • An ftpGrid account with SFTP enabled, see our onboarding guide



Create your Python tutorial project

To follow along with the Python persistent storage tutorial, you need to complete some initial steps to get the boilerplate done. I personally use Mac or Linux for development, so this is my way to do it.

#Install python3 on MacOS:
brew install python3

#Install python3 on Debian (or variants)
sudo apt install python3

#Verify:
python3 --version

#Install dependencies
pip3 install paramiko

With Python installed, it is time to create the actual boilerplate

mkdir python-storage-api
cd python-storage-api

#use your favorite editor to create the python script
vim storage.py


Step 1: Connect to ftpGrid

Establish your connection to ftpGrid, using your account from the onboarding guide.

#Quick Storage API in Python with the SFTP protocol
import paramiko

print('Python FTP storage guide')

hostname = "edge1.ftpgrid.com"
port = 22
username = "PREFIX.username"
password = "password"
transport = paramiko.Transport((hostname, port))

transport.connect(username=username, password=password)
sftp = paramiko.SFTPClient.from_transport(transport)


Step 2: Create a test file

Initially create our test upload file

with open("hello.txt", "w") as f:
    f.write("Hello from Python and ftpGrid!")


Step 3: Ensure the remote directory exists

Paramiko doesn’t have a built-in mkdir -p, so we’ll create the directory if missing:

import stat
def mkdir_p(sftp, remote_directory):
    try:
        sftp.stat(remote_directory)
    except FileNotFoundError:
        sftp.mkdir(remote_directory)

mkdir_p(sftp, "/uploads")


Step 4: Upload the file

Then we upload the actual file

local_file = "hello.txt"
remote_file = "/uploads/hello.txt"
sftp.put(local_file, remote_file)
print(f"Uploaded {local_file} to {remote_file}")


Step 5: Close the connection

Finish by closing the SFTP connection

sftp.close()
transport.close()


Complete example and running

This is the complete tutorial code for using ftpGrid FTP cloud storage in your Python programs

#Quick Storage API in Python with the SFTP protocol
import paramiko

print('Python FTP storage guide')

#Connection settings
hostname = "edge1.ftpgrid.com"
port = 22
username = "PREFIX.username"
password = "password"
transport = paramiko.Transport((hostname, port))
transport.connect(username=username, password=password)
sftp = paramiko.SFTPClient.from_transport(transport)

with open("hello.txt", "w") as f:
    f.write("Hello from Python and ftpGrid!")

import stat
def mkdir_p(sftp, remote_directory):
    try:
        sftp.stat(remote_directory)
    except FileNotFoundError:
        sftp.mkdir(remote_directory)

mkdir_p(sftp, "/uploads")

local_file = "hello.txt"
remote_file = "/uploads/hello.txt"
sftp.put(local_file, remote_file)
print(f"Uploaded {local_file} to {remote_file}")

sftp.close()
transport.close()


Now simply run from the command line using the python3 command:


# python3 storage.py
Python FTP storage guide
Uploaded hello.txt to /uploads/hello.txt
#


Conclusion

You’ve just created a small “Storage API” in Python using SFTP. With only a few lines of code, you generated a file, connected securely, and uploaded it to ftpGrid.


From here, you can extend the script to:

  • Automate backups

  • Upload log files

  • Build a lightweight storage layer into your Python applications

Try it yourself and experience how easy it is to use ftpGrid as your Python Storage API.



Signup now
© 2025 ftpGrid

ftpGrid ApS
Branebjerg 24
DK-5471
Gamby
Denmark

Looking for an all-in-one time tracking, timesheet, and invoicing solution - visit our Devanux sister company Nureti at https://nureti.com.

Preview Devanux’s upcoming project Pictoguide – a visual support tool designed to bring structure and clarity to people with ASD.