-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.go
91 lines (75 loc) · 2.73 KB
/
example.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package example
import (
"log"
"github.com/potproject/easysftp"
)
func main() {
// Connect Example
// [SFTP Command] $ sftp USERNAME@example.hostname.local -oPort=22 -i ~/.ssh/id_rsa
esftpSession, err := easysftp.Connect("USERNAME", "example.hostname.local", 22, "~/.ssh/id_rsa")
// Alternative: Using *ssh.Client
// esftpSession, err := easysftp.NewClient(conn)
if err != nil {
log.Fatalln(err.Error())
}
defer esftpSession.Close()
// SFTP File Get
// [SFTP Command] sftp> get /tmp/remotefile.txt /tmp/localfile.txt
downloadBytes, downloadError := esftpSession.Get("/tmp/localfile.txt", "/tmp/remotefile.txt")
if downloadError != nil {
log.Fatalln("Download Error:", err.Error())
}
log.Println("Download OK:", downloadBytes)
// SFTP File Put
// [SFTP Command] sftp> put /tmp/localfile.txt /tmp/remotefile.txt
uploadBytes, uploadError := esftpSession.Put("/tmp/localfile.txt", "/tmp/remotefile.txt")
if uploadError != nil {
log.Fatalln("Upload Error:", err.Error())
}
log.Println("Upload OK:", uploadBytes)
}
func exampleRecursively() {
// Recursively Example
// [SFTP Command] $ sftp USERNAME@example.hostname.local -oPort=22 -i ~/.ssh/id_rsa
esftpSession, err := easysftp.Connect("USERNAME", "example.hostname.local", 22, "~/.ssh/id_rsa")
// Alternative: Using *ssh.Client
// esftpSession, err := easysftp.NewClient(conn)
if err != nil {
log.Fatalln(err.Error())
}
defer esftpSession.Close()
// SFTP Directory Get
// [SFTP Command] sftp> get -r /tmp/remoteDirectory /tmp/localDirectory
downloadError := esftpSession.GetRecursively("/tmp/localDirectory", "/tmp/remoteDirectory")
if downloadError != nil {
log.Fatalln("Download Error:", err.Error())
}
log.Println("Download OK")
// SFTP Directory Put
// [SFTP Command] sftp> put -r /tmp/localDirectory /tmp/remoteDirectory
uploadError := esftpSession.PutRecursively("/tmp/localDirectory", "/tmp/remoteDirectory")
if uploadError != nil {
log.Fatalln("Upload Error:", err.Error())
}
log.Println("Upload OK")
}
func exampleWithProgress() {
// Connect Example
// [SFTP Command] $ sftp USERNAME@example.hostname.local -oPort=22 -i ~/.ssh/id_rsa
esftpSession, err := easysftp.Connect("USERNAME", "example.hostname.local", 22, "~/.ssh/id_rsa")
// Alternative: Using *ssh.Client
// esftpSession, err := easysftp.NewClient(conn)
if err != nil {
log.Fatalln(err.Error())
}
defer esftpSession.Close()
// SFTP File Get
// [SFTP Command] sftp> get /tmp/remotefile.txt /tmp/localfile.txt
var transferred int64
var total int64
downloadBytes, downloadError := esftpSession.GetWithProgress("/tmp/localfile.txt", "/tmp/remotefile.txt", &transferred, &total)
if downloadError != nil {
log.Fatalln("Download Error:", err.Error())
}
log.Println("Download OK:", downloadBytes)
}