33 lines
738 B
Go
33 lines
738 B
Go
package fileutils
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
|
|
"github.com/pkg/sftp"
|
|
)
|
|
|
|
// UploadFile uploads a local file to a remote server using SFTP
|
|
func UploadFile(client *sftp.Client, localPath, remotePath string) error {
|
|
// Open the local file
|
|
localFile, err := os.Open(localPath)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to open local file: %w", err)
|
|
}
|
|
defer localFile.Close()
|
|
|
|
// Create the remote file
|
|
remoteFile, err := client.Create(remotePath)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to create remote file: %w", err)
|
|
}
|
|
defer remoteFile.Close()
|
|
|
|
// Copy from the local file to the remote file
|
|
if _, err = io.Copy(remoteFile, localFile); err != nil {
|
|
return fmt.Errorf("failed to upload file: %w", err)
|
|
}
|
|
|
|
return nil
|
|
} |