sftp

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultConcurrentFiles = 5
	DefaultThreadsPerFile  = 64
	DefaultChunkSize       = 32 * 1024
	DefaultResumeMinSize   = 1024 * 1024
	DefaultTempSuffix      = ".xops-tmp"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

func NewClient

func NewClient(ctx context.Context, sshCli *ssh.Client, opts ...Option) (*Client, error)

NewClient creates an SFTP subsystem on an existing SSH connection. Setup is bounded by ctx and an internal maximum timeout, including subsystem and protocol handshakes.

func (*Client) Close

func (c *Client) Close() error

Close closes this SFTP subsystem. A blocked subsystem close may close the shared SSH transport as a bounded last-resort interrupt.

func (*Client) Config added in v0.6.0

func (c *Client) Config() TransferConfig

Config 返回当前传输配置

func (*Client) Cwd

func (c *Client) Cwd(ctx context.Context) (cwd string, retErr error)

Cwd returns the remote working directory using the caller's cancellation boundary.

func (*Client) Do added in v0.12.0

func (c *Client) Do(ctx context.Context, operation func(*sftp.Client) error) error

Do runs a low-level SFTP operation with context cancellation. It deliberately adds no command policy or presentation concerns; callers retain ownership of operation-specific error context. Cancellation interrupts this SFTP subsystem, so the client must not be reused afterwards.

func (*Client) Download

func (c *Client) Download(ctx context.Context, remotePath, localPath string, progress ProgressCallback) error

Download 下载入口:支持文件或目录

func (*Client) DownloadDirectory

func (c *Client) DownloadDirectory(ctx context.Context, remoteDir, localDir string, progress ProgressCallback) error

func (*Client) DownloadFile

func (c *Client) DownloadFile(ctx context.Context, remotePath, localPath string, size int64, mode os.FileMode, progress ProgressCallback) (retErr error)

func (*Client) JoinPath

func (c *Client) JoinPath(elem ...string) string

JoinPath 辅助函数:处理远程路径拼接 (SFTP 协议强制使用 forward slash)

func (*Client) RemoteCopy added in v0.12.0

func (c *Client) RemoteCopy(ctx context.Context, src, dst string) (retErr error)

RemoteCopy copies a remote file, directory, or symbolic link through SFTP. Symbolic links are copied as links and are never followed during traversal.

func (*Client) RemoveAll added in v0.12.0

func (c *Client) RemoveAll(ctx context.Context, remotePath string) error

RemoveAll removes a remote file, directory tree, or symbolic link through SFTP. Symbolic links are removed as links and are never followed.

func (*Client) Rename added in v0.12.0

func (c *Client) Rename(ctx context.Context, src, dst string) error

Rename renames one remote entry through SFTP.

func (*Client) ReplaceRemoteFile added in v0.12.0

func (c *Client) ReplaceRemoteFile(ctx context.Context, tempPath, remotePath string) error

ReplaceRemoteFile promotes a temporary remote file to its destination while preserving an existing destination when the replacement fails. Cancellation interrupts this SFTP subsystem, so the client must not be reused afterwards.

func (*Client) SFTPClient

func (c *Client) SFTPClient() *sftp.Client

SFTPClient returns the underlying *sftp.Client.

func (*Client) StreamTransfer

func (c *Client) StreamTransfer(ctx context.Context, source *Client, r io.Reader, w io.Writer, size int64, progress ProgressCallback) error

StreamTransfer copies exactly size bytes between two SFTP clients. The caller retains ownership of r and w. Cancellation interrupts both clients' SFTP subsystems, so both SFTP clients become unusable afterwards. Shared SSH transports remain open unless a subsystem close itself blocks and requires the bounded transport fallback documented on Client.

func (*Client) Upload

func (c *Client) Upload(ctx context.Context, localPath, remotePath string, progress ProgressCallback) error

Upload 上传入口:支持文件或目录

func (*Client) UploadDirectory

func (c *Client) UploadDirectory(ctx context.Context, localDir, remoteDir string, progress ProgressCallback) error

func (*Client) UploadFile

func (c *Client) UploadFile(ctx context.Context, localPath, remotePath string, size int64, mode os.FileMode, progress ProgressCallback) (retErr error)

func (*Client) WithForce added in v0.12.0

func (c *Client) WithForce(force bool) *Client

WithForce returns a new Client instance sharing the same underlying connection but with the specified Force configuration. This is safe for concurrent use.

type Option

type Option func(*Client)

func WithChunkSize

func WithChunkSize(size int) Option

func WithConcurrentFiles

func WithConcurrentFiles(con int) Option

func WithForce added in v0.6.0

func WithForce(force bool) Option

func WithResume added in v0.6.0

func WithResume(enable bool) Option

func WithResumeMinSize added in v0.6.0

func WithResumeMinSize(size int64) Option

func WithThreadsPerFile

func WithThreadsPerFile(t int) Option

type ProgressCallback

type ProgressCallback func(n int64)

ProgressCallback 进度回调,n 为本次增量传输的字节数 此函数必须是并发安全的

type TransferConfig

type TransferConfig struct {
	ConcurrentFiles int   // 同时传输的文件数
	ThreadsPerFile  int   // 单个文件的并发分块数
	ChunkSize       int64 // 分块大小
	EnableResume    bool
	ResumeMinSize   int64
	TempSuffix      string
	Force           bool
}

TransferConfig 定义传输配置

func DefaultConfig

func DefaultConfig() TransferConfig

Jump to

Keyboard shortcuts

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