sftp

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultConcurrentFiles = 5
	DefaultThreadsPerFile  = 64
	DefaultChunkSize       = 32 * 1024 // 32KB SFTP 默认包大小优化
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

Client 包装了 sftp.Client,并持有底层的 ssh 连接引用

func NewClient

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

New 基于现有的 SSH 连接创建一个 SFTP 客户端 这里复用了 pkg/ssh 中已经建立好的连接 (包括跳板机隧道)

func (*Client) Close

func (c *Client) Close() error

Close 关闭 SFTP 会话 (注意:这不会关闭底层的 SSH 连接,除非你希望这样)

func (*Client) Cwd

func (c *Client) Cwd() (string, error)

Cwd 获取远程当前工作目录

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) error

func (*Client) JoinPath

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

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

func (*Client) NewShell

func (c *Client) NewShell(stdin io.Reader, stdout, stderr io.Writer) (*Shell, error)

NewShell 创建一个新的交互式 Shell

func (*Client) SFTPClient

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

SFTPClient 返回底层的 *sftp.Client 对象, 允许调用者执行 rename, chmod, stat, symlink 等高级操作。

func (*Client) StreamTransfer

func (c *Client) StreamTransfer(r io.Reader, w io.Writer, progress ProgressCallback) error

StreamTransfer 简单的流式传输兜底

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) error

type Option

type Option func(*Client)

Option 定义配置函数的类型

func WithChunkSize

func WithChunkSize(size int) Option

func WithConcurrentFiles

func WithConcurrentFiles(con int) Option

func WithThreadsPerFile

func WithThreadsPerFile(t int) Option

type ProgressCallback

type ProgressCallback func(n int)

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

type SftpCompleter added in v0.4.0

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

SftpCompleter 实现 readline.AutoCompleter 接口 提供命令名和路径的自动补全功能

func (*SftpCompleter) Do added in v0.4.0

func (c *SftpCompleter) Do(line []rune, pos int) (newLine [][]rune, length int)

Do 执行自动补全,返回候选列表和已匹配长度

type Shell

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

Shell 定义交互式 SFTP 环境

func (*Shell) Run

func (s *Shell) Run(ctx context.Context) error

Run 启动交互式循环 (REPL)

type TransferConfig

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

TransferConfig 定义传输配置

func DefaultConfig

func DefaultConfig() TransferConfig

Jump to

Keyboard shortcuts

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