Sichere Dateiübertragung bleibt ein Eckpfeiler der Unternehmensintegration. Ob du Daten mit Bankpartnern austauschst, Dateien mit Remote-Servern synchronisierst oder Deployment-Pipelines automatisierst — SFTP (SSH File Transfer Protocol) ist der Industriestandard, um Dateien sicher über nicht vertrauenswürdige Netzwerke zu übertragen.
Das sgcIndy-Paket enthält TIdSFTPClient — eine native Delphi-SFTP-Client-Komponente, die über SSH arbeitet, ohne externe Kommandozeilen-Tools oder Drittanbieter-Executables zu benötigen. Sie unterstützt das Hoch- und Herunterladen von Dateien, Verzeichnisverwaltung, symbolische Links, Dateiattribute, Fortschrittsverfolgung und moderne kryptografische Algorithmen — alles über eine saubere, ereignisgesteuerte API.
Dieser Artikel deckt den vollständigen Funktionsumfang ab und liefert sofort einsetzbaren Delphi-Code für die häufigsten SFTP-Operationen.
Wichtige Funktionen
|
Dateiübertragung Lade Dateien mit konfigurierbaren Puffergrößen und Echtzeit-Fortschrittsereignissen hoch und herunter. Übertrage von Dateipfaden oder direkt von TStream-Objekten. |
Verzeichnisoperationen Liste Verzeichnisse mit vollständigen Metadaten auf, erstelle und entferne Verzeichnisse und löse Pfade einschließlich symbolischer Links auf. |
Moderne Kryptografie Curve25519, ECDH, AES-GCM, Ed25519-Schlüssel und HMAC-SHA2. Konfigurierbare Algorithmus-Aushandlung für Compliance-Anforderungen. |
|
Mehrere Auth-Methoden Passwort, Public Key (RSA, ECDSA, Ed25519) und Keyboard-Interactive-Authentifizierung. Host-Key-Verifizierung mit Fingerprint-Callbacks. |
Dateiattribute & Berechtigungen Lese und ändere Dateiberechtigungen, Eigentümer, Zeitstempel und Größen. Volle Unterstützung für Unix-Mode-Bits und symbolische Links. |
Fortschritt & Ereignisse Verfolge den Übertragungsfortschritt mit übertragenen Bytes und Gesamtgröße. Abbrechbare Übertragungen. Error-, Connect- und Disconnect-Ereignisse. |
Schnellstart — Verbinden und eine Datei herunterladen
Ein minimales Beispiel, das sich mit einem Remote-Server verbindet, eine Datei herunterlädt und die Verbindung trennt.
var
oSFTP: TIdSFTPClient;
begin
oSFTP := TIdSFTPClient.Create(nil);
Try
oSFTP.Host := 'sftp.example.com';
oSFTP.Port := 22;
oSFTP.Authentication.Username := 'deploy';
oSFTP.Authentication.Password := 'secret';
oSFTP.Connect;
// Download a file
oSFTP.Get('/data/report.csv', 'C:\local\report.csv');
oSFTP.Disconnect;
Finally
oSFTP.Free;
End;
end;
Authentifizierung
Die Komponente unterstützt drei Authentifizierungsmethoden. Alle drei sind standardmäßig aktiviert — Client und Server handeln automatisch die passendste Methode aus.
Passwort-Authentifizierung
oSFTP.Authentication.Username := 'admin';
oSFTP.Authentication.Password := 'secret';
Public-Key-Authentifizierung
oSFTP.Authentication.Username := 'deploy';
oSFTP.Authentication.PrivateKeyFile := 'C:\keys\id_rsa';
oSFTP.Authentication.PublicKeyFile := 'C:\keys\id_rsa.pub';
oSFTP.Authentication.Passphrase := 'keypassphrase';
Host-Key-Verifizierung
Überprüfe die Identität des Servers, indem du den Host-Key-Fingerprint im OnSSHHostKey-Ereignis inspizierst.
oSFTP.OnSSHHostKey := OnHostKey;
procedure TForm1.OnHostKey(Sender: TObject;
const aHostKeyType, aFingerprint: string;
var aAction: TIdSSHHostKeyVerification);
begin
// Verify fingerprint against known hosts
if aFingerprint = 'SHA256:xyzABC123...' then
aAction := sshHostKeyAccept
else
aAction := sshHostKeyReject;
end;
Dateioperationen
Upload & Download
// Upload a file
oSFTP.Put('C:\local\data.zip', '/uploads/data.zip');
// Download a file
oSFTP.Get('/reports/monthly.pdf', 'C:\local\monthly.pdf');
// Upload from a stream
oSFTP.Put(oMemoryStream, '/uploads/stream-data.bin');
// Download to a stream
oSFTP.Get('/data/export.csv', oFileStream);
String-Komfortmethoden
// Read a remote file into a string
vContent := oSFTP.GetFileAsString('/config/settings.json');
// Write a string to a remote file
oSFTP.PutFileFromString('{"key":"value"}', '/config/settings.json');
Löschen, Umbenennen & Symlinks
// Delete a remote file
oSFTP.Delete('/tmp/old-file.log');
// Rename / move a file
oSFTP.Rename('/data/temp.csv', '/data/final.csv');
// Create a symbolic link
oSFTP.Symlink('/data/final.csv', '/data/latest.csv');
Verzeichnisoperationen
// List directory contents with full metadata
var
oItems: TIdSFTPDirectoryItems;
i: Integer;
begin
oItems := oSFTP.ListDirectory('/data');
for i := 0 to Length(oItems) - 1 do
WriteLn(oItems[i].Filename + ' - ' +
IntToStr(oItems[i].Attrs.Size) + ' bytes');
end;
// Create and remove directories
oSFTP.MakeDirectory('/data/archive/2026');
oSFTP.RemoveDirectory('/data/temp');
// Get current working directory
vPath := oSFTP.GetCurrentDirectory;
// Resolve a path (follows symlinks, resolves . and ..)
vRealPath := oSFTP.RealPath('../data/../data/./file.txt');
Dateiattribute & Informationen
// Check existence
if oSFTP.FileExists('/data/report.csv') then
WriteLn('File found');
if oSFTP.DirectoryExists('/data/archive') then
WriteLn('Directory exists');
// Get file size
vSize := oSFTP.FileSize('/data/report.csv');
// Get full attributes (size, permissions, timestamps, UID/GID)
var
oAttrs: TIdSFTPFileAttributes;
begin
oAttrs := oSFTP.Stat('/data/report.csv');
WriteLn('Size: ' + IntToStr(oAttrs.Size));
WriteLn('Permissions: ' + IntToStr(oAttrs.Permissions));
end;
Übertragungsfortschritt & Abbruch
Das Ereignis OnSFTPProgress wird während jeder Dateiübertragung ausgelöst und ermöglicht eine Echtzeitverfolgung mit der Möglichkeit, die Übertragung jederzeit abzubrechen.
oSFTP.OnSFTPProgress := OnProgress;
procedure TForm1.OnProgress(Sender: TObject;
const aFilename: string;
aTransferred, aTotal: Int64;
var Cancel: Boolean);
begin
ProgressBar1.Max := aTotal;
ProgressBar1.Position := aTransferred;
Label1.Caption := Format('%s: %d / %d bytes',
[aFilename, aTransferred, aTotal]);
// Set Cancel := True to abort the transfer
Cancel := FUserCancelled;
end;
Konfiguration kryptografischer Algorithmen
Die Komponente unterstützt moderne kryptografische Standards. Die Standardeinstellungen sind sicher, du kannst die Algorithmus-Aushandlung jedoch für Compliance- oder Interoperabilitätsanforderungen anpassen.
| Kategorie | Unterstützte Algorithmen |
|---|---|
| Schlüsselaustausch | Curve25519, ECDH (P-256, P-384, P-521), DH Group14/16 |
| Host Keys | Ed25519, ECDSA (P-256, P-384, P-521), RSA (SHA2-256, SHA2-512) |
| Ciphers | AES-256/192/128-CTR, AES-256/128-GCM |
| MACs | HMAC-SHA2-256, HMAC-SHA2-512, HMAC-SHA1 |
// Restrict to only the strongest algorithms
oSFTP.Algorithms.Ciphers := 'aes256-gcm@openssh.com'
,aes256-ctr';
oSFTP.Algorithms.KexAlgorithms := 'curve25519-sha256';
oSFTP.Algorithms.MACs := 'hmac-sha2-256,hmac-sha2-512';
Vollständiges Beispiel
Ein produktionsreifes Beispiel, das sich mit Public-Key-Authentifizierung verbindet, ein Verzeichnis auflistet, eine Datei mit Fortschrittsverfolgung herunterlädt und Fehler behandelt.
uses
IdSFTPClient, IdSSHClasses;
var
oSFTP: TIdSFTPClient;
oItems: TIdSFTPDirectoryItems;
i: Integer;
begin
oSFTP := TIdSFTPClient.Create(nil);
Try
// Connection
oSFTP.Host := 'sftp.example.com';
oSFTP.Port := 22;
// Public key authentication
oSFTP.Authentication.Username := 'deploy';
oSFTP.Authentication.PrivateKeyFile := 'C:\keys\id_ed25519';
// Events
oSFTP.OnSFTPProgress := OnProgress;
oSFTP.OnSFTPError := OnError;
oSFTP.OnSSHHostKey := OnHostKey;
// Connect
oSFTP.Connect;
// List remote directory
oItems := oSFTP.ListDirectory('/data');
for i := 0 to Length(oItems) - 1 do
WriteLn(oItems[i].Filename);
// Download file with progress
oSFTP.Get('/data/backup.tar.gz', 'C:\backups\backup.tar.gz');
// Disconnect
oSFTP.Disconnect;
Finally
oSFTP.Free;
End;
end;
Methodenreferenz
| Methode | Beschreibung |
|---|---|
Get | Datei auf lokalen Pfad oder TStream herunterladen |
Put | Datei aus lokalem Pfad oder TStream hochladen |
Delete | Remote-Datei löschen |
Rename | Remote-Datei umbenennen oder verschieben |
ListDirectory | Verzeichnisinhalt mit Metadaten auflisten |
MakeDirectory | Remote-Verzeichnis erstellen |
Stat / LStat | Dateiattribute abrufen (mit/ohne Symlink-Auflösung) |
FileExists / DirectoryExists | Prüfen, ob Datei oder Verzeichnis existiert |
Symlink / ReadLink | Symbolische Links erstellen oder lesen |
GetFileAsString / PutFileFromString | String-basierte Komfortmethoden |
