pkg_hh_sftp_client version: 0.0.26

Published 2025-10-03

reinbeumer

sdk | dart
repository | svn
platform | generic
status | n/a
  • Readme
  • Changelog
  • Versions

HH SFTP Client

Pub Version License: MIT

graph

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.

Features

  • Authentication: Connect to SFTP servers using password, PEM key string, or private key file
  • File Operations:
    • List files and directories (with recursive option)
    • Download files from the SFTP server
    • Upload files to the SFTP server
    • Remove files from the SFTP server or local system
    • Move/rename files on the SFTP server or local system
  • Error Handling: Comprehensive error handling with descriptive exception messages
  • Retry Logic: Built-in connection retry mechanism for enhanced reliability
  • Debugging: Optional debug logging to troubleshoot connection issues

Installation

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

Usage

Basic Connection

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');
  }
}

Authentication Methods

Password Authentication

final sftpConnector = ModelSftpFileConnector(
  sftpHost: 'your_sftp_host.com',
  sftpUsername: 'your_username',
  sftpPasswordOrKey: 'your_password',
);

Private Key Authentication (using key file)

final sftpConnector = ModelSftpFileConnector(
  sftpHost: 'your_sftp_host.com',
  sftpUsername: 'your_username',
  sftpPasswordOrKey: '/path/to/your/private_key',
);

Private Key Authentication (using PEM key string)

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-----''',
);

File Operations

Listing Files

// 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,
);

Downloading Files

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');
}

Uploading Files

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');
}

Moving Files

// 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',
);

Removing Files

// 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',
);

Advanced Configuration

SSH Algorithms

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
    ],
  ),
);

Error Handling

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');
}

Complete Example

For a complete example that demonstrates all features, see the example file in the repository.

Troubleshooting

If you encounter connection issues:

  1. Verify your host, port, username, and password/key are correct
  2. Check if the server is accessible (try manual SFTP connection)
  3. Check if there are any firewall restrictions
  4. Try enabling debug mode for more detailed logs:
    final sftpConnector = ModelSftpFileConnector(
      // Other settings...
      debug: true
    );
    

Contributing

Contributions are welcome! Please feel free to submit a Pull Request or open an issue on the GitHub repository.

License

This package is available under the MIT License.

Changelog

0.0.26 - 2025-10-03

Changed

  • Version bump from 0.0.25 to 0.0.26 (build_runner -> 2.9.0 (was 2.8.0)).

0.0.25 - 2025-09-22

Changed

  • Version bump from 0.0.24 to 0.0.25 (mockito -> 5.5.1 (was 5.5.0)).

0.0.24 - 2025-09-22

Changed

  • Version bump from 0.0.23 to 0.0.24 (build_runner -> 2.8.0 (was 2.7.1)).

0.0.23 - 2025-09-16

Changed

  • Version bump from 0.0.22 to 0.0.23 (xml -> 6.6.1 (was 6.5.0)).

0.0.22 - 2025-09-04

Changed

  • Version bump from 0.0.21 to 0.0.22 (mockito -> 5.5.0 (was 5.5.1)).

0.0.21 - 2025-09-04

Changed

  • Version bump from 0.0.20 to 0.0.21 (mockito -> 5.5.1 (was 5.4.6)).

0.0.20 - 2025-09-04

Changed

  • Version bump from 0.0.19 to 0.0.20 (build_runner -> 2.7.1 (was 2.7.2)).

0.0.19 - 2025-09-04

Changed

  • Version bump from 0.0.18 to 0.0.19 (build_runner -> 2.7.2 (was 2.7.1)).

0.0.18 - 2025-09-03

Changed

  • Version bump from 0.0.17 to 0.0.18 (build_runner -> 2.7.1 (was 2.7.0)).

0.0.17 - 2025-09-01

Changed

  • Version bump from 0.0.16 to 0.0.17 (build_runner -> 2.7.0 (was 2.6.0)).

0.0.16 - 2025-08-08

Changed

  • Version bump from 0.0.15 to 0.0.16 (build_runner -> 2.6.0 (was 2.6.1)).

0.0.15 - 2025-08-08

Changed

  • Version bump from 0.0.14 to 0.0.15 (build_runner -> 2.6.1 (was 2.6.0)).

0.0.14 - 2025-07-28

Changed

  • Version bump from 0.0.13 to 0.0.14 (build_runner -> 2.6.0 (was 2.5.4)).

0.0.13 - 2025-07-23

Changed

  • Version bump from 0.0.12 to 0.0.13 (test -> 1.26.3 (was 1.26.2)).

0.0.12 - 2025-06-25

Changed

  • Version bump from 0.0.11 to 0.0.12 (xml -> 6.5.0 (was 6.6.0)).

0.0.11 - 2025-06-24

Changed

  • Version bump from 0.0.10 to 0.0.11 (build_runner -> 2.5.4 (was 2.5.3)).

0.0.10 - 2025-06-23

Changed

  • Version bump from 0.0.9 to 0.0.10 (build_runner -> 2.5.3 (was 2.5.2)).

0.0.9 - 2025-06-18

Changed

  • Version bump from 0.0.8 to 0.0.9 (build_runner -> 2.5.2 (was 2.4.12)).

0.0.8 - 2025-06-18

Changed

  • Version bump from 0.0.2 to 0.0.3 (build_runner -> 2.4.12 (was 2.5.2)).

0.0.7 - 2025-06-18

Changed

  • Version bump from 0.0.1 to 0.0.2 (xml -> 6.5.0 (was 6.6.0)).

0.0.6 - 2025-06-17

Changed

  • Version bump from 0.0.5 to 0.0.6 (build_runner -> 2.5.1 (was 2.5.0)).

0.0.5 - 2025-06-17

Changed

  • Version bump from 0.0.4 to 0.0.5 (build_runner -> 2.5.0 (was 2.4.15)).

0.0.4 - 2025-05-30

Changed

  • Version bump from 0.0.1 to 0.0.2 (build_runner -> 2.4.15 (was 2.4.10)).

0.0.3 - 2025-05-30

Changed

  • Version bump from 0.0.2 to 0.0.3 (lints -> 6.0.0 (was 5.0.0)).

0.0.2 - 2025-05-30

Changed

  • Version bump from 0.0.1 to 0.0.2 (test -> 1.26.2 (was 1.24.0)).

0.0.1 - 2025-05-30

  • Initial version.

0.0.26

2025-10-03 download

0.0.25

2025-09-22 download

0.0.22

2025-09-04 download

0.0.18

2025-09-03 download

0.0.16

2025-08-08 download

0.0.14

2025-07-28 download

0.0.13

2025-07-23 download

0.0.12

2025-06-25 download

0.0.11

2025-06-24 download

0.0.10

2025-06-23 download

0.0.5

2025-06-17 download

0.0.4

2025-05-30 download