sftp

package
v0.0.0-...-db1a339 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2024 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewAuthError

func NewAuthError(e error) error

NewAuthError returns a pointer to a new AuthError from the underlying |e| error message.

Types

type AsyncUpload

type AsyncUpload interface {
	// Bytes returns the number of bytes copied to the SFTP destination.
	Bytes() int64
	// Close closes SFTP connection used for the upload. Close must be called
	// when the upload is complete to prevent memory leaks.
	Close() error
	// Done returns a channel that receives a true value when the upload is
	// complete.  A done signal should not be sent on error.
	Done() chan bool
	// Done returns a channel that receives an error if the upload encounters
	// an error.
	Err() chan error
	// Write implements the io.Writer interface and adds len(p) to the count of
	// bytes uploaded.
	Write(p []byte) (int, error)
}

AsyncUpload provides information about an upload happening asynchronously in a separate goroutine.

type AsyncUploadImpl

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

AsyncUploadImpl provides an asynchronous upload implementation.

func NewAsyncUpload

func NewAsyncUpload(conn *connection) AsyncUploadImpl

NewAsyncUpload returns an initialized AsyncUploadImpl struct that wraps the underlying SFTP connection.

func (*AsyncUploadImpl) Bytes

func (u *AsyncUploadImpl) Bytes() int64

Bytes returns the current number of bytes uploaded.

func (*AsyncUploadImpl) Close

func (u *AsyncUploadImpl) Close() error

Close closes SFTP connection used for the upload. Close must be called when the upload is complete to prevent memory leaks.

func (*AsyncUploadImpl) Done

func (u *AsyncUploadImpl) Done() chan bool

Done returns a done channel that receives a true value when the upload is complete.

func (*AsyncUploadImpl) Err

func (u *AsyncUploadImpl) Err() chan error

Error returns an error channel that receives an error if the upload encounters an error.

func (*AsyncUploadImpl) Write

func (u *AsyncUploadImpl) Write(p []byte) (int, error)

Write adds the length of p to the total number of bytes written on the connection.

Write implements the io.Writer interface.

type AuthError

type AuthError struct {
	Message string
}

AuthError represents an SFTP authentication error.

func (*AuthError) Error

func (e *AuthError) Error() string

Error implements the error interface.

type Client

type Client interface {
	// Delete removes dest from the SFTP server.
	Delete(ctx context.Context, dest string) error
	// Upload asynchronously copies data from the src reader to the specified
	// dest on the SFTP server.
	Upload(ctx context.Context, src io.Reader, dest string) (remotePath string, upload AsyncUpload, err error)
}

A Client manages the transmission of data over SFTP.

Implementations of the Client interface handle the connection details, authentication, and other intricacies associated with different SFTP servers and protocols.

type Config

type Config struct {
	// Host address, e.g. 127.0.0.1 (default), sftp.example.org.
	Host string

	// User name.
	User string

	// Host port (default: 22).
	Port string

	// Path to known_hosts file as per https://linux.die.net/man/8/sshd
	// "SSH_KNOWN_HOSTS FILE FORMAT" (default: "$HOME/.ssh/known_hosts"). The
	// known_hosts file must include the public key of the SFTP server for
	// authentication to succeed.
	KnownHostsFile string

	// Private key used for authentication.
	PrivateKey PrivateKey

	// Default directory on SFTP server for file transfers.
	RemoteDir string
}

Config represents the configuration needed to connect to an SFTP server.

func (*Config) SetDefaults

func (c *Config) SetDefaults()

SetDefaults sets default values for some configs.

type GoClient

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

GoClient implements the SFTP service using native Go SSH and SFTP packages.

func NewGoClient

func NewGoClient(logger logr.Logger, cfg Config) *GoClient

NewGoClient returns a new GoSFTP client with the given configuration.

func (*GoClient) Delete

func (c *GoClient) Delete(ctx context.Context, dest string) error

Delete removes the data from dest. A new SFTP connection is opened before removing the file, and closed when the delete is complete.

func (*GoClient) Upload

func (c *GoClient) Upload(ctx context.Context, src io.Reader, dest string) (string, AsyncUpload, error)

Upload asynchronously copies the src data to dest over an SFTP connection.

When Upload is called it starts the upload in an asynchronous goroutine, then immediately returns the full remote path, and an AsyncUpload struct that provides access to the upload status and progress.

When the upload completes, the `AsyncUpload.Done()` channel is sent a `true` value. If an error occurs during the upload the error is sent to the `AsyncUpload.Error()` channel and the upload is terminated. If a ctx cancellation signal is received, the `ctx.Err()` error will be sent to the `AsyncUpload.Error()` channel, and the upload is terminated.

type PrivateKey

type PrivateKey struct {
	// Path to private key file used for authentication (default:
	// "$HOME/.ssh/id_rsa")
	Path string

	// Passphrase (if any) used to decrypt the private key.
	Passphrase string
}

PrivateKey represents a SSH private key, with an optional passphrase.

Directories

Path Synopsis
Package fake is a generated GoMock package.
Package fake is a generated GoMock package.

Jump to

Keyboard shortcuts

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