代码拉取完成,页面将自动刷新
package ssh
import (
"errors"
"fmt"
"github.com/pkg/sftp"
"io"
"os"
"time"
)
type Sftp struct {
session *Session
}
func NewSftp(session *Session) *Sftp {
return &Sftp{session: session}
}
func (sf *Sftp) Exec(id int, localFilePath string, remoteFilePath string) *Result {
result := &Result{
Id: id,
Host: sf.session.Host,
LocalPath: localFilePath,
RemotePath: remoteFilePath,
}
start := time.Now()
result.StartTime = start
if err := sf.session.Connect(); err != nil {
result.Error = err
return result
}
var fileSize int64
if s, err := os.Stat(localFilePath); err != nil {
result.Error = err
return result
} else {
fileSize = s.Size()
}
srcFile, err := os.Open(localFilePath)
if err != nil {
result.Error = err
return result
}
defer srcFile.Close()
sftpClient, err := sftp.NewClient(sf.session.Client)
if err != nil {
result.Error = err
return result
}
defer sftpClient.Close()
dstFile, err := sftpClient.Create(remoteFilePath)
if err != nil {
result.Error = err
return result
}
defer dstFile.Close()
n, err := io.Copy(dstFile, io.LimitReader(srcFile, fileSize))
if err != nil {
result.Error = err
return result
}
if n != fileSize {
result.Error = errors.New(fmt.Sprintf("copy: expected %v bytes, got %d", fileSize, n))
return result
}
end := time.Now()
result.EndTime = end
return result
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。