Documentation
¶
Overview ¶
Package sftp 提供基于 github.com/pkg/sftp 与 golang.org/x/crypto/ssh 的 SFTP 客户端封装。
Config 配置主机、端口、账号与超时,New 建立 SSH 连接并打开 SFTP 通道;Client 提供单文件与 递归目录的上传下载、传输进度统计以及常用远端文件系统操作。
Index ¶
- type Client
- func (client *Client) Chmod(path string, mode os.FileMode) error
- func (client *Client) Chown(path string, uid, gid int) error
- func (client *Client) Close() (errors []error)
- func (client *Client) DownFile(serverPath, destPath string) (int64, error)
- func (client *Client) DownFileWithProgress(serverPath, destPath string, transferred *int64, total *int64) (int64, error)
- func (client *Client) GetRecursively(remotePath, localPath string) error
- func (client *Client) Getwd() (string, error)
- func (client *Client) Mkdir(path string) error
- func (client *Client) MkdirAll(path string) error
- func (client *Client) PosixRename(oldname, newname string) error
- func (client *Client) PutRecursively(localPath string, remotePath string) error
- func (client *Client) Quit() (errors []error)
- func (client *Client) Remove(path string) error
- func (client *Client) RemoveDirectory(path string) error
- func (client *Client) Rename(oldname, newname string) error
- func (client *Client) UploadFile(localFilepath string, remoteFilepath string) (int64, error)
- func (client *Client) UploadFileWithProgress(localFilepath string, remoteFilepath string, transferred *int64, total *int64) (int64, error)
- type Config
- type IOReaderProgress
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) DownFileWithProgress ¶
func (client *Client) DownFileWithProgress(serverPath, destPath string, transferred *int64, total *int64) (int64, error)
DownFileWithProgress 下载 serverPath 处的远程文件到 destPath,通过 transferred 累计已传输 字节、通过 total 回填文件总大小。
func (*Client) GetRecursively ¶
GetRecursively 递归下载 remotePath 目录到本地 localPath;本地同名文件已存在且非目录时返回错误。
func (*Client) PosixRename ¶
PosixRename 使用 posix-rename@openssh.com 扩展重命名文件;目标已存在时会被直接替换。
func (*Client) PutRecursively ¶
PutRecursively 递归上传本地 localPath 目录到远程 remotePath;远程同名文件已存在且非目录时返回错误。
func (*Client) RemoveDirectory ¶
RemoveDirectory 删除指定的空目录。
func (*Client) UploadFile ¶
UploadFile 将本地 localFilepath 单文件上传到远程 remoteFilepath,返回写入的字节数。
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 服务器的连接配置。
type IOReaderProgress ¶
type IOReaderProgress struct {
io.Reader
// TransferredBytes 指向已传输字节总数,由 Read 回调持续累加。
TransferredBytes *int64
}
IOReaderProgress 在读取流的同时累计已传输字节数,供上传下载进度展示使用。