Quick Storage API in C# with the SFTP protocol
Published {$created} by Carsten Blum
With C#, you can easily create a simple Storage API by using SFTP to connect to ftpGrid. This tutorial shows you how to generate a text file and upload it to your ftpGrid account using the SSH.NET (Renci.SshNet
) library. The code works on macOS, Linux, and Windows.
It is recommended to 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
.NET 6 or later (cross-platform)
SSH.NET NuGet package (
dotnet add package SSH.NET
)An ftpGrid account with SFTP access
I use Mac or Linux for development, so the tutorial is targeted towards those *Nix platforms, but will work on Windows
Step 1: Create a new C# console project
#Install dotnet on MacOS
brew install dotnet
#If using Debian, or similar, follow this dotnet installation guide
#Follow this guide: https://learn.microsoft.com/en-us/dotnet/core/install/linux-debian?tabs=dotnet9
#Prepare you storage tutorial project
dotnet new console -n StorageApiTutorial
cd StorageApiTutorial
dotnet add package SSH.NET
Step 2: Create boilerplate and create a test file
Inside Program.cs
, create the boilerplate code:
// ftpGrid C# storage API tutorial using SFTP
using System;
using System.IO;
using Renci.SshNet;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Creating hello.txt test file.");
File.WriteAllText("hello.txt", "Hello from C# and ftpGrid!");
}
}
Step 3: Setup ftpGrid credentials
In the main method add parameters for ftpGrid edge connection.
string host = "edge1.ftpgrid.com";
int port = 22;
string username = "PREFIX.username";
string password = "password";
Step 4: Connect to ftpGrid with SSH.NET
In the main method add the actual connection and upload in a using block
using (var client = new SftpClient(host, port, username, password))
{
client.Connect();
Console.WriteLine("Connected to ftpGrid via SFTP");
// Ensure /uploads directory exists
if (!client.Exists("/uploads"))
{
client.CreateDirectory("/uploads");
Console.WriteLine("Created remote directory /uploads");
}
// Upload file
using (var fileStream = new FileStream("hello.txt", FileMode.Open))
{
client.UploadFile(fileStream, "/uploads/hello.txt");
Console.WriteLine("Uploaded hello.txt to /uploads");
}
client.Disconnect();
}
Step 5: Run the program
Run the program in the terminal, this is the expected output:
# dotnet run
Creating hello.txt sample file.
Connected to ftpGrid via SFTP
Uploaded hello.txt to /uploads
#
The complete sample code
This is the complete sample code used in this tutorial
// ftpGrid C# storage API tutorial using SFTP
using System;
using System.IO;
using Renci.SshNet;
class Program
{
static void Main(string[] args)
{
string host = "edge1.ftpgrid.com";
int port = 22;
string username = "PREFIX.username";
string password = "password";
Console.WriteLine("Creating hello.txt sample file.");
File.WriteAllText("hello.txt", "Hello from C# and ftpGrid!");
using (var client = new SftpClient(host, port, username, password))
{
client.Connect();
Console.WriteLine("Connected to ftpGrid via SFTP");
// Ensure /uploads directory exists
if (!client.Exists("/uploads"))
{
client.CreateDirectory("/uploads");
Console.WriteLine("Created remote directory /uploads");
}
// Upload file
using (var fileStream = new FileStream("hello.txt", FileMode.Open))
{
client.UploadFile(fileStream, "/uploads/hello.txt");
Console.WriteLine("Uploaded hello.txt to /uploads");
}
client.Disconnect();
}
}
}
Conclusion
In just a few lines of C# code, you connected to ftpGrid via SFTP and uploaded a file.This shows how easily you can use ftpGrid as a Storage API in .NET projects.
From here, you can:
Automate file backups from your apps
Upload logs or reports
Integrate secure cloud storage into your existing .NET solutions
Try it yourself, and experience how ftpGrid makes cloud storage simple for C# developers. If you haven't done so already, create your account today.