2013-11-06 16:42:50 +08:00
|
|
|
package sftp_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"code.google.com/p/go.crypto/ssh"
|
|
|
|
|
|
|
|
"github.com/pkg/sftp"
|
|
|
|
)
|
|
|
|
|
2014-04-10 20:37:10 +08:00
|
|
|
func Example(conn *ssh.Client) {
|
2014-09-30 06:39:25 +08:00
|
|
|
// open an SFTP session over an existing ssh connection.
|
2013-11-06 16:42:50 +08:00
|
|
|
sftp, err := sftp.NewClient(conn)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
defer sftp.Close()
|
|
|
|
|
|
|
|
// walk a directory
|
|
|
|
w := sftp.Walk("/home/user")
|
|
|
|
for w.Step() {
|
|
|
|
if w.Err() != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
log.Println(w.Path())
|
|
|
|
}
|
|
|
|
|
|
|
|
// leave your mark
|
|
|
|
f, err := sftp.Create("hello.txt")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
if _, err := f.Write([]byte("Hello world!")); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2013-11-06 16:48:37 +08:00
|
|
|
// check it's there
|
2013-11-06 16:42:50 +08:00
|
|
|
fi, err := sftp.Lstat("hello.txt")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
log.Println(fi)
|
|
|
|
}
|