Published 2025-10-03

A robust Dart package for interacting with SFTP servers. This package provides a comprehensive set of functionalities for file operations between local and remote systems using the SFTP protocol.
Add pkg_hh_sftp_client as a dependency in your pubspec.yaml file:
dependencies:
pkg_hh_sftp_client: ^latest_version
Then run:
dart pub get
Or for Flutter projects:
flutter pub get
import 'package:pkg_hh_sftp_client/pkg_hh_sftp_client.dart';
void main() async {
// Configure the SFTP connection
final sftpConnector = ModelSftpFileConnector(
sftpHost: 'your_sftp_host.com',
sftpPort: 22, // Default is 22
sftpUsername: 'your_username',
sftpPasswordOrKey: 'your_password', // For password authentication
connectRetries: 3, // Optional: Number of connection retry attempts
connectTimeout: Duration(seconds: 30), // Optional: Connection timeout
keepAliveInterval: Duration(seconds: 60), // Optional: Keep-alive interval
debug: false, // Optional: Enable debug logs
);
final client = HhSftpClient(conn: sftpConnector);
try {
print('Connecting to SFTP server...');
bool connected = await client.connect();
if (connected) {
print('Successfully connected!');
// Implement your logic here
// Always disconnect when done
client.disconnect();
} else {
print('Failed to connect to SFTP server.');
}
} catch (e) {
print('An error occurred: $e');
}
}
final sftpConnector = ModelSftpFileConnector(
sftpHost: 'your_sftp_host.com',
sftpUsername: 'your_username',
sftpPasswordOrKey: 'your_password',
);
final sftpConnector = ModelSftpFileConnector(
sftpHost: 'your_sftp_host.com',
sftpUsername: 'your_username',
sftpPasswordOrKey: '/path/to/your/private_key',
);
final sftpConnector = ModelSftpFileConnector(
sftpHost: 'your_sftp_host.com',
sftpUsername: 'your_username',
sftpPasswordOrKey: '''-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEAl6eJT7hnTHTR2xPQWAiGxveoA...
...your private key content...
-----END RSA PRIVATE KEY-----''',
);
// List files in a directory (recursive by default)
List<String> files = await client.listFiles(dirPath: '/remote/path');
// List files without recursion
List<String> topLevelFiles = await client.listFiles(
dirPath: '/remote/path',
recursive: false,
);
bool success = await client.downloadFile(
fullRemoteFilePath: '/remote/path/file.txt',
fullLocalFilePath: '/local/path/file.txt',
);
if (success) {
print('File downloaded successfully');
} else {
print('Download failed');
}
bool success = await client.uploadFile(
fullLocalFilePath: '/local/path/file.txt',
fullRemoteFilePath: '/remote/path/file.txt',
);
if (success) {
print('File uploaded successfully');
} else {
print('Upload failed');
}
// Move remote file
bool remoteSuccess = await client.moveRemoteFile(
fullRemoteFilePath: '/remote/source/file.txt',
fullTargetRemoteFilePath: '/remote/target/file.txt',
);
// Move local file
bool localSuccess = await client.moveLocalFile(
fullLocalFilePath: '/local/source/file.txt',
fullTargetLocalFilePath: '/local/target/file.txt',
);
// Remove remote file
bool remoteSuccess = await client.removeRemoteFile(
fullRemoteFilePath: '/remote/path/file.txt',
);
// Remove local file
bool localSuccess = await client.removeLocalFile(
fullLocalFilePath: '/local/path/file.txt',
);
You can specify custom SSH algorithms if needed:
import 'package:dartssh2/dartssh2.dart';
final sftpConnector = ModelSftpFileConnector(
// Basic configuration...
algorithms: const SSHAlgorithms(
mac: <SSHMacType>[
SSHMacType.hmacSha256_96, // hmac-sha2-256-96
],
kex: <SSHKexType>[
SSHKexType.x25519, // curve25519-sha256@libssh.org
],
cipher: <SSHCipherType>[
SSHCipherType.aes128ctr, // aes128-ctr
],
hostkey: <SSHHostkeyType>[
SSHHostkeyType.rsaSha256 // rsa-sha2-256
],
),
);
The package provides custom exception classes for better error handling:
try {
await client.connect();
// other operations...
} on SftpConnectionException catch (e) {
print('Connection error: ${e.message}');
} on SftpInvalidInputException catch (e) {
print('Invalid input: ${e.message}');
} catch (e) {
print('Other error: $e');
}
For a complete example that demonstrates all features, see the example file in the repository.
If you encounter connection issues:
final sftpConnector = ModelSftpFileConnector(
// Other settings...
debug: true
);
Contributions are welcome! Please feel free to submit a Pull Request or open an issue on the GitHub repository.
This package is available under the MIT License.