sftpclient

package
v0.16.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 7, 2023 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SftpConnect

func SftpConnect(user, password, addr string, key []byte) (*sftp.Client, error)

SftpConnect establishes an SFTP connection to a server. It takes in the username and address of the server, as well as the user's password and private key as parameters. If the private key is valid, it uses that for authentication. If not, it falls back on password authentication. If none of the authentication methods succeed, it returns an error. It returns the connected sftp.Client and any errors encountered.

Params:

user: Username for the server
password: User's password
addr: Address of the server
key: User's private key

Returns:

*sftp.Client: The connected SFTP client
error: Any errors encountered while connecting to the server

func SftpDeleteFile

func SftpDeleteFile(client *sftp.Client, path string) error

SftpDeleteFile function is used to delete a file at a specified path using an SFTP client. It takes a sftp.Client type instance and a path string as parameters.

Returns an error if any issue occurs while deleting the file.

client: an instance of an SFTP client
path: a string representing the path of the file in the SFTP server

Note: The function makes use of the Remove method of the sftp.Client which is responsible for removing a file located at the specific path.

Usage:

err := SftpDeleteFile(client, "/path/to/file")
if err != nil {
    log.Fatal(err)
}

If successfully executed, the function will delete the specified file in the SFTP server.

func SftpDownloadFile

func SftpDownloadFile(client *sftp.Client, remotePath, localPath string) (retErr error)

SftpDownloadFile takes an established sftp client, a remote file path on the sftp server, and a local file path on your system. The function opens the remote file and creates a new file on the local path. It then copies the contents of the remote file into the local file. If it encounters errors during any of these operations (opening source file, creating destination file, or copying contents), it returns the error, including errors occurred during closing either of the files.

client: An established sftp client. remotePath: The full path (including filename) of the remote file on the sftp server. localPath: The full path on the local system where the file will be created.

Returns an error if any file operations fail, otherwise nil.

func SftpListFiles

func SftpListFiles(client *sftp.Client, path string) ([]os.FileInfo, error)

SftpListFiles lists all files at the given path on the SFTP server.

The function accepts two parameters: a `client` which is an instance of the sftp.Client and a `path` which is a string representing the path in the SFTP server where the files are to be listed.

If the operation is successful, it will return a list of os.FileInfo objects, each representing a file or directory on the given path, and a nil error. In case of any error while reading the directory, it will return a non-nil error along with a nil list.

Usage:

fileInfoList, err := SftpListFiles(client, "/path/to/directory")
if err != nil {
    log.Fatal(err)
}
for _, fileInfo := range fileInfoList {
    fmt.Println(fileInfo.Name())
}

Parameters: client : *sftp.Client - a pointer to the SFTP client object path : string - the directory path

Returns: []os.FileInfo - a slice containing os.FileInfo objects error - an error object that reports why the operation failed, or nil if it succeeded

func SftpUploadFile

func SftpUploadFile(client *sftp.Client, localPath, remotePath string) (retErr error)

SftpUploadFile uploads a file to a remote server over SFTP. It takes a sftp.Client object, a local file path and the remote file path as arguments. The local file at 'localPath' is opened for reading, while a new file at 'remotePath' is created on the remote server. The contents of the local file are then copied over to the remote file.

The function handles cleanup of resources, ensuring that the file handles are properly closed once the operation is complete (even in cases where an error might have occurred).

If an error occurs at any point during this operation, it is immediately returned to the calling function. This includes errors that occur when opening the files, creating the remote file, copying the contents, and errors that occur while closing the local or remote file.

The function returns the error value from the last operation (if any), or nil if the operation was successful.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL