Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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 this issue Dec 27, 2023 · 3 comments

Comments

@aashakabra
Copy link

aashakabra commented Dec 27, 2023

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
Copy link
Collaborator

You should be able to use sftp.Client.Glob

@aashakabra
Copy link
Author

aashakabra commented Jan 8, 2024

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
Copy link
Collaborator

puellanivis commented Jan 9, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants