sftp

Contributions Welcomearrow-up-right Releasearrow-up-right

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, Stat

    • Context-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

Go Referencearrow-up-right

For detailed API documentation, examples, and usage patterns, visit the Go Package Documentationarrow-up-right.

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

Field
Type
Description
Default

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

Field
Type
Description
Default

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

Field
Type
Description
Default

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 exists

  • OverwriteIfNewer - Overwrite only if source is newer

  • OverwriteIfDifferentSize - Overwrite only if sizes differ

  • OverwriteIfNewerOrDifferentSize - 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 configuration

  • ErrAuthentication – Authentication failure

  • ErrConnection – Connection or network issues

  • ErrConnectionPoolFull – No available connections in the pool

  • ErrConnectionPoolClosed – Connection pool is closed

  • ErrFileNotFound – File does not exist, either remotely or locally

  • ErrDataTransfer – Errors during data transfer, including overwrite policy violations

Example

You can find a complete working example in the repository under framework/sftp/examplearrow-up-right.

Last updated