sftp
SFTP Package
The SFTP package provides a SFTP client with connection pooling, flexible authentication, and advanced file transfer capabilities. Built for reliability and ease of use, it simplifies secure file operations in distributed systems.
Features
Unified SFTP Client Interface
Complete file operations:
Upload,Download,List,Mkdir,Remove,Rename,StatContext-aware operations with cancellation support
Connection Pooling
Efficient connection reuse with automatic management
Configurable pool size and idle timeout
Health checks and automatic cleanup
Thread-safe concurrent access
Flexible Authentication
Password authentication
Private key authentication (file or in-memory)
Passphrase-protected private keys
Extensible authentication handlers
Advanced File Transfer
Configurable buffer size for optimal performance
Real-time progress callbacks
Smart overwrite policies (always, never, if newer, if different size)
Automatic directory creation
Permission preservation
Reliability & Retry
Built-in retry mechanism with exponential backoff
Configurable retry policies
Connection health monitoring
Graceful error handling
Installation
Documentation
For detailed API documentation, examples, and usage patterns, visit the Go Package Documentation.
Usage
Client Interface
The core of the package is the Client interface:
🚀 Quick Start
Creating an SFTP Client
Password Authentication:
Private Key Authentication:
📤 Uploading Files
Basic Upload:
Upload with Options:
📥 Downloading Files
Basic Download:
Download with Options:
📁 Directory Operations
Configuration
SFTP client behavior can be customized via the Config struct:
Configuration Options
AuthConfig
Host
string
SFTP server hostname or IP
Required
Port
int
SFTP server port
22
Username
string
Authentication username
Required
Method
AuthMethod
Auth method (AuthPassword or AuthPrivateKey)
Required
Password
string
Password (for AuthPassword)
-
PrivateKeyPath
string
Path to private key file
-
PrivateKeyData
[]byte
Private key data (in-memory)
-
HostKeyCallback
ssh.HostKeyCallback
Server verification callback
Insecure (accept all)
ConnectionConfig
Timeout
time.Duration
Connection timeout
30s
MaxConnections
int
Max connections in pool
10
IdleTimeout
time.Duration
Idle connection timeout
5m
RetryPolicy
retry.Config
Retry configuration
Exponential backoff
TransferConfig
BufferSize
int
Transfer buffer size (bytes)
32768 (32KB)
CreateDirs
bool
Auto-create directories
false
PreservePermissions
bool
Preserve file permissions
false
ProgressCallback
ProgressCallback
Progress reporting function
nil
Overwrite Policies
Control how the client handles existing files:
OverwriteAlways- Always overwrite existing files (default)OverwriteNever- Never overwrite, return error if existsOverwriteIfNewer- Overwrite only if source is newerOverwriteIfDifferentSize- Overwrite only if sizes differOverwriteIfNewerOrDifferentSize- Overwrite if newer OR different size
Connection Pool Management
Connections are automatically managed The pool will:
Reuse existing connections when available
Create new connections up to MaxConnections limit
Automatically close idle connections after IdleTimeout
Perform health checks before returning connections
Error Handling
Common Errors
ErrConfiguration– Invalid configurationErrAuthentication– Authentication failureErrConnection– Connection or network issuesErrConnectionPoolFull– No available connections in the poolErrConnectionPoolClosed– Connection pool is closedErrFileNotFound– File does not exist, either remotely or locallyErrDataTransfer– Errors during data transfer, including overwrite policy violations
Example
You can find a complete working example in the repository under framework/sftp/example.
Last updated