sftp icon indicating copy to clipboard operation
sftp copied to clipboard

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

Open aashakabra opened this issue 2 years ago • 3 comments

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

aashakabra avatar Dec 27 '23 12:12 aashakabra

You should be able to use sftp.Client.Glob

puellanivis avatar Dec 28 '23 20:12 puellanivis

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)
}

aashakabra avatar Jan 08 '24 20:01 aashakabra

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.

puellanivis avatar Jan 09 '24 14:01 puellanivis