Help us shape the future of Go! Take the 2026 Go Developer Survey

sftp

package
v0.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 25, 2026 License: MIT Imports: 16 Imported by: 1

Documentation

Overview

Package sftp provides an SFTP backend for omnistorage.

Basic usage with password authentication:

backend, err := sftp.New(sftp.Config{
    Host:     "example.com",
    User:     "username",
    Password: "password",
})

With SSH key authentication:

backend, err := sftp.New(sftp.Config{
    Host:    "example.com",
    User:    "username",
    KeyFile: "/path/to/id_rsa",
})

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrHostRequired = errors.New("sftp: host is required")
	ErrUserRequired = errors.New("sftp: user is required")
)

Errors specific to the SFTP backend.

Functions

func NewFromConfig

func NewFromConfig(configMap map[string]string) (omnistorage.Backend, error)

NewFromConfig creates a new SFTP backend from a config map. This is used by the omnistorage registry.

Types

type Backend

type Backend struct {
	// contains filtered or unexported fields
}

Backend implements omnistorage.ExtendedBackend for SFTP.

func New

func New(cfg Config) (*Backend, error)

New creates a new SFTP backend with the given configuration.

func (*Backend) Close

func (b *Backend) Close() error

Close releases any resources held by the backend.

func (*Backend) Copy

func (b *Backend) Copy(ctx context.Context, src, dst string) error

Copy copies a file.

func (*Backend) Delete

func (b *Backend) Delete(ctx context.Context, p string) error

Delete removes a path.

func (*Backend) Exists

func (b *Backend) Exists(ctx context.Context, p string) (bool, error)

Exists checks if a path exists.

func (*Backend) Features

func (b *Backend) Features() omnistorage.Features

Features returns the capabilities of the SFTP backend.

func (*Backend) List

func (b *Backend) List(ctx context.Context, prefix string) ([]string, error)

List lists paths with the given prefix.

func (*Backend) Mkdir

func (b *Backend) Mkdir(ctx context.Context, p string) error

Mkdir creates a directory.

func (*Backend) Move

func (b *Backend) Move(ctx context.Context, src, dst string) error

Move moves a file.

func (*Backend) NewReader

func (b *Backend) NewReader(ctx context.Context, p string, opts ...omnistorage.ReaderOption) (io.ReadCloser, error)

NewReader creates a reader for the given path.

func (*Backend) NewWriter

func (b *Backend) NewWriter(ctx context.Context, p string, opts ...omnistorage.WriterOption) (io.WriteCloser, error)

NewWriter creates a writer for the given path.

func (*Backend) Rmdir

func (b *Backend) Rmdir(ctx context.Context, p string) error

Rmdir removes an empty directory.

func (*Backend) Stat

Stat returns metadata about an object.

type Config

type Config struct {
	// Host is the SFTP server hostname or IP address (required).
	Host string

	// Port is the SSH port. Default: 22.
	Port int

	// User is the SSH username (required).
	User string

	// Password is the SSH password.
	// Either Password or KeyFile must be provided.
	Password string

	// KeyFile is the path to an SSH private key file.
	// Either Password or KeyFile must be provided.
	KeyFile string

	// KeyPassphrase is the passphrase for encrypted private keys.
	KeyPassphrase string

	// Root is the base directory on the remote server.
	// All paths are relative to this directory.
	Root string

	// KnownHostsFile is the path to the known_hosts file.
	// If empty, host key verification is disabled (insecure).
	KnownHostsFile string

	// Timeout is the connection timeout in seconds.
	// Default: 30.
	Timeout int

	// Concurrency is the maximum number of concurrent operations.
	// Default: 5.
	Concurrency int
}

Config holds configuration for the SFTP backend.

func ConfigFromEnv

func ConfigFromEnv() Config

ConfigFromEnv creates a Config from environment variables. Environment variables:

  • OMNISTORAGE_SFTP_HOST: server hostname
  • OMNISTORAGE_SFTP_PORT: SSH port (default: 22)
  • OMNISTORAGE_SFTP_USER: username
  • OMNISTORAGE_SFTP_PASSWORD: password
  • OMNISTORAGE_SFTP_KEY_FILE: path to private key
  • OMNISTORAGE_SFTP_KEY_PASSPHRASE: passphrase for encrypted key
  • OMNISTORAGE_SFTP_ROOT: base directory
  • OMNISTORAGE_SFTP_KNOWN_HOSTS: path to known_hosts file
  • OMNISTORAGE_SFTP_TIMEOUT: connection timeout in seconds

func ConfigFromMap

func ConfigFromMap(m map[string]string) Config

ConfigFromMap creates a Config from a string map. Supported keys:

  • host: server hostname (required)
  • port: SSH port (default: 22)
  • user: username (required)
  • pass or password: password
  • key_file: path to private key
  • key_passphrase: passphrase for encrypted key
  • root: base directory
  • known_hosts: path to known_hosts file
  • timeout: connection timeout in seconds
  • concurrency: maximum concurrent operations

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns a Config with default values.

func (Config) Validate

func (c Config) Validate() error

Validate checks if the configuration is valid.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL