pkg / sftp

SFTP support for the go.crypto/ssh package
BSD 2-Clause "Simplified" License
1.52k stars 380 forks source link

How to recursively download directories and files on the sftp server? #427

Closed Aquarian-Age closed 3 years ago

Aquarian-Age commented 3 years ago

There are the following sample directories

.
├── a
│   └── b
│       └── c
├── dir.go
├── get-docker.sh
├── go.mod
├── install.sh
├── tcp.sh
├── tcp.sh.txt
├── testdir
│   └── 1
│       ├── 1.txt
│       └── 2
│           ├── 2.txt
│           └── 3
│               ├── 3.txt
│               └── 4
│                   ├── 4.txt
│                   └── 5
│                       └── 5.txt
├── testdir.txt
├── test.txt

How can I download it locally to keep the directory and files unchanged?

puellanivis commented 3 years ago

A recursive process of sftp.ReadDir to list the files in the root, and then either: an os.Mkdir of the subdirectory, followd by a recursive call on the subdirectory; or an r, err := sftp.Open() with a parallel-filename w, err := os.Create() followed by a io.Copy(w, r) and closing the files. The details are left super vague here, because this is a library for providing API access to SFTP.

If you just want to copy the files, I would use scp -r remotehost:dir . or some other readdy-made tool already available to do so… there really isn’t much a need for you to roll your own.

Aquarian-Age commented 3 years ago

A recursive process of sftp.ReadDir to list the files in the root, and then either: an os.Mkdir of the subdirectory, followd by a recursive call on the subdirectory; or an r, err := sftp.Open() with a parallel-filename w, err := os.Create() followed by a io.Copy(w, r) and closing the files. The details are left super vague here, because this is a library for providing API access to SFTP.

If you just want to copy the files, I would use scp -r remotehost:dir . or some other readdy-made tool already available to do so… there really isn’t much a need for you to roll your own.

Thanks Reply scp works well in linux but cannot be used in windows7 system So I wrote a simple file transfer function

puellanivis commented 3 years ago

For windows I would recomment PuTTY which has a pscp binary that does scp on Windows.