easysftp

package module
v0.0.0-...-dca537b Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2020 License: MIT Imports: 8 Imported by: 0

README

potproject/easysftp

SFTP connection easy. pkg/sftp wrapper Golang Library

Usage

import "github.com/potproject/easysftp"

Example

Single File Upload/Download
package main

import (
	"log"

	"github.com/potproject/easysftp"
)

func main() {
	// Connect Example
	// [SFTP Command] $ sftp USERNAME@example.hostname.local -oPort=22 -i ~/.ssh/id_rsa
	esftpSession, err := easysftp.Connect("USERNAME", "example.hostname.local", 22, "~/.ssh/id_rsa")

	// Alternative: Using *ssh.Client
	// esftpSession, err := easysftp.NewClient(conn)

	if err != nil {
		log.Fatalln(err.Error())
	}
	defer esftpSession.Close()

	// SFTP File Get
	// [SFTP Command] sftp> get /tmp/remotefile.txt /tmp/localfile.txt
	downloadBytes, downloadError := esftpSession.Get("/tmp/localfile.txt", "/tmp/remotefile.txt")
	if downloadError != nil {
		log.Fatalln("Download Error:", err.Error())
	}
	log.Println("Download OK:", downloadBytes)

	// SFTP File Put
	// [SFTP Command] sftp> put /tmp/localfile.txt /tmp/remotefile.txt
	uploadBytes, uploadError := esftpSession.Put("/tmp/localfile.txt", "/tmp/remotefile.txt")
	if uploadError != nil {
		log.Fatalln("Upload Error:", err.Error())
	}
	log.Println("Upload OK:", uploadBytes)
}
Directory Upload/Download (Recursively)
	// Recursively Example
	// [SFTP Command] $ sftp USERNAME@example.hostname.local -oPort=22 -i ~/.ssh/id_rsa
	esftpSession, err := easysftp.Connect("USERNAME", "example.hostname.local", 22, "~/.ssh/id_rsa")

	// Alternative: Using *ssh.Client
	// esftpSession, err := easysftp.NewClient(conn)

	if err != nil {
		log.Fatalln(err.Error())
	}
	defer esftpSession.Close()

	// SFTP Directory Get
	// [SFTP Command] sftp> get -r /tmp/remoteDirectory /tmp/localDirectory
	downloadError := esftpSession.GetRecursively("/tmp/localDirectory", "/tmp/remoteDirectory")
	if downloadError != nil {
		log.Fatalln("Download Error:", err.Error())
	}
	log.Println("Download OK")

	// SFTP Directory Put
	// [SFTP Command] sftp> put -r /tmp/localDirectory /tmp/remoteDirectory
	uploadError := esftpSession.PutRecursively("/tmp/localDirectory", "/tmp/remoteDirectory")
	if uploadError != nil {
		log.Fatalln("Upload Error:", err.Error())
	}
	log.Println("Upload OK")

LICENSE

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Easysftp

type Easysftp struct {
	SSHClient  *ssh.Client
	SFTPClient *sftp.Client
}

Easysftp Stored Client val

func Connect

func Connect(username string, host string, port uint16, keyPath string) (esftp Easysftp, err error)

Connect SSH Connection

func NewClient

func NewClient(conn *ssh.Client) (esftp Easysftp, err error)

NewClient SSH Using Conection

func (Easysftp) Close

func (esftp Easysftp) Close() (errors []error)

Close Connection ALL Connection Close

func (Easysftp) Get

func (esftp Easysftp) Get(localFilepath string, remoteFilepath string) (int64, error)

Get is Single File Download

func (Easysftp) GetRecursively

func (esftp Easysftp) GetRecursively(localPath string, remotePath string) error

GetRecursively is Recursively Download entire directories

func (Easysftp) GetWithProgress

func (esftp Easysftp) GetWithProgress(localFilepath string, remoteFilepath string, transferred *int64, total *int64) (int64, error)

GetWithProgress [Experimental] Get with Display Processing Bytes

func (Easysftp) Put

func (esftp Easysftp) Put(localFilepath string, remoteFilepath string) (int64, error)

Put is Single File Upload

func (Easysftp) PutRecursively

func (esftp Easysftp) PutRecursively(localPath string, remotePath string) error

PutRecursively is Recursively Upload entire directories

func (Easysftp) PutWithProgress

func (esftp Easysftp) PutWithProgress(localFilepath string, remoteFilepath string, transferred *int64, total *int64) (int64, error)

PutWithProgress [Experimental] Put with Display Processing Bytes

func (Easysftp) Quit

func (esftp Easysftp) Quit() (errors []error)

Quit alias Close()

type File

type File struct {
	LocalFilepath  string
	RemoteFilepath string
}

File Local and remote filepath

type IOReaderProgress

type IOReaderProgress struct {
	io.Reader
	TransferredBytes *int64 // Total of bytes transferred
}

IOReaderProgress forwards the Read() call Addging transferredBytes

func (*IOReaderProgress) Read

func (iorp *IOReaderProgress) Read(p []byte) (int, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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