pkg / sftp

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

How can I perform sftp download and upload feature for multiple files at root directory and within nested subfolder using https://github.com/pkg/sftp #570

Open aashakabra opened 6 months ago

aashakabra commented 6 months ago

Hello,

How can I perform sftp download and upload feature for multiple files at root directory and within nested subfolder using https://github.com/pkg/sftp for wildcards like , abc?.txt, abc.txt and so on.

Currently I have implemented single file transfer from and to sftp server using https://github.com/pkg/sftp

Are there any apis available that will list all the files with specific prefix ? Also, for implementation of creation of nested folders? Any reference that can help with this requirement.

I am able to perform the operation using java third party Jsch library and I am looking to do similar implementation in golang.

Thanks, Asha

puellanivis commented 6 months ago

You should be able to use sftp.Client.Glob

aashakabra commented 6 months ago

Thanks @puellanivis , above works for files but I am facing error for directory name that matches the pattern.

How will I be able to copy the entire folder as well that matches the pattern? For example - folder structure contains a folder named "new" and has two files one.txt and two.txt my pattern is n*

Code snippet that worked for files is -

paths, _:= sc.Glob("n*")

for i := 0; i < len(paths); i++ {
srcFile, _ := sc.OpenFile(paths[i], (os.O_RDONLY))
defer srcFile.Close()

dstFile, _ := os.Create(filepath.Join(<<LocalFolderPath>>, paths[i])) //created the file name at destination folder same as source file name
defer dstFile.Close()

bytes, _ := io.Copy(dstFile, srcFile)
fmt.Println("bytes copied:", bytes)
}
puellanivis commented 6 months ago

You will need to use a recursive function that in its body will os.Stat()s the file and if it is a FileInfo.IsDir() you need to then use sftp.Client.ReadDir() to get a listing of the directory and then add those to the list of paths, or just download them directly.