sftp

package
v3.8.56 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package sftp 提供基于 github.com/pkg/sftp 与 golang.org/x/crypto/ssh 的 SFTP 客户端封装。

Config 配置主机、端口、账号与超时,New 建立 SSH 连接并打开 SFTP 通道;Client 提供单文件与 递归目录的上传下载、传输进度统计以及常用远端文件系统操作。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// SSHClient 为底层 SSH 客户端连接。
	SSHClient *ssh.Client
	// SFTPClient 为底层 SFTP 客户端通道。
	SFTPClient *sftp.Client
}

Client 封装已建立的 SSH 连接与打开的 SFTP 通道,提供远端文件操作。

零值不可直接使用,须由 Config.New 创建;SSHClient 与 SFTPClient 均为底层句柄,可直接调用。

func (*Client) Chmod

func (client *Client) Chmod(path string, mode os.FileMode) error

Chmod 修改指定远端文件或目录的权限位。

func (*Client) Chown

func (client *Client) Chown(path string, uid, gid int) error

Chown 修改指定文件的属主用户与属组。

func (*Client) Close

func (client *Client) Close() (errors []error)

Close 依次关闭 SFTP 通道与 SSH 连接,返回各自关闭过程中遇到的全部错误。

func (*Client) DownFile

func (client *Client) DownFile(serverPath, destPath string) (int64, error)

DownFile 将 serverPath 处的远程文件下载到本地 destPath,返回写入的字节数。

func (*Client) DownFileWithProgress

func (client *Client) DownFileWithProgress(serverPath, destPath string, transferred *int64, total *int64) (int64, error)

DownFileWithProgress 下载 serverPath 处的远程文件到 destPath,通过 transferred 累计已传输 字节、通过 total 回填文件总大小。

func (*Client) GetRecursively

func (client *Client) GetRecursively(remotePath, localPath string) error

GetRecursively 递归下载 remotePath 目录到本地 localPath;本地同名文件已存在且非目录时返回错误。

func (*Client) Getwd

func (client *Client) Getwd() (string, error)

Getwd 返回服务器当前工作目录;涉及相对路径的操作均以该目录为基准。

func (*Client) Mkdir

func (client *Client) Mkdir(path string) error

Mkdir 创建指定目录;同名文件或目录已存在,或父目录不存在时返回错误,不会递归创建完整路径。

func (*Client) MkdirAll

func (client *Client) MkdirAll(path string) error

MkdirAll 创建 path 目录及其全部父目录;path 已是目录时不做处理并返回 nil, 路径中包含普通文件时返回错误。

func (*Client) PosixRename

func (client *Client) PosixRename(oldname, newname string) error

PosixRename 使用 posix-rename@openssh.com 扩展重命名文件;目标已存在时会被直接替换。

func (*Client) PutRecursively

func (client *Client) PutRecursively(localPath string, remotePath string) error

PutRecursively 递归上传本地 localPath 目录到远程 remotePath;远程同名文件已存在且非目录时返回错误。

func (*Client) Quit

func (client *Client) Quit() (errors []error)

Quit 是 Close 的别名,关闭全部连接。

func (*Client) Remove

func (client *Client) Remove(path string) error

Remove 删除指定文件或目录;路径不存在,或目录非空时返回错误。

func (*Client) RemoveDirectory

func (client *Client) RemoveDirectory(path string) error

RemoveDirectory 删除指定的空目录。

func (*Client) Rename

func (client *Client) Rename(oldname, newname string) error

Rename 重命名远端文件或目录。

func (*Client) UploadFile

func (client *Client) UploadFile(localFilepath string, remoteFilepath string) (int64, error)

UploadFile 将本地 localFilepath 单文件上传到远程 remoteFilepath,返回写入的字节数。

func (*Client) UploadFileWithProgress

func (client *Client) UploadFileWithProgress(localFilepath string, remoteFilepath string, transferred *int64, total *int64) (int64, error)

UploadFileWithProgress 上传本地文件并通过 transferred 累计已传输字节、通过 total 回填文件总大小。

type Config

type Config struct {
	// Host 为服务器主机名或 IP 地址。
	Host string
	// Port 为服务端口,字符串形式。
	Port string
	// User 为登录用户名。
	User string
	// Password 为登录密码。
	Password string
	// Timeout 为 SSH 拨号超时时间。
	Timeout time.Duration
}

Config 保存 SFTP 服务器的连接配置。

func (*Config) New

func (config *Config) New() (*Client, error)

New 依据配置建立 SSH 连接并打开 SFTP 通道;拨号或通道创建失败时返回错误。

当前 ClientConfig 未设置 HostKeyCallback;按 x/crypto/ssh 的校验规则,主机密钥回调为空时 ssh.Dial 会直接返回 “must specify HostKeyCallback” 错误。

type IOReaderProgress

type IOReaderProgress struct {
	io.Reader
	// TransferredBytes 指向已传输字节总数,由 Read 回调持续累加。
	TransferredBytes *int64
}

IOReaderProgress 在读取流的同时累计已传输字节数,供上传下载进度展示使用。

func (*IOReaderProgress) Read

func (iorp *IOReaderProgress) Read(p []byte) (int, error)

Read 转发底层 Reader 的读取,并把本次读取的字节数累加到 TransferredBytes。

Jump to

Keyboard shortcuts

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