2022-09-16 10:53:45 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2023-06-08 10:58:15 +00:00
|
|
|
"bufio"
|
2022-09-16 10:53:45 +00:00
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/pkg/sftp"
|
|
|
|
"golang.org/x/crypto/ssh"
|
|
|
|
)
|
|
|
|
|
|
|
|
type sftpClient struct {
|
2023-12-01 09:50:09 +00:00
|
|
|
bucket string
|
|
|
|
connInfo string
|
|
|
|
config *ssh.ClientConfig
|
2022-09-16 10:53:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewSftpClient(vars map[string]interface{}) (*sftpClient, error) {
|
2023-12-01 09:50:09 +00:00
|
|
|
address := loadParamFromVars("address", true, vars)
|
|
|
|
port := loadParamFromVars("port", false, vars)
|
|
|
|
password := loadParamFromVars("password", true, vars)
|
|
|
|
username := loadParamFromVars("username", true, vars)
|
|
|
|
bucket := loadParamFromVars("bucket", true, vars)
|
|
|
|
|
|
|
|
auth := []ssh.AuthMethod{ssh.Password(password)}
|
|
|
|
clientConfig := &ssh.ClientConfig{
|
|
|
|
User: username,
|
|
|
|
Auth: auth,
|
|
|
|
Timeout: 30 * time.Second,
|
|
|
|
HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
|
|
|
|
return nil
|
|
|
|
},
|
2022-09-16 10:53:45 +00:00
|
|
|
}
|
2023-11-30 13:50:08 +00:00
|
|
|
|
2023-12-01 09:50:09 +00:00
|
|
|
return &sftpClient{bucket: bucket, connInfo: fmt.Sprintf("%s:%s", address, port), config: clientConfig}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s sftpClient) Upload(src, target string) (bool, error) {
|
|
|
|
sshClient, err := ssh.Dial("tcp", s.connInfo, s.config)
|
2022-09-16 10:53:45 +00:00
|
|
|
if err != nil {
|
2023-12-01 09:50:09 +00:00
|
|
|
return false, err
|
2022-09-16 10:53:45 +00:00
|
|
|
}
|
2023-12-01 09:50:09 +00:00
|
|
|
client, err := sftp.NewClient(sshClient)
|
2022-09-16 10:53:45 +00:00
|
|
|
if err != nil {
|
2023-12-01 09:50:09 +00:00
|
|
|
return false, err
|
2022-09-16 10:53:45 +00:00
|
|
|
}
|
2023-12-01 09:50:09 +00:00
|
|
|
defer client.Close()
|
|
|
|
defer sshClient.Close()
|
2023-11-30 13:50:08 +00:00
|
|
|
|
2022-09-16 10:53:45 +00:00
|
|
|
srcFile, err := os.Open(src)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
defer srcFile.Close()
|
|
|
|
|
2023-12-01 09:50:09 +00:00
|
|
|
targetFilePath := s.bucket + "/" + target
|
2023-11-30 13:50:08 +00:00
|
|
|
targetDir, _ := path.Split(targetFilePath)
|
2023-12-01 09:50:09 +00:00
|
|
|
if _, err = client.Stat(targetDir); err != nil {
|
2022-09-16 10:53:45 +00:00
|
|
|
if os.IsNotExist(err) {
|
2023-12-01 09:50:09 +00:00
|
|
|
if err = client.MkdirAll(targetDir); err != nil {
|
2022-09-16 10:53:45 +00:00
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
}
|
2023-12-01 09:50:09 +00:00
|
|
|
dstFile, err := client.Create(targetFilePath)
|
2022-09-16 10:53:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
defer dstFile.Close()
|
2023-06-08 10:58:15 +00:00
|
|
|
|
|
|
|
reader := bufio.NewReaderSize(srcFile, 128*1024*1024)
|
|
|
|
for {
|
|
|
|
chunk, err := reader.Peek(8 * 1024 * 1024)
|
|
|
|
if len(chunk) != 0 {
|
|
|
|
_, _ = dstFile.Write(chunk)
|
|
|
|
_, _ = reader.Discard(len(chunk))
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
2022-09-16 10:53:45 +00:00
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s sftpClient) ListBuckets() ([]interface{}, error) {
|
|
|
|
var result []interface{}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s sftpClient) Download(src, target string) (bool, error) {
|
2023-12-01 09:50:09 +00:00
|
|
|
sshClient, err := ssh.Dial("tcp", s.connInfo, s.config)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
client, err := sftp.NewClient(sshClient)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
defer client.Close()
|
|
|
|
defer sshClient.Close()
|
|
|
|
srcFile, err := client.Open(s.bucket + "/" + src)
|
2022-09-16 10:53:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
defer srcFile.Close()
|
|
|
|
|
|
|
|
dstFile, err := os.Create(target)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
defer dstFile.Close()
|
|
|
|
|
|
|
|
if _, err = srcFile.WriteTo(dstFile); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s sftpClient) Exist(path string) (bool, error) {
|
2023-12-01 09:50:09 +00:00
|
|
|
sshClient, err := ssh.Dial("tcp", s.connInfo, s.config)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
client, err := sftp.NewClient(sshClient)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
defer client.Close()
|
|
|
|
defer sshClient.Close()
|
|
|
|
|
|
|
|
srcFile, err := client.Open(s.bucket + "/" + path)
|
2022-09-16 10:53:45 +00:00
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return false, nil
|
|
|
|
} else {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
defer srcFile.Close()
|
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
|
2023-12-01 09:50:09 +00:00
|
|
|
func (s sftpClient) Size(path string) (int64, error) {
|
|
|
|
sshClient, err := ssh.Dial("tcp", s.connInfo, s.config)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
client, err := sftp.NewClient(sshClient)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
defer client.Close()
|
|
|
|
defer sshClient.Close()
|
|
|
|
|
|
|
|
files, err := client.Stat(s.bucket + "/" + path)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return files.Size(), nil
|
|
|
|
}
|
|
|
|
|
2022-09-16 10:53:45 +00:00
|
|
|
func (s sftpClient) Delete(filePath string) (bool, error) {
|
2023-12-01 09:50:09 +00:00
|
|
|
sshClient, err := ssh.Dial("tcp", s.connInfo, s.config)
|
|
|
|
if err != nil {
|
2022-09-16 10:53:45 +00:00
|
|
|
return false, err
|
|
|
|
}
|
2023-12-01 09:50:09 +00:00
|
|
|
client, err := sftp.NewClient(sshClient)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
defer client.Close()
|
|
|
|
defer sshClient.Close()
|
2022-09-16 10:53:45 +00:00
|
|
|
|
2023-12-01 09:50:09 +00:00
|
|
|
targetFilePath := s.bucket + "/" + filePath
|
|
|
|
if err := client.Remove(targetFilePath); err != nil {
|
|
|
|
return false, err
|
2022-09-16 10:53:45 +00:00
|
|
|
}
|
2023-12-01 09:50:09 +00:00
|
|
|
return true, nil
|
|
|
|
}
|
2022-09-16 10:53:45 +00:00
|
|
|
|
2023-12-01 09:50:09 +00:00
|
|
|
func (s sftpClient) ListObjects(prefix string) ([]string, error) {
|
|
|
|
sshClient, err := ssh.Dial("tcp", s.connInfo, s.config)
|
|
|
|
if err != nil {
|
2022-09-16 10:53:45 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2023-12-01 09:50:09 +00:00
|
|
|
client, err := sftp.NewClient(sshClient)
|
|
|
|
if err != nil {
|
2022-09-16 10:53:45 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2023-12-01 09:50:09 +00:00
|
|
|
defer client.Close()
|
|
|
|
defer sshClient.Close()
|
2022-09-16 10:53:45 +00:00
|
|
|
|
2023-12-01 09:50:09 +00:00
|
|
|
files, err := client.ReadDir(s.bucket + "/" + prefix)
|
2022-09-28 10:11:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-08-08 08:44:12 +00:00
|
|
|
var result []string
|
2022-09-28 10:11:36 +00:00
|
|
|
for _, file := range files {
|
|
|
|
result = append(result, file.Name())
|
|
|
|
}
|
|
|
|
return result, nil
|
2022-09-27 16:08:21 +00:00
|
|
|
}
|